Article Directory
What is the binary search tree
Binary binary search trees (binary search tree), which is either empty tree, or having the following properties: if its left subtree is not empty, then the value of the left sub-tree, all the nodes are less than its root value node; if its right subtree is not empty, the value of the right sub-tree, all the nodes are greater than the value of the root; its left and right sub-trees are binary search tree. Each sub-tree nodes are formed to satisfy the properties.
Definition tree node
public class BST01<E extends Comparable<E>> {
private class Node {
E e;
Node left; //左子节点
Node right; //右子节点
public Node(E e) {
this.e = e;
}
}
}
Add Nodes
Define a root node attribute Node root;
Recursive
public void add(E e) {
root = add(root, e);
}
private Node add(Node node, E e) {
if (node == null) {
return new Node(e);
}
if (e.compareTo(node.e) > 0) {
node.right = add(node.right, e);
} else /*if (e.compareTo(node.e) < 0)*/ {
node.left = add(node.left, e);
}
return node;
}
Else to achieve the above, it contains <= node; if the annotation is opened, the free = node.
Cycle to achieve
public void addByCircle(E e) {
if (root == null) {
root = new Node(e);
} else {
Node p = root;
while (p != null) {
if (e.compareTo(p.e) > 0) {
if (p.right == null) {
p.right = new Node(e);
break;
} else {
p = p.right;
}
} else /*if (e.compareTo(p.e) < 0)*/ {
if (p.left == null) {
p.left = new Node(e);
break;
} else {
p = p.left;
}
} /*else {
break;
}*/
}
}
}
Else to achieve the above, it contains <= node; if the two annotations open, free = node.
Hierarchy traversal (breadth-first traversal)
Print out the value of all elements in the tree nodes e, and printed in a hierarchical order.
public void levelOrder1() {
Queue<Node> queue = new LinkedList<>();
queue.add(root);
StringBuilder sb = new StringBuilder();
while (!queue.isEmpty()) {
Node node = queue.remove();
sb.append(node.e + " ");
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
System.out.println(sb);
}
Starting from the root, each node removed, it will keep the left and right child nodes to a FIFO queue.
test
public static void main(String[] args) {
BST01<Integer> bst = new BST01<>();
bst.add(33);
bst.add(31);
bst.add(36);
bst.add(19);
bst.add(92);
bst.add(9);
bst.add(38);
bst.add(98);
bst.add(43);
bst.add(54);
bst.add(54);
bst.add(98);
bst.add(19);
bst.levelOrder1();
}
Export
33 31 36 19 92 9 38 98 19 43 98 54 54
Increased levels of depth attribute node
From the above output, view hierarchy, is not very clear.
Next, Node
add a level of depth representation propertydepth
private class Node {
E e;
Node left;
Node right;
int depth;
public Node(E e) {
this.e = e;
}
public Node(E e, int depth) {
this(e);
this.depth = depth;
}
}
Accordingly, to modify and add the node traversal methods.
The final follows
public class BST01<E extends Comparable<E>> {
private class Node {
E e;
Node left;
Node right;
int depth;
public Node(E e) {
this.e = e;
}
public Node(E e, int depth) {
this(e);
this.depth = depth;
}
}
private Node root;
public void add(E e) {
root = add(root, e, 0);
}
private Node add(Node node, E e, int depth) {
if (node == null) {
return new Node(e, depth);
}
if (e.compareTo(node.e) > 0) {
node.right = add(node.right, e, depth + 1);
} else /*if (e.compareTo(node.e) <= 0)*/ {
node.left = add(node.left, e, depth + 1);
}
return node;
}
public void addByCircle(E e) {
if (root == null) {
root = new Node(e);
root.depth = 0;
} else {
Node p = root;
while (p != null) {
if (e.compareTo(p.e) > 0) {
if (p.right == null) {
p.right = new Node(e, p.depth + 1);
break;
} else {
p = p.right;
}
} else /*if (e.compareTo(p.e) < 0)*/ {
if (p.left == null) {
p.left = new Node(e, p.depth + 1);
break;
} else {
p = p.left;
}
} /*else {
break;
}*/
}
}
}
public void levelOrder1() {
Queue<Node> queue = new LinkedList<>();
queue.add(root);
StringBuilder sb = new StringBuilder();
while (!queue.isEmpty()) {
Node node = queue.remove();
sb.append(node.e + " ");
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
System.out.println(sb);
}
public void levelOrder2() {
Queue<Node> queue = new LinkedList<>();
queue.add(root);
StringBuilder sb = new StringBuilder();
int tempDepth = -1;
while (!queue.isEmpty()) {
Node node = queue.remove();
if (tempDepth != node.depth) {
sb.append("\n");
}
sb.append(node.e + "(depth=" + node.depth +") ");
tempDepth = node.depth;
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
System.out.println(sb);
}
}
test
Add the same test data, and then call levelOrder2()
traversal.
Export
33(depth=0)
31(depth=1) 36(depth=1)
19(depth=2) 92(depth=2)
9(depth=3) 38(depth=3) 98(depth=3)
19(depth=4) 43(depth=4) 98(depth=4)
54(depth=5)
54(depth=6)
This output structure, to understand this binary search tree more intuitive.
The entire tree should look like this
33
31 36
19 92
9 38 98
19 43 98
54
54