Binary Tree Basics:
Just see the heap sort, and by the way record some basic concepts about trees:
Objective
The stacks and queues described earlier are linear structures (linear structure). And the tree is a nonlinear structure (non-linear structure). As a result, there is generally no one-to-one relationship between the elements in the tree that resembles a linear structure, and is more represented as a many-to-many relationship. Visually, it is the structure of data elements (called nodes in a tree) organized by branching relationships. Clearly, a tree structure is a more complex type of data structure than a linear structure.
First, the tree
Tree definition: A tree is a rich set of N nodes, with a node that is more specifically called the root node. In the diagram tree, connect two logically related nodes with one edge, which is called a parent-child relationship.
Two or two fork tree
Binary trees (binary tree) consist of a finite set of nodes. This collection is either an empty set, or a two-Zuozi tree consisting of a root node (root) and two disjoint, each of which is called the root (left subtree) and the right subtree (r subtree).
A tree of two forks:
Three, the main difference between tree and two-fork tree
- There is no limit to the maximum number of nodes in a tree, and the degree of a binary tree node does not exceed 2.
- Nodes in the tree node, there is no left and right points, and the binary tree is differentiated, that is, children are different: Left children, children, and the order can not be reversed.
- The number of nodes in a tree is at least 1, and the number of nodes in a binary tree can be 0.
Iv. Common Concepts
- Degree of node: the degree of a node is defined as the number of child nodes of the node.
- Leaf node: A node with a degree of 0.
- Degree of the tree: the degree of the largest node in a tree is called the degree of the tree.
- The height of the node: the number of sides of the longest simple path from the node to the leaf node. (Simple path: a path with no repeating edges)
- Height of the tree: the height of the root node.
- Number of layers of a node: Starting from the root, the root is the 1th layer, the child node of the root is the 2nd layer, and so on.
- Number of layers: the number of layers of the root node.
- The depth of the node: the number of layers of the node.
- The depth of the tree: the depth of the root node.
- Outer node: leaf node.
- Inner node: a node other than a leaf node.
- Full two fork tree: Two The node in the tree can only be 0 or 2.
- Complete binary tree: In addition to the last layer, each layer has reached the maximum number of nodes. If the last layer is not full, then the node is centered on the left and the empty one is to the right.
- Expand the binary tree: add empty nodes to the nodes and leaf nodes with a two-tree medium degree of 1, making it a full two-fork tree.
Note: The definition of tree depth, number of layers, height is different: There are counting starting from 0, and also counting starting from 1. From where to start counting is not the key, the key is consistency, the amount of the wording to be consistent. Five, several quantities of the relationship
- For a binary tree, the root node is the first layer, and the layer I has at most one node. If there is a K-tier, there are at most nodes.
- All nodes of a complete binary tree with n nodes are numbered from 0 to n-1 in a hierarchical order. If the parent node number is I, the left child's number is 2*i+1, and the right child's number is 2*i+2. (This is, of course, in the presence of children in the left and right circumstances.) Similarly, if the child (regardless of the right and left child) node is I, then the parent node is (i-1)/2.
- For a tree full of two forks, the external node or the leaf node number is n, then the number of internal nodes is n-1.
- For a binary tree, the number of nodes with NI representing the degree of I is n0=n2+1. The proof is as follows: Total number of nodes n=n0+n1+n2. Use e to indicate the number of sides, then n=e+1, because each node is corresponding to an edge outside the root node. At the same time, E=2N2+N1, the introduction of n0+n1+n2=2n2+n1+1, simplifying the n0=n2+1. This conclusion is expressed in language: Two tree, the leaf node ratio of 2 nodes more than one.
- There are n nodes of the complete binary tree, the tree height, that is, the logn downward rounding, the height from 0 counts.
- In a binary tree, the first node of layer I (the leftmost node) is numbered, and the number of layers is counted from 0. This amount is used in the Select sort: Tree Selection.
Six
Binary Tree Traversal:
in Computer science, a binary tree is a tree structure that has a maximum of two subtrees per node. The subtree is usually referred to as the " Left Dial Hand tree" (leftsubtree) and the "right subtree," (Rsubtree).
A simple two-fork tree
The traversal of the binary tree is divided into three kinds: the pre (first) order, the middle sequence and the post-order traversal.
Set L, D, R respectively to indicate the Zuozi of the binary tree, the root node and traverse the right subtree, the first (root) sequence traversal binary tree sequence is the DLR, in (root) sequence traversal binary tree sequence is Ldr, after (root) sequence traversal binary tree sequence is lrd. and traversing a binary tree by layer. The time complexity of these methods is O (n) and N is the number of nodes.
Let's say we have a value that contains values and a tree node structure that points to the left and right of the two child nodes. We can write a process like this :
First-order traversal (recursive implementation):
Visit (node)
Print Node.value
If node.left! = NULL then visit (node.left)
If node.right! = NULL then visit (node.right)
Middle-order traversal (recursive implementation):
Visit (node)
If node.left! = NULL then visit (node.left)
Print Node.value
If node.right! = NULL then visit (node.right)
Post-post traversal (recursive implementation):
Visit (node)
If node.left! = NULL then visit (node.left)
If node.right! = NULL then visit (node.right)
Print Node.value
Binary tree creation and traversal:
public class BinaryTree {private Node root; /** * * Internal node class * @author YHH */private class node{private node left; Private Node right; private int data; public Node (int data) {this.left = null; This.right = null; This.data = data; }} public BinaryTree () {root = null; }/** * Recursive creation of two tree * @param node * @param data */public void Buildtree (node node,int dat A) {if (root = null) {root = new Node (data); }else{if (Data < Node.data) {if (Node.left = = null) {Node.left = new Node (data); }else{Buildtree (Node.left,data); }}else{if (node.right = = null) {Node.right = new node (data); }else{ Buildtree (Node.right,data); }}}}/** * pre-order Traversal * @param node */public void Preorder (node no DE) {if (node! = null) {System.out.println (node.data); Preorder (Node.left); Preorder (node.right); }}/** * middle sequence Traversal * @param node */public void Inorder (node node) {if (node! = Nu ll) {inorder (node.left); System.out.println (Node.data); Inorder (Node.right); }}/** * post-post Traversal * @param node */public void Postorder (node node) {if (node! = NULL) {postorder (node.left); Postorder (Node.right); System.out.println (Node.data); }} public static void Main (string[] args) {int[] a = {2,4,12,45,21,6,111}; BinaryTree bTree = new BinaryTree (); for (int i = 0;i < a.length; i++) {Btree.buildtree (Btree.root, a[i]); } btree.preorder (Btree.root); Btree.inorder (Btree.root); Btree.postorder (Btree.root); } }
Insert operation
Binary tree Find tree b the procedure for inserting an operation x is as follows:
1, if B is an empty tree, the inserted node is inserted directly as the root node.
2, x equals the value of the data of the root node of B, then returns directly, otherwise.
3. If x is less than the value of the data of the root node of B, change the position of the node where x is to be inserted to the left subtree of B, otherwise.
4. Change the position of the node where x is going to go to the right sub-tree of B.
Specific code:
public void Insert (T t) { roottree = insert (t, roottree); } Public Binarynode Insert (T t,binarynode node) { if (node==null) { ///New construct a binary lookup tree return new Binarynode (t, NULL, NULL); } int result = T.compareto (node.data); if (result<</span>0) node.left= Insert (t,node.left); else if (result>0) node.right= Insert (t,node.right); else ;//donothing return node; }
Delete operation:
For a binary lookup tree delete operation (here based on the value of the deletion, rather than the node) in three cases:
But before we do that, we should make sure we find the node that we want to delete based on the given value, and if we don't find that node.
The delete operation is not performed!
The following three scenarios assume that you have found the node you want to delete.
1, if the node is a leaf node (there is no left and right subtree), at this point delete the node will not be the structure of the glassy tree
Delete directly, and modify the reference to which the parent node points to it null. For example:
2, if its node contains only the left subtree, or the right subtree, then directly delete the node, and its left sub-tree
Or the right subtree is set to the left or right subtree of its parent node, and this operation does not break the tree structure.
3, when the nodes of the left and right sub-tree are not empty, the general deletion strategy is to use the minimum data of the subtree (easy to find) to delete the node data and recursively delete the node (this is null), because the right subtree of the smallest node can not have left children, so the second deletion is easier. The Saozi right subtree of z is not empty. The successor Y of Z is found, because Y must have no left subtree, so you can delete y and let Y's father node become the parent node of Y's right subtree, substituting the value of Y for Z.
Specific code:
public void Remove (T t) { Roottree = remove (T,roottree); } Public Binarynode Remove (T t,binarynode node) { if (node = = null) return node;//not found, doNothing int result = T.compareto (node.data); if (result>0) node.right = Remove (t,node.right); else if (result<</span>0) node.left = Remove (t,node.left); else if (node.left!=null&&node.right!=null) { node.data = findmin (node.right). data; Node.right = Remove (node.data,node.right); } else node = (node.left!=null)? node.left:node.right; return node; }
To find a binary tree node:
The process of finding x in the binary lookup tree is as follows:
1, if the binary tree is empty tree, then the search failed.
2, if x equals the root node of the data, then find success, otherwise.
3, if X small root node data, then recursively find its left subtree, otherwise.
4, recursively find its right subtree.
Specific code:
Public TreeNode Search (int Key) { TreeNode node = root; First define a node to point to the root, in the following loop //As long as the node value is not equal to the value of the node to be looked up into the loop if not found then return null while (node.keyvalue! = key) {If (key < Node.keyvalue) {//If the value to be found is less than the node value then point to the left node = Node.leftnode; } else {//otherwise point to right node = Node.rightnode; } If (node = = null) {//If the node is empty then return null return null;} } return node; }
minimum and maximum values:
Idea: The maximum always goes to the right, the minimum is always left.
Specific code:
public int Max () { TreeNode node = root; TreeNode parent = null; while (node! = NULL) { parent = node; node = Node.rightnode; } return parent.keyvalue; } public int min () { TreeNode node = root; TreeNode parent = null; while (node! = NULL) { parent = node; node = Node.leftnode; } return parent.keyvalue; }
depth of tree:
Specific code:
int length (Node root) { int depth1; int depth2; if (root = null) return 0; Zuozi depth depth1 = Length (root.right); The depth of the right subtree is depth2 = length (root.left); if (depth1>depth2) return depth1+1; else return depth2+1;}
Reference Links:
Http://blog.sina.com.cn/s/blog_401823210101f8t6.html
Http://blog.sina.com.cn/s/blog_937cbcc10101dmqm.html
Http://www.cnblogs.com/wuchanming/p/4067951.html
http://blog.csdn.net/fansongy/article/details/6798278
Http://www.cricode.com/3489.html
Http://www.cricode.com/3212.html
http://blog.csdn.net/sjf0115/article/details/8645991
Http://www.cnblogs.com/vamei/archive/2013/03/17/2962290.html
http://blog.csdn.net/sysu_arui/article/details/7865876
http://blog.163.com/xiaopengyan_109/blog/static/14983217320108168618624/
The Foundation and application of binary tree