/
BinarySearchTreeIterator.java
153 lines (125 loc) · 3.62 KB
/
BinarySearchTreeIterator.java
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
import java.io.*;
import java.util.*;
import java.util.Iterator;
class Solution {
static class Node {
int key;
Node left;
Node right;
Node parent;
Node(int key) {
this.key = key;
left = null;
right = null;
parent = null;
}
}
static class BinarySearchTree {
Node root;
// Iterator part
class BSTIterator implements Iterator<Node> {
Node last = null;
BSTIterator(Node root) {
if (root == null) return;
last = root;
while (last.left != null)
last = last.left;
}
public boolean hasNext() {
return last != null;
}
public Node next() {
Node cur = last;
last = findSuccessor(last);
return cur;
}
private Node findSuccessor(Node root) {
if (root == null) return null;
if (root.right != null) {
Node tmp = root.right;
while (tmp.left != null) tmp = tmp.left;
return tmp;
}
Node father = root.parent;
Node child = root;
while (father != null && father.left != child) {
child = father;
father = father.parent;
}
return father;
}
}
Iterator<Node> iterator() {
BSTIterator iter = new BSTIterator(root);
return iter;
}
// Given a binary search tree and a number, inserts a new node
// with the given number in the correct place in the tree
void insert(int key) {
// 1. If the tree is empty, create the root
if(this.root == null) {
this.root = new Node(key);
return;
}
// 2) Otherwise, create a node with the key
// and traverse down the tree to find where to
// to insert the new node
Node currentNode = this.root;
Node newNode = new Node(key);
while(currentNode != null) {
if(key < currentNode.key) {
if(currentNode.left == null) {
currentNode.left = newNode;
newNode.parent = currentNode;
break;
} else {
currentNode = currentNode.left;
}
} else {
if(currentNode.right == null) {
currentNode.right = newNode;
newNode.parent = currentNode;
break;
} else {
currentNode = currentNode.right;
}
}
}
}
// Return a reference to a node in the BST by its key.
// Use this method when you need a node to test your
// findInOrderSuccessor method on
Node getNodeByKey(int key) {
Node currentNode = this.root;
while(currentNode != null) {
if(key == currentNode.key) {
return currentNode;
}
if(key < currentNode.key) {
currentNode = currentNode.left;
} else {
currentNode = currentNode.right;
}
}
return null;
}
}
/***********************************************
* Driver program to test findInOrderSuccessor *
***********************************************/
public static void main(String[] args) {
Node test = null, succ = null;
// Create a Binary Search Tree
BinarySearchTree tree = new BinarySearchTree();
tree.insert(20);
tree.insert(9);
tree.insert(25);
tree.insert(5);
tree.insert(12);
tree.insert(11);
tree.insert(14);
Iterator<Node> iter = tree.iterator();
while (iter.hasNext())
System.out.println(iter.next().key);
}
}