-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.java
More file actions
125 lines (114 loc) · 2.73 KB
/
BST.java
File metadata and controls
125 lines (114 loc) · 2.73 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
import java.util.ArrayList;
/**
* Binary search tree implementation
* @author bhills
*
*/
public class BST {
Node root;
public static class Node {
int data, height;
Node left, right;
Node(int data){
this.data = data;
}
public String toString(){
return data+"";
}
}
public BST(int[] initialValues){
root = new Node(initialValues[0]);
for(int i=0; i<initialValues.length; i++){
insert(root, new Node(initialValues[i]));
}
}
/**
* Inserts from a particular starting origin node
*
* Inserting a node at a subtree that does not have the particular value, but has no way of knowing that, may result in duplicate entries in the overall BST.
* @param origin
* @param n
*/
private void insert(Node origin, Node n){
if(origin.data > n.data){ // new value belongs in left subtree
if(origin.left==null){
origin.left = n;
} else {
insert(origin.left, n);
}
} else if(origin.data < n.data) { // new value belongs in right subtree
if(origin.right==null){
origin.right = n;
} else {
insert(origin.right, n);
}
} else {
// do nothing. value already exists in the BST.
}
}
public void printInOrder(){
printInOrder(root);
}
public void printInOrder(Node n){
if(n.left != null){
printInOrder(n.left);
}
System.out.println(n);
if(n.right != null){
printInOrder(n.right);
}
}
public Node findNode(Node subroot, int data){
if(subroot.data<data){
return findNode(subroot.right, data);
} else if (subroot.data>data){
return findNode(subroot.left, data);
} else if (subroot.data == data){
return subroot;
} else {
return null;
}
}
public Node findNode(int data){
if(root.data<data){
return findNode(root.right, data);
} else if (root.data>data){
return findNode(root.left, data);
} else if (root.data==data){
return root;
} else {
return null;
}
}
public void displayBST(){
// This is the base case to get the recursion started. ONLY 1 ROOT
ArrayList<Node> nextLayer = new ArrayList<Node>();
nextLayer.add(this.root);
while((nextLayer=this.printLayer(nextLayer)).size()>0){
System.out.println();
}
}
private ArrayList<Node> printLayer(ArrayList<Node> nodeLayer){
ArrayList<Node> next = new ArrayList<Node>();
for(Node n: nodeLayer){
if(n.left!=null)
next.add(n.left);
if(n.right!=null)
next.add(n.right);
System.out.print(" "+n.data);
}
return next;
}
public static void main(String[] args){
int[] input0 = new int[]{5,4,7,10,3,1,2,-4,-2,8,9};
BST bst = new BST(input0);
bst.printInOrder();
System.out.println("-------");
bst.displayBST();
System.out.println("-------");
Node n = bst.findNode(2);
bst.printInOrder();
System.out.println("-------");
bst.displayBST();
}
}