Two-fork Tree

Source: Internet
Author: User
Tags in degrees

A tree is a very important data structure, and a binary tree is the most basic form of a tree. Like other advanced data structures, such as binary search tree, balanced binary tree, AVL tree, red black tree, splay tree (stretching tree), flute Karlshu, treap, SBT tree are all based on two fork trees.

Properties
  1. Each node in the tree has 0-2 child nodes, and each child node becomes the root of a subtrees tree.
  2. The number of edges in the tree is the number of nodes-1, which is E = n-1
  3. The number of nodes in the tree is N0, N1, N2, N0 + n1 + n2 = N, n1 + 2*n2 = e. You can launch N2-N1 = 1, which means that the number of points with degrees 2 is more than 1 of the number of points in degrees 1.0,1,2.
  4. The nth layer (the root is the No. 0 layer, and then down is 1th, 2, 3 ... The maximum number of midpoints in a layer is 2n
  5. n nodes with a minimum number of two-fork-tree layers of ⌊log2n⌋ + 1
Special two-fork tree
  • 1 Complete binary Tree
    If the number of layers of the binary tree is h, and the nodes from layer No. 0 to h-2 are full ( 20 、21 ...2^ (h−2) ), the number of nodes in the h-1 layer is >0, and the nodes of the h-1 layer are in the left position (if the position of the last layer of the two-fork tree can be placed from left to right numbered 1, 2 ... 2^ (h−1), the last layer has K nodes, then this K node position is 1, 2,3...K). Such a two-fork tree is called a complete binary tree.
  • 2 Full two fork tree
    If the binary tree is an H layer, and the nodes from the No. 0 to the h-1 layer are full ( 20 、21 ... 2^ ( h− 1) ), that is, the total number of nodes is  2^h−1 . This binary tree is called a full two-fork tree, which can be seen as a special case of a complete binary tree with a full two fork tree.
Two implementation of a fork tree (c + +)
#include <iostream>#include<stack>using namespacestd;structtreenode{intdata; TreeNode*Left ; TreeNode*Right ; TreeNode*parent; TreeNode (intd): Data (d), left (null), Right (NULL), parent (null) {};};voidVisit (treenode*node) {cout<<"visit node, data ="<< Node->data <<Endl;}//Pre-sequence traversal of a tree, non-recursivevoidPreordertravel_stack (treenode*root) {TreeNode* node =Root; Stack<TreeNode*>Node_stack;  while(!node_stack.empty () | |node) {        if(node) {Visit (node);            Node_stack.push (node); Node= node->Left ; }        Else{node=Node_stack.top ();            Node_stack.pop (); Node= node->Right ; }    }}//Middle sequence traversal of a tree, non-recursivevoidInordertravel_stack (treenode*root) {TreeNode* node =Root; Stack<TreeNode*>Node_stack;  while(!node_stack.empty () | |node) {        if(node) {Node_stack.push (node); Node= node->Left ; }        Else{node=Node_stack.top ();            Node_stack.pop ();            Visit (node); Node= node->Right ; }    }}//Post -sequential traversal of a tree, non-recursivevoidPostordertravel_stack (treenode*root) {Stack<pair<treenode*,BOOL> > Node_stack;//BOOL represents whether the node is returned from Zuozi (true) or returned from the right subtree when the node is popped from the stack (false)pair<treenode*,BOOL> Node_tag (Root,true); TreeNode* node =Root;  while(true){        if(node) {//Node nodes and their left child nodes are definitely not accessedNode_tag.first =node; Node_tag.second=true;//go to the left subtree, then out of the stack, indicating that the node returned from ZuoziNode_stack.push (Node_tag); } Node_tag=Node_stack.top ();        Node_stack.pop (); Node=Node_tag.first;  while(Node_tag.second = =false){//Now that node is returned from the right subtree, you can access the node nodesVisit (node); if(Node_stack.empty ()) {//indicates that all nodes have been accessed.                return; }            Else{Node_tag=Node_stack.top ();                Node_stack.pop (); Node=Node_tag.first; }        }        //the left child of node has been accessed, and the right subtree has not started to be accessedNode_tag.second =false;//into the right subtree, when it is out of the stack, indicating the return from the right subtreeNode_tag.first =node;        Node_stack.push (Node_tag); //go to the right subtree to traversenode = node->Right ; }}//Pre-sequence traversal of a tree (recursive)voidPreordertravel_recursive (treenode*root) {    if(Root) {Visit (root); Preordertravel_recursive (Root-Left ); Preordertravel_recursive (Root-Right ); }}//Middle sequence traversal of a tree (recursive)voidInordertravel_recursive (treenode*root) {    if(Root) {inordertravel_recursive (root-Left );        Visit (root); Inordertravel_recursive (Root-Right ); }}//Sequential traversal of a tree (recursive)voidPostordertravel_recursive (treenode*root) {    if(Root) {postordertravel_recursive (root-Left ); Postordertravel_recursive (Root-Right );    Visit (root); }}

Two-fork Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.