-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST.java
160 lines (143 loc) · 3.68 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
158
159
160
class Node {
int data;
Node left, right;
Node(int data) {
this.data = data;
left = right = null;
}
}
class BST {
static Node root;
BST() {
root = null;
}
static Node Succeser(Node root, int key) {
Node succNode = null;
while (root != null) {
if (root.data > key) {
succNode = root;
root = root.left;
} else {
root = root.right;
}
}
return succNode;
}
Node Predessor(Node root, int key) {
Node predNode = null;
while (root != null) {
if (root.data < key) {
predNode = root;
root = root.right;
} else {
root = root.left;
}
}
return predNode;
}
static Node DeleteRec(Node root, int key) {
if (root == null) {
return root;
}
if (root.data > key) {
root.left = DeleteRec(root.left, key);
} else if (root.data < key) {
root.right = DeleteRec(root.right, key);
} else {
if (root.left == null) {
return root.right;
}
if (root.right == null) {
return root.left;
}
root.data = Succeser(root.right, key).data;
root.right = DeleteRec(root.right, root.data);
}
return root;
}
void InsertItr(Node root, int key) {
Node x = new Node(key);
if (root == null) {
root = x;
return;
}
Node prev = null;
Node temp = root;
while (temp != null) {
prev = temp;
if (key > temp.data) {
temp = temp.right;
} else {
temp = temp.left;
}
}
if (key < prev.data) {
prev.left = x;
} else {
prev.right = x;
}
}
void Insertitr(int key) {
InsertItr(root, key);
}
void Insertrec(int key) {
root = InsertRec(root, key);
}
static Node InsertRec(Node root, int key) {
if (root == null) {
root = new Node(key);
// temp = flag;
return root;
}
if (root.data > key) {
root.left = InsertRec(root.left, key);
} else {
root.right = InsertRec(root.right, key);
}
return root;
}
void Levelorder() {
LevelOrder(root);
}
static void LevelOrder(Node root) {
if (root == null) {
return;
}
System.out.print(root.data + " ");
LevelOrder(root.left);
LevelOrder(root.right);
}
void Inorder() {
InOrder(root);
}
static void InOrder(Node temp) {
if (temp == null) {
// System.out.println("Hello");
return;
}
InOrder(temp.left);
System.out.print(temp.data + " ");
InOrder(temp.right);
}
public static void main(String[] args) {
BST tree = new BST();
tree.Insertrec(10);
tree.Insertrec(5);
tree.Insertrec(15);
tree.Insertrec(2);
tree.Insertrec(8);
tree.Insertrec(13);
tree.Insertrec(18);
tree.Insertitr(20);
System.out.println("This is example of BST");
tree.Levelorder();
tree.Inorder();
System.out.println();
System.out.println(tree.Predessor(root, 8).data);
Node tempNode = DeleteRec(root, 15);
System.out.println(tempNode.data);
// DeleteRec(root, 0);
// System.out.println(tempNode1.data);
LevelOrder(root);
}
}