-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreeNode.java
More file actions
57 lines (51 loc) · 1.08 KB
/
treeNode.java
File metadata and controls
57 lines (51 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public class treeNode{
private treeNode head;
private int value;
private treeNode next;
public treeNode(int x){
value = x;
next = null;
head = this;
}
public treeNode(int x, treeNode head){
value = x;
next = null;
this.head = head;
}
public void insert(int newValue){
treeNode x = this;
while(x.next != null){
x = x.next;
}
x.next = new treeNode(newValue);
}
public void reverseList(){
if(this !=null){
head = reverseListNodes(null , head);
}
}
private treeNode reverseListNodes(treeNode parent , treeNode child ){
treeNode next = child.next;
child.next = parent;
return (next==null)?child:reverseListNodes(child, next);
}
public void printValues(){
treeNode head = this;
treeNode x = this;
while(x.next != null && x.next != head){
System.out.print(x.value);
System.out.print("->");
x = x.next;
}
System.out.print(x.value);
}
/*public void inOrderTraversal(treeNode x){
if(x == null){
return;
}
x.inOrderTraversal(x.left);
System.out.println(x.value);
x.inOrderTraversal(x.right);
return;
}*/
}