-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST
170 lines (138 loc) · 3.89 KB
/
BST
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
package com.mycompany.node;
public class BST {
private Node root;
void insert(Integer val){
if(seatch(val)) return;
Node p = new Node(val);
if(root == null){
root = p;
return;
}
while(true){
Node curr = root;
if(val < (Integer)curr.data){
if(curr.left != null) {
curr = curr.left;
}
else{
curr.left = p;
break;
}
}
else {
if(curr.right != null) {
curr = curr.right;
}
else{
curr.right = p;
break;
}
}
}
}
void recIntert(Integer val){
root = recInsert(root, val);
}
private Node recInsert(Node curr, Integer val) {
if(curr == null){
curr = new Node(val);
return curr;
}
if(val < (Integer)curr.data){
curr.left = recInsert(curr.left, val);
}
else if(val > (Integer)curr.data){
curr.right = recInsert(curr.right, val);
}
return curr;
}
void inOrder(){
RecInOrder(root);
}
private void RecInOrder(Node root){
if(root == null) return;
RecInOrder(root.left);
System.out.print(root.data+" ");
RecInOrder(root.right);
}
void preOrder(){
RecInOrder(root);
}
private void RecPreOrder(Node root) {
if(root == null) return;
System.out.print(root.data+" ");
RecPreOrder(root.left);
RecPreOrder(root.right);
}
void postOrder(){
RecInOrder(root);
}
private void RecPostOrder(Node root) {
if(root == null) return;
RecPostOrder(root.left);
RecPostOrder(root.right);
System.out.print(root.data+" ");
}
boolean seatch(Integer x){
return search(root, x);
}
private boolean search(Node curr, Integer x) {
if(curr == null) return false;
if(x < (Integer)curr.data){
return search(curr.left,x);
}
else if(x > (Integer) curr.data){
return search(curr.right, x);
}
return true;
}
Integer minimum(){
return minimum(root);
}
private Integer minimum(Node root) {
if(root == null)
return -1;
if(root.left == null)
return (Integer)root.data;
return minimum(root.left);
}
Integer maximum(){
return maximum(root);
}
private Integer maximum(Node root) {
if(root == null)
return -1;
if(root.right == null)
return (Integer)root.data;
return maximum(root.right);
}
void printLeafNode(){
PrintLeafNode(root);
}
private void PrintLeafNode(Node root) {
if(root == null) return;
if(root.left == null && root.left == null)
System.out.print(root.data+ " ");
PrintLeafNode(root.left);
PrintLeafNode(root.right);
}
void remove(Integer val){
root = remove(root,val);
}
private Node remove(Node root, int val){
if(root == null) return null;
else if((Integer)root.data > val){
root.left = remove(root.left, val);
}
else if((Integer)root.data < val){
root.right = remove(root.right,val);
}
else if(root.left == null) return root.right;
else if(root.right == null) return root.left;
else {
root.data = minimum(root.right);
root.right = remove(root.right, (Integer)root.data);
}
return root;
}
}