[Common data structures - binary tree (as well as trigeminal quad ...?)]

--- --- restore content begins

Brief introduction

  In computer science, a binary tree every node has at most two subtrees ordered tree. Typically the sub-root of the tree is called "left subtree" (left subtree), and "right subtree" (right subtree). Binary tree is often used as a binary search tree and a binary heap or binary sort tree.

Basic terms 

  

  1. Subtree : In addition to the root node, each child node can be divided into a plurality of disjoint sub-trees.
  2. Children and parents : if a node has sub-tree, then the root node called the child's "parents", sub-tree root node is the "child."
  3. Brothers : nodes with the same parent each other brothers.
  4. Degree nodes : has a number of sub-tree node.
  5. Leaves : no sub-tree, that is a node of degree zero.
  6. A branch node : in addition to the leaf node other than the node, i.e. the node is not 0 degrees.
  7. Internal node : a branch node except the root node.
  8. Level : the first layer is the root node, the node remaining level equals the level of its parent node plus one.
  9. Height of the tree : maximum depth level also known as tree nodes in the tree.
  10. Ordered tree : the sub-tree of order between the nodes in the tree is important, not freely exchange position.
  11. Unordered tree : order between the sub-species tree node is not important. Free to exchange places.
  12. Forests : a collection of disjoint trees 0 or trees.

The basic shape

 

Also five kinds, from left to right are empty tree, only the root of the tree, the root node and left his son, the root node and right child, root and about children.

Basic properties

1. The number of nodes in the binary i-th layer at most 2 i-. 1 (i> =. 1)

2. The depth of the binary tree has at most k 2 k -1 nodes (k> = 1)

3. The height of the binary tree of n nodes comprising at least (log 2 n) + 1'd

4. In any of the binary tree, when the number is 0 to n- 0 , the number of nodes of degree 2 is n- 2 , the n- 0 = n- 2 + 1'd

5. If a node complete binary tree of n nodes according to the sequence number (beginning from the first layer to the next layer, each layer numbered from left to right), for any of a node i has:

  1. If i = 1, then the node is the root node, no parents.
  2. If 2 * i> n, then i have not left child node; otherwise its left child node 2 * i (n is the total number of nodes)
  3. If 2 * i + 1> n, there is no right child node i; otherwise the right child node is 2 1 + 1 *

Full Binary Tree

  As its name suggests, it is full of (crap)

  Full Binary refers to a highly 2 (n-1) th node ^ n of the tree (in fact, each point has two children, a single dog in addition to the last level)

 

Full Nimata 树

  Refers to a complete binary tree is a binary tree leaf nodes only in the last two, and the last layer of leaf nodes are concentrated on the left. Clearly, the full binary tree is a complete binary tree.

 

--- end --- restore content

Brief introduction

  In computer science, a binary tree every node has at most two subtrees ordered tree. Typically the sub-root of the tree is called "left subtree" (left subtree), and "right subtree" (right subtree). Binary tree is often used as a binary search tree and a binary heap or binary sort tree.

Basic terms 

  

  1. Subtree : In addition to the root node, each child node can be divided into a plurality of disjoint sub-trees.
  2. Children and parents : if a node has sub-tree, then the root node called the child's "parents", sub-tree root node is the "child."
  3. Brothers : nodes with the same parent each other brothers.
  4. Degree nodes : has a number of sub-tree node.
  5. Leaves : no sub-tree, that is a node of degree zero.
  6. A branch node : in addition to the leaf node other than the node, i.e. the node is not 0 degrees.
  7. Internal node : a branch node except the root node.
  8. Level : the first layer is the root node, the node remaining level equals the level of its parent node plus one.
  9. Height of the tree : maximum depth level also known as tree nodes in the tree.
  10. Ordered tree : the sub-tree of order between the nodes in the tree is important, not freely exchange position.
  11. Unordered tree : order between the sub-species tree node is not important. Free to exchange places.
  12. Forests : a collection of disjoint trees 0 or trees.

The basic shape

 

Also five kinds, from left to right are empty tree, only the root of the tree, the root node and left his son, the root node and right child, root and about children.

Basic properties

1. The number of nodes in the binary i-th layer at most 2 i-. 1 (i> =. 1)

2. The depth of the binary tree has at most k 2 k -1 nodes (k> = 1)

3. The height of the binary tree of n nodes comprising at least (log 2 n) + 1'd

4. In any of the binary tree, when the number is 0 to n- 0 , the number of nodes of degree 2 is n- 2 , the n- 0 = n- 2 + 1'd

5. If a node complete binary tree of n nodes according to the sequence number (beginning from the first layer to the next layer, each layer numbered from left to right), for any of a node i has:

  1. If i = 1, then the node is the root node, no parents.
  2. If 2 * i> n, then i have not left child node; otherwise its left child node 2 * i (n is the total number of nodes)
  3. If 2 * i + 1> n, there is no right child node i; otherwise the right child node is 2 1 + 1 *

Full Binary Tree

  As its name suggests, it is full of (crap)

  Full Binary refers to a highly 2 (n-1) th node ^ n of the tree (in fact, each point has two children, a single dog in addition to the last level)

 

Full Nimata 树

  Refers to a complete binary tree is a binary tree leaf nodes only in the last two, and the last layer of leaf nodes are concentrated on the left. Clearly, the full binary tree is a complete binary tree.

Binary tree traversal

  For the binary tree traversal, we divided into three cases:

First, the preorder traversal the root -> left sub-tree -> right subtree

The following is the order of 124 583 796

1 void work(int x)
2 {
3     if(!x)return;//如果这个点有效 
4     printf("%d ",x);//
5     work(l[x]);//左子树 
6     work(r[x]);//右子树 
7 }

 

 

一,中序遍历       左子树->根结点->右子树

以下顺序就是428519736

 

1 void work(int x)
2 {
3     if(!x)return;//如果这个点有效 
4     work(l[x]);//左子树 
5     printf("%d ",x);//
6     work(r[x]);//右子树 
7 }

 

一,后序遍历      右子树 ->左子树->根结点

以下顺序就是485297631

 

void work(int x)
{
    if(!x)return;//如果这个点有效 
    work(r[x]);//右子树 
    work(l[x]);//左子树 
    printf("%d ",x);//
}        

 然而它们之间有神奇的关系

中、后序求先序

P1030 求先序排列

 

图中粉色的表示这一个序分成了根,左,右三部分(也可能没有左或右)

首先,你要知道,后序的最后一个肯定是根结点,然后你要知道,在一串中序中,找到了它的根结点,那么它左边就是左子树,右边就是右子树。

我们先找到根结点A,然后把中序分为FDBE和CHG左右子树,而左子树的后序就是FDEB,右子树的后序就是HGC,然后一步一步分解下去,知道不能分解,因为我们求先序,所以先输出根结点,然后分别递归左、右子树。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 void beford(string s2,string s3)//中序and后序 
 4 {
 5     if(s2.size()==0)return;
 6     char root=s3[s3.size()-1];//找到根结点 
 7     cout<<root; //输出根结点 
 8     int k=s2.find(root);
 9     beford(s2.substr(0,k),s3.substr(0,k));//左子树 
10     beford(s2.substr(k+1),s3.substr(k,s2.size()-k-1));//右子树 
11 }
12 int main()
13 {
14     string s2,s3;//1,2,3分别对应前,中,后 
15     cin>>s2>>s3; 
16     beford(s2,s3);
17  } 

中、先序求后序

与上面同理,只需要稍微改动一点即可

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 void beford(string s1,string s2)//先序and中序 
 4 {
 5     if(s1.size()==0)return;
 6     char root=s1[0];//找到根结点 
 7     int k=s2.find(root);
 8     beford(s1.substr(1,k),s2.substr(0,k));//左子树 
 9     beford(s1.substr(k+1),s2.substr(k+1));//右子树 
10     cout<<root; //输出根结点 
11 }
12 int main()
13 {
14     string s1,s2;//1,2,3分别对应前,中,后 
15     cin>>s1>>s2; 
16     beford(s1,s2);
17 } 

 

Guess you like

Origin www.cnblogs.com/hualian/p/11344907.html