-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.java
More file actions
135 lines (131 loc) · 3.47 KB
/
BST.java
File metadata and controls
135 lines (131 loc) · 3.47 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
public class BST{
class Node{//sub-class for node objects in BST
int key;
Node left, right; //left and right child
public Node(int data){//Node object constructor
key = data;
left = right = null;
}
}
Node root;//head of tree
//constructors for BST object
public BST(int data){
root = new Node(data);
}
public BST(){
root = null;
}
Node insertNode(Node root, int data){//insert function returns root of tree
if(root == null){//base case (empty tree)
root = new Node(data);
return root;
}
Node temp = root;//create temporary object to traverse through tree
while(true){
if(temp.key < data){//traverse right sub-tree
if(temp.right == null){// if right child does not exist
temp.right = new Node(data);
return root;
}
else{
temp = temp.right;
}
}
else{//traverse left subtree
if(temp.left == null){//if left child does not exist
temp.left = new Node(data);
return root;
}
else{
temp = temp.left;
}
}
}
}
void inOrder(Node root){//inorder traversal of tree (recursively)
if(root != null){
inOrder(root.left);
System.out.println(root.key);
inOrder(root.right);
}
}
int maxValue(Node n){//get max value in tree
Node temp = n;
int val = n.key;
while(temp.right != null){//continue to traverse through right childs
val = temp.right.key;
temp = temp.right;
}
return val;
}
int minValue(Node n){//get min value in tree
Node temp = n;
int val = n.key;
while(temp.left != null){//continue to traverse through left childs
val = temp.left.key;
temp = temp.left;
}
return val;
}
Node search(int key, Node n){//value of node as input, returns null if node not found
Node temp = n;
while(temp != null){//keep searching until leaf node is reached
//System.out.println(temp.key);
if(temp.key == key){//check if node has been found
//System.out.println("Node found!");
return temp;
}
else if(temp.key < key){//go to right child
temp = temp.right;
}
else{
temp = temp.left;//go to left child
}
}
//System.out.println("Node not found");
return null;
}
Node deleteNode(int key, Node root){//removes specified node and returns updated tree (recursive)
//base case (empty)
if(root == null){
return root;
}
Node temp = root;//traverse through tree recursively until node is found
if(key < temp.key){
temp.left = deleteNode(key, temp.left);//left subtree
}
else if(key > temp.key){
temp.right = deleteNode(key, temp.right);//right subtree
}
else{//Node has been found
//check if children exist
if(temp.left == null){
return temp.right;
}
else if(temp.right == null){
return temp.left;
}
else{//node with 2 children
temp.key = minValue(temp.right);
temp.right = deleteNode(temp.key, temp.right);
}
}
return temp;
}
public static void main(String[] args){//driver program
BST tree = new BST();
tree.root = tree.insertNode(tree.root, 50);
tree.root = tree.insertNode(tree.root, 30);
tree.root = tree.insertNode(tree.root, 20);
tree.root = tree.insertNode(tree.root, 40);
tree.root = tree.insertNode(tree.root, 70);
tree.root = tree.insertNode(tree.root, 60);
tree.root = tree.insertNode(tree.root, 80);
tree.root = tree.deleteNode(20, tree.root);
tree.root = tree.deleteNode(30, tree.root);
//System.out.println(tree.minValue(tree.root));
tree.inOrder(tree.root);
//tree.search(60);
//tree.search(25);
}
}