の binary storage structure

 

 

 Sequentially storing binary: the binary tree complete binary tree for future expansion in order to traverse the level number stored in the linear form

(If you do not expand direct deposit, you can not express the logical relationship between father and son binary tree)


Binary chain store: parent-child relationship is reflected by a pointer to the node.

It may be two fields: a data field data, pointer field lchild, rchild. // three domains may be added subsequent parent domain

typedef struct BiTNode {
    char data;
    struct BiTNode* lchild;
    struct BiTNode* rchild;
    BiTNode() {
        lchild = NULL;
        rchild = NULL;
    }
}BiTNode, * BiTree;

Incidentally attach build binary code

void BinaryTree :: createTree (BiTree & Node) {
     char CH; 
    CIN >> CH;
     IF (CH == ' # ' {) 
        Node = NULL; 
    } 
    the else { 
        Node = new new BiTNode; 
        Node -> Data = CH; 
        createTree (Node -> lchild); 
        createTree (node -> rchild); 
    } 
} // reconstruction according to the preamble binary tree traversal sequence "#" as an empty node

 

Note: the preamble sequence traversed / + subsequent sequence can be determined uniquely in a binary tree, the code attached below:

struct TreeNode* buildtree(char* inorder, char* postorder, int len) {
    if (len == 0) {
        return 0;
    }
    struct TreeNode* T = (struct TreeNode*)malloc(sizeof(TreeNode));
    T->val = postorder[len - 1];
    //    cout<<T->val;
        //在中根序列中找根节点
    int i = 0;
    while (postorder[len - 1] != inorder[i]&&i<len) {
        I++ ; 
    } // I final root index in the root sequence 
    IF (I == len) {COUT << " INVALID " ; Exit ( 0 );} 
    T -> left = BuildTree (InOrder, postorder, I); // left a 
    T-> right = BuildTree (InOrder + I + . 1 , I + postorder, len - I - . 1 ); // I is the index 
    return T; 
} // root + root traversed after reconstruction binary Tree

Today, more.

Guess you like

Origin www.cnblogs.com/yoriko/p/12193690.html