-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeNode.java
More file actions
145 lines (128 loc) · 2.74 KB
/
TreeNode.java
File metadata and controls
145 lines (128 loc) · 2.74 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
public class TreeNode{
private int data;
private TreeNode leftChild;
private TreeNode rightChild;
/*
Constructor of TreeNode class
*/
public TreeNode(int data){
this.data = data;
}
public TreeNode find(int data) {
if(this.data == data){
return this;
}
else if(data < this.data && leftChild != null ){
return leftChild.find(data);
}
else if(rightChild != null){
return rightChild.find(data);
}
return null;
}
/*
Inserting
*/
public void insert(int data){
if(data >= this.data){
if(this.rightChild == null){
this.rightChild = new TreeNode(data);
}
else{
this.rightChild.insert(data);
}
}
else{
if(this.leftChild == null){
this.leftChild = new TreeNode(data);
}
else{
this.leftChild.insert(data);
}
}
}
//Getters
public int getData(){
return this.data;
}
public TreeNode getLeftChild(){
return this.leftChild;
}
public TreeNode getRightChild(){
return this.rightChild;
}
//Setters
public void setLeftChild(TreeNode leftChild){
this.leftChild = leftChild;
}
public void setRightChild(TreeNode rightChild){
this.rightChild = rightChild;
}
/*
Left most node is the node with the smallest element
*/
public int findMin(){
if(this.leftChild == null){
return this.data;
}
return this.leftChild.findMin();
}
/*
RightMost node is the node with the largest element
*/
public int findMax(){
if(this.rightChild == null){
return this.data;
}
return this.rightChild.findMax();
}
/*
55
42 57
Print: 42, 55, 57
*/
public void traverseInOrder() {
if (this.leftChild != null)
this.leftChild.traverseInOrder();
System.out.print(this + " ");
if (this.rightChild != null)
this.rightChild.traverseInOrder();
}
public int height() {
if (isLeaf()) return 1;
int left = 0;
int right = 0;
if (this.leftChild != null)
left = this.leftChild.height();
if (this.rightChild != null)
right = this.rightChild.height();
return (left > right) ? (left + 1) : (right + 1);
}
public boolean isLeaf() {
return this.leftChild == null && this.rightChild == null;
}
public int numOfLeafNodes() {
if (isLeaf()) return 1;
int leftLeaves = 0;
int rightLeaves = 0;
if (this.leftChild != null)
leftLeaves = leftChild.numOfLeafNodes();
if (this.rightChild != null)
rightLeaves = rightChild.numOfLeafNodes();
return leftLeaves + rightLeaves;
}
public static TreeNode addSorted(int[] data, int start, int end) {
if (end >= start) {
int mid = (start+end)/2;
TreeNode newNode = new TreeNode(data[mid]);
newNode.leftChild = addSorted(data, start, mid-1);
newNode.rightChild = addSorted(data, mid+1, end);
return newNode;
}
return null;
}
//ToString
public String toString(){
return ""+this.data;
}
}