The order of the tree -two-tree-two-fork lookup tree will be clarified first.
One, tree
A tree is a finite set of n (n≥0) nodes. In any tree that is not empty:
(1) There is only one specific node known as root (root);
(2) When n>1, the remaining nodes can be divided into m (m>0) non-intersecting finite set T1,T2,...,TM, each of which is itself a tree, and called the root of the subtree (subtree).
node degree (degree): The number of subtree owned by a node is called the degree of Node (degree). Nodes with a degree of 0 are called leaf (leaf) or terminal nodes. Nodes that are not 0 degrees are called non-terminal nodes or branch nodes.
The degree of the
tree: The maximum value of the degree of each node in the tree.
Children and parents: The root of the node's subtree is called the child of that node, and accordingly, the node is called the child's parent (parent).
The
level of the node: It is calculated from the root node, the root is the first layer, the child of the root is the second layer, and so on. The maximum level of nodes in a tree is called the depth (Depth) or height of the tree.
If the subtree of the nodes in the tree is considered sequential (that is, not interchangeable), the tree is called an ordered tree, otherwise it is called an unordered tree.
Two, two fork tree
The two binary tree is characterized by a maximum of two subtrees trees per node (i.e., nodes that do not exist in the binary tree with a degree greater than 2), and the sub-tree has a left-right point.
The nature of the binary tree:
(1), there are at most 2i-1 nodes (i≥1) on the first layer of a binary tree.
(2), the depth of the two-fork tree with a maximum of 2k-1 nodes (k≥1).
(3), to any binary tree, if its terminal node is n0, the degree of 2 of the node is N2, then n0=n2+1.
There are a lot of terms about the tree, here do not do too much text interpretation, do not want to draw. I've looked at the diagram below to illustrate that the graph reference comes from http://blog.csdn.net/u012152619/article/details/42059325, which allows you to intuitively understand the tree's path, root, parent, child, leaf, subtree, layer and other concepts
Three, binary search tree (left < Middle < right)
We start with a special, widely used two-fork tree: A two-fork search tree.
Binary Search Tree Properties:
(1), if its left subtree is not empty, then the value of all nodes on the left subtree is less than the value of its root node;
(2), if its right subtree is not empty, then the value of all nodes on the right subtree is greater than the value of its root node;
(3), its left and right sub-tree is also a two-fork search tree.
In a nutshell, the binary search tree is characterized by the key value of the left child node of a node is less than this node, and the key value of the right child node is greater than or equal to the parent node.
The basic operation of binary search tree is to find, insert, delete, traverse , described below:
1, find (search)
We already know that the binary search tree is characterized by the left dial hand node being smaller than the parent node, and the right child node being greater than or equal to the parent node. Find a node, first start from the root node, if the value of the element is less than the root node, then turn to the left child node, or to the right child node, and so on, until the node is found, or to the last leaf node is still not found, the proof tree does not have the node
The code is:
/** finds the element, returns True * /public boolean search (e e) {treenode<e>-current = root;//starts from the root element while the current! = NULL) {if (E.compareto (Current.element) < 0) {//If it is smaller than the current element value, it points to the left subtree current = Current.left; } else if (E.compareto (current.element) > 0) {//If it is larger than the current element value, it points to the right subtree current = current.right; } else/element equals current.element return true;//discovers element, returns True } return false; }
2, inserting (insert)
inserting a new node first to determine the location of the insertion, the key idea is to determine where the parent node of the new node resides .
Code:
/** Insert Element, successfully returns TRUE */Public Boolean insert (E e) { if (root = null) root = Createnewnode (E);//If the tree is empty, create a node C3/>else { //marks the current parent node position treenode<e> parent = null; treenode<e> current = root; while (current! = null) if (E.compareto (current.element) < 0) { parent = current; current = Current.left; } else if (E.compareto (current.element) > 0) { parent = current; current = Current.right; } else return false;//have duplicate nodes, cannot be inserted //Create a new node to hang under parent node if (E.compareto (parent.element) < 0) Parent.left = Createnewnode (e); else parent.right = Createnewnode (e); } size++; return true; Insert Success }
3, deleting (delete)
Deleting one of the nodes in BST is the most troublesome operation, summarizing about the following two ways:
Case 1: Delete point no left child , it is only necessary to connect the parent node of the node with the child of the current node
CASE2: Delete Point has left child. In this case, the right node of the left subtree of the current node is found first, because the right node of the left subtree of one node is smaller than the leftmost node of the right subtree, the right node is copied to the delete point, and the right node is deleted .
Code:
/** Delete node, delete successfully returns True, does not return false*/public in tree, Boolean delete (E e) {//Tag deleted node and parent node location of the node treenode<e> parent = Nu ll treenode<e> current = root; while (current! = null) {if (E.compareto (current.element) < 0) {parent = current; current = Current.left; } else if (E.compareto (current.element) > 0) {parent = current; current = Current.right; } else break; element in this tree} if (current = = null) return false; The element is not in the tree if (current.left = = null) {//First case: The element has no left subtree, the right subtree of the current node is hung directly on the right subtree of its parent node//The right subtree of the current node is hung directly on the right subtree of its parent node I F (parent = = NULL) {root = Current.right; } else {if (E.compareto (parent.element) < 0) Parent.left = current.right; else parent.right = current.right; }} else {////second case: element has left dial hand tree, first find the right node of the left subtree of the current node//mark the parent node of the current node's left subtree and the right-most node treenode<e> Parentofrightmos t = current; Treenode<e> rightmOST = Current.left; Always right, find the right-most node, because the right-most node of the left subtree of a node is also smaller than the leftmost node of the right subtree (rightmost.right! = null) {parentofrightmost = rightmost; rightmost = Rightmost.right; Always to the right}/* * The purpose of the above code is to find the Zuozi right node of the deleted node, because the right node of the left subtree of a node is also smaller than the leftmost node of the right subtree///Find the right node and drop it to the current location to be deleted Current.element = rightmost.element; Eliminate the right-most node if (parentofrightmost.right = = rightmost) Parentofrightmost.right = rightmost.left;//The left child of the right-most node rime at its parent node Right subtree on else//specific case: Parentofrightmost = = Current Parentofrightmost.left = Rightmost.left; } size--; return true; Delete succeeded}
Here's a look at the binary tree composition:
Tree.java
Package Com.hust.cn;public Interface Tree<e extends comparable<e>> { //Find element Public Boolean search ( e e); Insert Element Public Boolean insert (e e); Delete Element public Boolean Delete (e e); The middle sequence traverses public void Inorder (); Post-traverse public void Postorder (); Pre-order traverse public void Preorder (); Returns the size of public int getsize (); Empty public Boolean isEmpty (); Returns the iterator for the tree public java.util.Iterator Iterator ();}
Abstracttree.java
Package Com.hust.cn;public abstract class Abstracttree<e extends comparable<e>> implements Tree<e > {//sequence traversal public void Inorder () { } //post-traversal public void Postorder () { } //Pre-order traverse Public void Preorder () { } //Empty public Boolean isEmpty () { return getsize () = = 0; } Returns the iterator of the tree public java.util.Iterator Iterator () { return null; }}
Binarytree.java
Package Com.hust.cn;public class Binarytree<e extends comparable<e>> extends abstracttree<e> {prote CTED treenode<e> root;//Node class, is an inner class protected int size = 0; /** constructor */Public BinaryTree () {}/** object array create a binary lookup tree */public BinaryTree (e[] objects) {for (int i = 0; i < OB Jects.length; i++) Insert (objects[i]); /** finds the element, returns True */public boolean search (e e) {treenode<e> current = root;//starts from the root element while the current! = Nu LL) {if (E.compareto (current.element) < 0) {//If it is smaller than the current element value, it points to the left subtree current = Current.left; } else if (E.compareto (current.element) > 0) {//If it is larger than the current element value, it points to the right subtree current = Current.right; } else//element equals current.element return true; Find element, return true} return false; /** Insert Element, successfully returns TRUE */public boolean insert (E e) {if (Root = null) root = Createnewnode (E);//If the tree is empty, create a node else {//mark the current parent node position treenode<e> parent = null; TreeNode<e> current = root; while (current! = null) if (E.compareto (current.element) < 0) {parent = current; current = Current.left; } else if (E.compareto (current.element) > 0) {parent = current; current = Current.right; } else return false; There are duplicate nodes that cannot be inserted//create a new node to hang under the parent node if (E.compareto (parent.element) < 0) Parent.left = Createnewnode (e); else Parent.right = Createnewnode (e); } size++; return true; Insert Success}/* Create a new node */protected treenode<e> Createnewnode (e e) {return new treenode<e> (e); }/** in sequence traversal */public void inorder () {inorder (root); }/** the sequence traversal from the root node, recursive method */protected void inorder (treenode<e> root) {if (root = null) return; Inorder (Root.left); System.out.print (Root.element + ""); Inorder (Root.right); }/** Post-traversal */public void Postorder () {postorder (root); /** post-traversal from the root node, recursive method */protected VOID Postorder (treenode<e> root) {if (root = null) return; Postorder (Root.left); Postorder (Root.right); System.out.print (Root.element + ""); }/** Pre-order traversal */public void preorder () {preorder (root); }/** from the root node of the pre-sequence traversal, recursive method */protected void preorder (treenode<e> root) {if (root = null) return; System.out.print (Root.element + ""); Preorder (Root.left); Preorder (root.right); }/** returns the size of the tree */public int getsize () {return size; }/** returns the root node */public TreeNode Getroot () {return root; /** returns the path from the root node to a specific element */public java.util.arraylist<treenode<e>> path (e e) {Java.util.arraylist<tree node<e>> list = new java.util.arraylist<treenode<e>> ();//Use an array to store elements on the path treenode<e> Curren t = root; Start from the root node while (current! = null) {List.add (present);//Add the element to the array if (E.compareto (current.element) < 0) {current = Current.left; } else if (E.compareto (current.element); 0) {current = Current.right; } else break; } return list; Returns the node array}/** Delete node, delete successfully returns True, does not return false*/public in the tree, Boolean Delete (e) {//mark the deleted node and the node's parent node location Treenode<e> ; parent = NULL; treenode<e> current = root; while (current! = null) {if (E.compareto (current.element) < 0) {parent = current; current = Current.left; } else if (E.compareto (current.element) > 0) {parent = current; current = Current.right; } else break; element in this tree} if (current = = null) return false; The element is not in the tree if (current.left = = null) {//First case: The element has no left subtree, the right subtree of the current node is hung directly on the right subtree of its parent node//The right subtree of the current node is hung directly on the right subtree of its parent node I F (parent = = NULL) {root = Current.right; } else {if (E.compareto (parent.element) < 0) Parent.left = current.right; else parent.right = current.right; }} else {///second case: element has left dial hand tree, first find the right node of the left subtree of the current node Marks the parent and right node of the left subtree of the current node treenode<e> parentofrightmost = present; treenode<e> rightmost = Current.left; Always right, find the right-most node, because the right-most node of the left subtree of a node is also smaller than the leftmost node of the right subtree (rightmost.right! = null) {parentofrightmost = rightmost; rightmost = Rightmost.right; Always to the right}/* * The purpose of the above code is to find the Zuozi right node of the deleted node, because the right node of the left subtree of a node is also smaller than the leftmost node of the right subtree///Find the right node and drop it to the current location to be deleted Current.element = rightmost.element; Eliminate the right-most node if (parentofrightmost.right = = rightmost) Parentofrightmost.right = rightmost.left;//The left child of the right-most node rime at its parent node Right subtree on else//specific case: Parentofrightmost = = Current Parentofrightmost.left = Rightmost.left; } size--; return true; Delete succeeded}/** get the middle order iterator */public java.util.Iterator Iterator () {return inorderiterator (); }/** creates an iterator class */public Java.util.Iterator Inorderiterator () {return new inorderiterator (); }//middle order iterator class, inner class Inorderiterator implements Java.util.Iterator {//SaveAn array of stored elements private java.util.arraylist<e> list = new java.util.arraylist<e> (); private int current = 0; The position of the current element in the array public inorderiterator () {inorder ();//middle-order traversal binary tree}/** from the root of the sequence traversal */private void inorder () { Inorder (root); }/** in-order traversal subtree */private void inorder (treenode<e> root) {if (root = null) return; Inorder (Root.left); List.add (root.element); Inorder (Root.right); }/** traverses the next element */public Boolean hasnext () {if (Current < List.size ()) return true; return false; /** gets the current element and points the pointer to another element */public Object Next () {return list.get (current++); /** Remove the current element */public void Remove () {delete (list.get);//delete the current element list.clear ();//clean Array Inord ER (); Re-sequence Traversal Array}}/** clear all elements of the tree */public void Clear () {root = null; size = 0; }/** inner class, node class of tree */public static class Treenode<e extends Comparable<e>> {E element; TreenoDe<e> left; Treenode<e> right; Public TreeNode (e e) {element = e; } }}Testbinarytree.java
Package Com.hust.cn;public class Testbinarytree {public static void main (string[] args) {//Create a binary lookup tree binarytree& Lt string> tree = new binarytree<string> (); Tree.insert ("George"); Tree.insert ("Michael"); Tree.insert ("Tom"); Tree.insert ("Adam"); Tree.insert ("Jones"); Tree.insert ("Peter"); Tree.insert ("Daniel"); Traverse Tree System.out.println ("inorder (sorted):"); Tree.inorder (); System.out.println ("\npostorder:"); Tree.postorder (); System.out.println ("\npreorder:"); Tree.preorder (); System.out.println ("\nthe number of nodes is" + tree.getsize ()); Find an element of System.out.println ("\nis Peter in the tree" + Tree.search ("Peter")); A path from ROOT to Peter System.out.println ("\na path from the ROOT to Peter is:"); java.util.arraylist<binarytree.treenode<string>> Path = Tree.path ("Peter"); for (int i = 0; Path! = null && i < path.size (); i++) System.out.print (Path.get (i). Element + " "); Construct a binary lookup tree using an array, and the middle sequence traverse integer[] numbers = {2, 4, 3, 1, 8, 5, 6, 7}; binarytree<integer> inttree = new binarytree<integer> (numbers); System.out.println ("\ninorder (sorted):"); Inttree.inorder (); }}
Test results:
Data structure-two fork tree and two fork search tree