-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTreeNode.java
More file actions
115 lines (96 loc) · 3.25 KB
/
BinarySearchTreeNode.java
File metadata and controls
115 lines (96 loc) · 3.25 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
public class BinarySearchTreeNode {
BinarySearchTreeNode left, right;
int data;
public BinarySearchTreeNode(int data){
this.data = data;
}
//Time complexity: Avg: O(h) h = height of the tree. Worst: O(n) n = number of nodes in the tree (similar to a linkedlist)
public void insert(int value){
//check which side to insert the new node
//Left:
if(data <= value){
if(left == null){
//inserting a new node
left = new BinarySearchTreeNode(value);
}
else{
//recursively find the left most node that is null, position to insert
left.insert(value);
}
}
//Right:
else{
if(right == null){
//insert a new node
right = new BinarySearchTreeNode(value);
}
else{
//recursively find the right most node that is null, position to insert
right.insert(value);
}
}
}
//Time complexity: Best: O(1), the root node was searched. Avg: O(h), depending on max depth of the tree in the left or right. Worst: O(n), unbalance can create a linear structure
public Boolean contains(int value){
//base case, also could find the data at the root initially giving O(1) time for search
if(data == value){
return true;
}
//Search the left subtree
if(data < value){
//no left subtree, value does not exist
if(left == null){
return false;
}
else{
//iterate through the left subtree recursively and stop when the data is found (base case)
return left.contains(value);
}
}
//Search the right subtree
else{
//no right subtree, value does not exist
if(right == null){
return false;
}
else{
//iterate through the right subtree recursively and stop when the data is found (base case)
return right.contains(value);
}
}
}
/**
* Time Complexity for the following depth first traversals:
* Conceptually: O(n) - Have to traverse each node to print the value, amount work done on each node to print its data is the same operation
* Analytically: O(n+e), n = # of nodes, e = # of edges.
* The max # of edges on a single binary node is 2. Max # of edges in a BST = (total nodes - 1).
* The time complexity can then be re-written: O(n + (n-1)) = O(n).
*/
public void printInOrder(){
if(left != null){
left.printInOrder();
}
System.out.println(data);
if(right != null){
right.printInOrder();
}
}
public void printPreOrder(){
System.out.println(data);
if(left != null){
left.printInOrder();
}
if(right != null){
right.printInOrder();
}
}
public void printPostOrder(){
if(left != null){
left.printInOrder();
}
if(right != null){
right.printInOrder();
}
System.out.println(data);
}
}