[] Binary tree data structure basic content of the discussion (a)

First, the binary tree is defined: degrees (maximum value of all the sub-tree each node) is a number of 2.

Second, the nature of binary tree:

1. Binary Tree on the i-th layer up to 2 ^ (i - 1) th node;

2. The depth of k binary tree up to 2 ^ k - 1 th node;

  2-1 and has a depth of k 2 ^ k - 1 nodes of the binary tree is called a full binary tree ;

  2-2 each of the nodes of the full binary tree from the top down, left to right on the edited number (1 ~ 2 ^ k - 1 ). A depth of k, there are n nodes in the binary tree if and only if each of which nodes are associated with a full binary tree of depth k-one correspondence (i.e., only the last layer penultimate node degree is not 2 , only the last one with respect to the full binary tree node missing, and the last one is the deletion node from the rightmost to left continuous), the tree is called a complete binary tree ;

3. For an arbitrary binary tree, which leaves a certain ratio of the number of nodes of the plurality of nodes 2 1. Namely: n- 0 = n- 2 +. 1 ;

4 has an n-vertex complete binary tree of depth Floor (log 2 n) +. 1 (Floor directed downward rounding);

5. For a n-node binary tree, for any node (number I), are:

  5-1. If i = 1, then the node is the root node, the parent node number or floor (i / 2) (in the program can be written directly to i / 2);

  . 5-2 if 2 * i> n, then this node has no children, is the root; if 2 * i == n, this is only the left child node, its number is 2 * i; otherwise, this node has left child and right child, were No. 2 * i and 2 * i + 1.

Third, the binary tree Storage: Storage Structure:

. 1  struct Node
 2  {
 . 3       the DATA_TYPE Data; // data 
. 4       Node * leftChild, rightChild; // left child and right child 
. 5  };
 . 6 Node * BT; // root

IV: binary tree traversal (each in the binary tree traversal operation is not empty)

1. preorder: (1) access to the root node; (2) preorder left subtree; (3) preorder right subtree (Root -> Left -> right)

2. traversal sequence: (1) left subtree traversal sequence; (2) access to the root node; (3) preorder right subtree (left -> Root -> right)

3. postorder: (1) postorder left subtree; (2) the right subtree preorder; (3) access to the root node (left -> Right -> Root)

void pre (Node * BT) // preorder 
{
     IF (BT) 
    { 
        COUT << BT-> Data; 
        pre (BT -> leftChild); 
        pre (BT -> rightChild); 
    } 
} 
void  in (Node * BT ) // traversal sequence 
{
     IF (BT) 
    { 
        in (BT-> leftChild); 
        COUT << BT-> Data;
         in (BT-> rightChild); 
    } 
} 
void pOST (Node * BT) // postorder traversal 
{
    if(bt)
    {
        post(bt->leftChild);
        post(bt->rightChild);
        cout<<bt->data;
    }
}

 

Guess you like

Origin www.cnblogs.com/jiangyuechen/p/12622488.html