-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeBuilding.java
More file actions
198 lines (168 loc) · 3.73 KB
/
TreeBuilding.java
File metadata and controls
198 lines (168 loc) · 3.73 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import java.util.*;
class treeNode
{
int keyValue;
treeNode leftChild;
treeNode rightChild;
public void displayNode()
{
System.out.println("Key Value: "+keyValue);
}
}
class treeOperation
{
treeNode root;
treeOperation()
{
root = null;
}
public treeNode insertNode(int value)
{
treeNode current, parent, newNode;
newNode = new treeNode();
newNode.keyValue = value;
if(root == null)
root = newNode;
else //go left or right.
{
current = root;//start from the ROOT.
while(true)
{
parent = current;
//we need to access the last non-null node.
if(value < current.keyValue) //left child
{
current = current.leftChild;
if(current == null)
{
parent.leftChild = newNode;
return root;
}
}
else if (value >= current.keyValue) //right child
{
current = current.rightChild;
if(current == null)
{
parent.rightChild = newNode;
return root;
}
}
}
}
return root;
}
public treeNode find(int key)
{
treeNode current = root;// root is initialized when
//
while(current.keyValue!= key)
{
if(key < current.keyValue)
current = current.leftChild;
else if(key > current.keyValue)
current = current.rightChild;
if(current == null)
{
System.out.println("Item Not found");
return null;
}
}
return current;
}
public treeNode minValue(treeNode localRoot)
{
treeNode current = localRoot;
while(current.leftChild!=null)
{
current = current.leftChild;
}
return current;
}
public treeNode maxValue(treeNode localRoot)
{
treeNode current = localRoot;
while(current.rightChild!=null)
{
current = current.rightChild;
}
return current;
}
public void inOrder(treeNode localRoot)
{
if(localRoot != null)
{
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
}
}
public void findDelete(int key)
{
treeNode current = root;
treeNode parent = current;
while(current.keyValue!= key)
{
if(key < current.keyValue)
{
parent = current;
current = current.leftChild;
}
else if(key > current.keyValue)
{
parent = current;
current = current.rightChild;
}
if(current == null)
{
System.out.println("Item Not found");
break;
}
}
if(current != null)//means we found it.
{
//delete node with NO children.
if(current.leftChild == null && current.rightChild == null)
{
System.out.println("Its a leaf");
if(current == root)
parent = current = root = null;//empty tree
else if(parent.leftChild == current)
parent.leftChild = null;
else if(parent.rightChild == current)
parent.rightChild = null;
}
if( (current.leftChild != null && current.rightChild == null) || (current.leftChild == null && current.rightChild != null) )
{
System.out.println("With only ONE child");
}
}
}
}
public class TreeBuilding
{
public static void main(String[] args)
{
treeNode root = null;
treeOperation tProcess = new treeOperation();
int[] inputData = {60,50,55,98,-8,78};
for(int i = 0; i < inputData.length; i++)
root = tProcess.insertNode(inputData[i]);
tProcess.inOrder(root);
System.out.println("Input item to find:");
Scanner inpObj = new Scanner(System.in);
int key = inpObj.nextInt();
treeNode res = tProcess.find(key);
if(res!=null)
res.displayNode();
System.out.println("Min value in BST:");
tProcess.minValue(root).displayNode();
System.out.println("Max value in BST:");
tProcess.maxValue(root).displayNode();
//delete node
System.out.println("Find item and delete:");
key = inpObj.nextInt();
tProcess.findDelete(key);
tProcess.inOrder(root);
}
}