-
Notifications
You must be signed in to change notification settings - Fork 135
/
BST.java
157 lines (142 loc) · 3.99 KB
/
BST.java
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
/**
*
* @author Lakshitha Samod
*/
public class BST {
public static void main(String[] args) {
// TODO code application logic here
BSTree t = new BSTree();
t.addNode(3);
t.addNode(4);
t.addNode(1);
t.addNode(7);
t.addNode(2);
t.addNode(5);
t.addNode(6);
// System.out.println("inorder");
// t.inorderTraversal(t.root);
// System.out.println("preorder");
// t.preorderTraversal(t.root);
// System.out.println("postorder");
// t.postorderTraversal(t.root);
// System.out.println("find 1 ="+t.findNode(1));
t.inOrderTraversal(t.root);
}
}
class BSTNode {
int data;
BSTNode leftChild;
BSTNode rightChild;
public BSTNode(int data){
this.data=data;
}
@Override
public String toString(){
return "data -> "+data;
}
public int minValue() {
if(this.leftChild == null){
return this.data;
}
else {
return this.leftChild.minValue();
}
}
public boolean remove(int data, BSTNode parent){
if(data < this.data){
if(this.leftChild != null){
return this.leftChild.remove(data,this);
}
else {
return false;
}
}
else if(data > this.data){
if(this.rightChild != null){
return this.rightChild.remove(data,this);
}
else {
return false;
}
}
else {
if(this.leftChild != null && this.rightChild != null){
this.data = this.rightChild.minValue();
this.rightChild.remove(this.data,this);
}
else if( parent.leftChild == this){
parent.leftChild = (this.leftChild == null)? this.rightChild:this.leftChild;
}
else if(parent.rightChild == this){
parent.rightChild = (this.leftChild == null)? this.rightChild:this.leftChild;
}
return true;
}
}
}
class BSTree {
BSTNode root;
public boolean remove(int data){
if(root == null){
return false;
}
if(root.leftChild == null){
root = root.rightChild;
}
else if(root.rightChild == null){
root = root.leftChild;
}
return root.remove(data,null);
}
public void addNode(int data) {
BSTNode newNode = new BSTNode(data);
if(root == null){
root = newNode;
}
else {
BSTNode currentNode = root;
while (true){
BSTNode parentNode = currentNode;
if(currentNode.data > newNode.data){
currentNode = currentNode.leftChild;
if(currentNode == null){
parentNode.leftChild = newNode;
return;
}
}
else {
currentNode = currentNode.rightChild;
if(currentNode == null){
parentNode.rightChild = newNode;
return;
}
}
}
}
}
public void inOrderTraversal(BSTNode currentNode) {
if(currentNode == null){
return;
}
inOrderTraversal(currentNode.leftChild);
System.out.println(currentNode);
inOrderTraversal(currentNode.rightChild);
}
public BSTNode findNode(int data) {
BSTNode currentNode = root;
while (currentNode != null){
if(currentNode.data == data){
return currentNode;
}
else{
if(data < currentNode.data){
currentNode = currentNode.leftChild;
}
else {
currentNode = currentNode.rightChild;
}
}
}
return null;
}
}