Binary search tree is a binary tree.
There is about a node binary tree, the tree has a single root class data structure. Binary tree has a natural recursive structure, the left and right sub-tree for each parent node is a binary tree
class Node{
And E;
Node left;
Node right;
}
The figure is a full binary search tree, but not necessarily full time use, in line with the conditions on the line. Therefore, the storage element must be comparative (the object to be custom comparison rules). Comparable interface inheritance
New node binary search tree
// add a node public void the Add (E E) { root = add(root,e); } private Node add(BST<E>.Node node, E e) { // TODO Auto-generated method stub if(node == null) { node = new Node(e); return node; }else{ if(e.compareTo(node.e)<0) { node.left = add(node.left,e); }else if(e.compareTo(node.e)>0) { node.right = add(node.right,e); } } return node; }
Traversing the binary number search
1. preorder traversal
// preorder traversal, passing the root node, then the node and recursively traversed by thinking about public void preOrder (the Node Node) { IF (Node == null ) { System.out.println("null"); return; } System.out.println(node.e); preOrder(node.left); preOrder(node.right); }
2. preorder
// inorder traversal public void inOrder (the Node Node) { IF (Node == null ) { System.out.println("null"); return; } InOrder (Node.Left); // the left in the right System.out.println (node.e); inOrder(node.right); }
3. postorder, all the same reason, do not write here.