Part 1-- tree data structure of binary tree

nature

The basic nature: binary tree _ Baidu Encyclopedia

Binary tree on the depth (height) related to the number of layers of nodes, some of the materials specified root \ (0 \) layer, and some first predetermined root \ (1 \) layer.

All herein to "language data structure -C Edition" (Yan Wei Min, Wu Weimin Edition) prevail: height and depth of the same concept, the root node in the first layer 1 , is to look at the height of the tree, a total of several layers. Height \ (H \) a complete binary tree, up to \ (2 ^ {n} -1 \) nodes, the \ (K \) layer up to \ (2 ^ {k-1 } \) nodes. Comprising \ (n-\) complete binary tree nodes, height \ (log (n) + 1 \) rounding the next.

Complete Nimata 树:

If labeled under the first element of \ (1 \) , then the first \ (k \) left child element of \ (2 * k \) , right child is \ (2 * k + 1 \) , parent is (k / 2 \) \ the rounding of the last node is a parent \ (length / 2 \) rounded lower.

If the standard under the first element is \ (0 \) , then the first \ (k \) left child element of \ (2 * k + 1 \) , right child is \ (2 * k + 2 \) , parent node is \ ((k - 1) / 2 \) under rounding, a parent node is the last \ (length / 2 \) after rounding at minus one.

Predecessor successor node of a binary tree traversal sequence represents the time, a rear and a front of a node.

Height \ (H \) a complete binary tree, up to \ (2 ^ {h} -1 \) nodes.

Node is \ (n-\) a complete binary tree, a height \ (log (n-) \) .

Binary tree traversal

There are three ways binary tree traversal, in order traversal after the first, "before, during and after" indicates traversal order of the root node .

First order: first traversal root of that branch, then traverse left subtree, finally traversing the right subtree.

Preorder sequence: \ (ABDFECGHI \)

img

In order: first traverse the left subtree branch, and then traverse the root node, and finally traversing the right subtree.

In order sequence: \ (DBEFAGHCI \)

img

After the order: first traverse the left subtree branch, and then traverse the right subtree, and finally traversed the root node.

Sequence after the sequence: \ (DEFBHGICA \)

img

Binary tree traversal implementation

Postorder non-recursive, it borrowed two stacks A, B, with A to access the stack, left and right push order (first order of left, right), and then to deposit these nodes B to the stack, and finally the stack together.

//先序
void preorderUnRecur(BTree T) {
    stack<BTree>s;
    s.push(T);
    while (!s.empty()) {
        T = s.top();
        s.pop();
        cout << T->data << " ";
        if (T->Right)
            s.push(T->Right);
        if (T->Left)
            s.push(T->Left);
    }
    cout << endl;
}

//中序
void inorderUnRecur(BTree T) {
    if (!T)return;
    stack<BTree>s;
        while (!s.empty() || T) {
            if (T) {
                s.push(T);
                T = T->Left;
            }
            else {
                T = s.top(); s.pop();
                cout << T->data << " ";
                T = T->Right;
            }
        }
    cout << endl;
}

//后序

void postorderUnRecur(BTree T) {
    if (!T)return;
    stack<BTree>s1,s2;
    s1.push(T);
        while (!s1.empty()) {
            T = s1.top(); s1.pop();
            s2.push(T);
            if (T->Left)s1.push(T->Left);
            if (T->Right)s1.push(T->Right);
    }
        while (!s2.empty()) {
            cout << s2.top()->data << " ";
            s2.pop();
        }
    cout << endl;
}

Recursive, compared to it is relatively simple:

void Inorder(BTree t) {
    if (!t)return;
    Inorder(t->Left);
    cout << t->data << " ";
    Inorder(t->Right);
}
void Preorder(BTree t) {
    if (!t)return;
    cout << t->data << " ";
    Preorder(t->Left);
    Preorder(t->Right);
}
void Postorder(BTree t) {
    if (!t)return;
    Postorder(t->Left);
    Postorder(t->Right);
    cout << t->data << " ";
}

Construction of Binary Tree

  Binary tree establishment are generally two ways:

(1) an array manner, or an input according to order of nodes, with a special value (such as 0, #) indicates that the current position is not the node. But this will always be conflicts, such as the value of a node is 0, # ASCII value. I do not write.

(2) two sequences comprise sequences given sequence. The following is a given in the preamble, the time sequence in order to create a binary tree.

In sequence by sequence contained (only before and after the sequence to create the binary tree is not unique) two sequences can be introduced only binary tree.

typedef struct BTNode
{
    int data;
    struct BTNode* Left;
    struct BTNode* Right;
}*BTree;

void PreAndInToPost(int* pre, int* in, int length) {
    if (length == 0) return;
    int rootIndex;
    BTree BT = new BTNode;
    BT->data = *pre;
    for (rootIndex = 0; in[rootIndex] != *pre; rootIndex++);
    PreAndInToPost(pre + 1, in, rootIndex);
    PreAndInToPost(pre + rootIndex + 1, in + rootIndex + 1, length - (rootIndex + 1));
    cout << BT->data << " ";
}

void PostAndInToPre(int* in, int* post, int length) {
    if (length == 0) return;
    int rootIndex;
    BTree BT = new BTNode;
    BT->data = *(post + length - 1);
    for (rootIndex = length - 1; in[rootIndex] != *(post + length - 1); rootIndex--);
    cout << BT->data << " ";
    PostAndInToPre(in, post, rootIndex);
    PostAndInToPre(in + rootIndex + 1, post + rootIndex, length - (rootIndex + 1));
}

Complete test code as follows:

#include <iostream>
using namespace std;

typedef char ElemType;

typedef struct BTNode
{
    ElemType data;
    struct BTNode* Left;
    struct BTNode* Right;
}*BTree;

BTree creat_tree_from_pre_inorder(ElemType* pre, ElemType* in, int length) {
    if (length == 0) return NULL;
    int rootIndex;
    BTree BT = new BTNode;
    //将当前根节点入树
    BT->data = *pre;
    for (rootIndex = 0; in[rootIndex] != *pre; rootIndex++);
    BT->Left = creat_tree_from_pre_inorder(pre + 1, in, rootIndex);
    BT->Right = creat_tree_from_pre_inorder(pre + rootIndex + 1, in + rootIndex + 1, length - (rootIndex + 1));
    return BT;
}
BTree creat_tree_from_post_inorder(ElemType* in, ElemType* post, int length) {
    if (length == 0) return NULL;
    int rootIndex;
    BTree BT = new BTNode;
    //将当前根节点入树
    BT->data = *(post + length - 1);
    for (rootIndex = length - 1; in[rootIndex] != *(post + length - 1); rootIndex--);
    BT->Left = creat_tree_from_post_inorder(in, post, rootIndex);
    BT->Right = creat_tree_from_post_inorder(in + rootIndex + 1, post + rootIndex/*删掉post的最后一个*/, length - (rootIndex + 1));

    return BT;
}


void Inorder(BTree BT) {
    if (!BT)return;
    Inorder(BT->Left);
    cout << BT->data << " ";
    Inorder(BT->Right);
}
void Preorder(BTree BT) {
    if (!BT)return;
    cout << BT->data << " ";
    Preorder(BT->Left);
    Preorder(BT->Right);
}
void Postorder(BTree BT) {
    if (!BT)return;
    Postorder(BT->Left);
    Postorder(BT->Right);
    cout << BT->data << " ";
}

int main() {
    char pre[] = "ABDGHCEIF";
    char in[] = "GDHBAEICF";
    char post[] = "GHDBIEFCA";
    BTree T1 = creat_tree_from_pre_inorder(pre, in, strlen(in));
    BTree T2 = creat_tree_from_post_inorder(in, post, strlen(in));
    Postorder(T1);
    cout << endl;
    Preorder(T1);
    cout << endl;
    return 0;
}

Related small algorithm

Get the depth of a binary tree

int height(BTree T) {
    if (T == NULL)
        return 0;
    int left = height(T->Left);
    int right = height(T->Right);
    return left >= right ? left + 1 : right + 1;
}

Guess you like

Origin www.cnblogs.com/czc1999/p/11768769.html