-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.java
More file actions
160 lines (140 loc) · 4.23 KB
/
BST.java
File metadata and controls
160 lines (140 loc) · 4.23 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
import java.util.ArrayList;
import java.util.LinkedList;
public class BST {
class Node {
int val;
Node left;
Node right;
public Node(int val) {
this.val = val;
}
}
public Node buildSearchTree(Node root, int key) {
if (root == null) {
root = new Node(key);
} else if (root.val > key) {
root.left = buildSearchTree(root.left, key);
} else {
root.right = buildSearchTree(root.right, key);
}
return root;
}
public Node insert(Node root, int val) {
if (root == null)
return root= new Node(val);
else if (root.val < val) {
root.right =insert(root.right, val);
} else {
root.left = insert(root.left, val);
}
return root;
}
public void inorder(Node root) {
if (root == null) return;
inorder(root.left);
System.out.print(root.val + " ");
inorder(root.right);
}
public boolean search(Node root, int key) {//0(H)
if (root == null) return false;
if (root.val > key) {
return search(root.left, key);
} else if (root.val < key) {
return search(root.right, key);
} else {
return true; // Key found
}
}
public Node delete(Node root, int key) {
if (root == null) {
return null;
}
if (root.val > key) {
root.left = delete(root.left, key);
} else if (root.val < key) {
root.right = delete(root.right, key);
} else {
// Case 1: Node is a leaf node
if (root.left == null && root.right == null) {
return null;
}
// Case 2: Node has only one child
if (root.left == null) {
return root.right;
} else if (root.right == null) {
return root.left;
}
// Case 3: Node has two children
Node IS = inorderSuccessor(root.right);
root.val = IS.val;
root.right = delete(root.right, IS.val);
}
return root;
}
public Node inorderSuccessor(Node root) {
while (root.left != null) {
root = root.left;
}
return root;
}
public void range(Node root, int x, int y) {
if (root == null) return;
else if (root.val >= x && root.val <= y) {
range(root.left, x, y);
System.out.print(root.val + " ");
range(root.right, x, y);
} else if (root.val >= y) {
range(root.left, x, y);
} else {
range(root.right, x, y);
}
}
public void pathToLeaf(Node root, ArrayList<Integer> path) {
if (root == null) return;
path.add(root.val);
//leaf
if (root.left == null && root.right == null) {
printPath(path);
}//non-leaf
else {
pathToLeaf(root.left, path);
pathToLeaf(root.right, path);
}
path.remove(path.size() - 1);
}
public void printPath(ArrayList<Integer> path) {
for (int i = 0; i < path.size(); i++) {
System.out.print(path.get(i) + "->");
}
System.out.println("null");
}
public static void main(String[] args) {
int a[] = {-10,-3,0,5,9};
BST b = new BST();
Node root = null;
for (int i = 0; i < a.length; i++) {
root = b.buildSearchTree(root, a[i]);
}
b.inorder(root);
// System.out.println();
// if (b.search(root, 5)) {
// System.out.println("founded");
// } else {
// System.out.println("not found");
// }
//// root= b.delete(root, 7);
//// System.out.println("Tree after deleting 7:");
//// b.inorder(root);
//// System.out.println();
////
//// // Delete node with value 5
//// root = b.delete(root, 5);
//// System.out.println("Tree after deleting 5:");
//// b.inorder(root);
//// System.out.println();
////
//// System.out.println(root.val);
// b.range(root, 1, 4);
// b.pathToLeaf(root, new ArrayList<>());
}
}