Algorithm learning-clue Binary Tree

Source: Internet
Author: User

Algorithm learning-clue Binary Tree
Clue Binary Tree

A clue Binary Tree adds something more to a common binary tree. What is more? A chain structure is a binary tree. Explanation: Generally, in our binary tree, leaf nodes do not have children, so pointing to null meansNULLIn the clue binary tree, the left and right children of the leaf node point to their own pioneers and successors respectively. Which node is the frontend and successor? It is the previous node and the next node in the tree traversal process. Therefore, the first traversal node has no precursor, And the last node has no successor. Generally, this is a binary tree of the central direction. Of course, there are also Binary Trees of the first direction and the binary tree of the latter direction.

            []a[]            /   \         []b[] []c[]         /   \         []d[] []e[]

The above is a binary tree. I have added a label on both sides of each node.[]I haven't added any content to this label yet. There are only two values in it. One is0One is1When a node has children0When there are no children1.
Therefore, the structure of the node is:

typedef struct TreeNode* node;struct TreeNode{    node lchild;    int ltag;    int rtag;    node rchild;    int data;};


When0Point to the child node1Point to the front or back.
The code is attached below.

Code Implementation

The Code is as follows:

node Pre = NULL;void InOrderTree(node n){    if (n == NULL) {    }else{        //        Pre = n;        if (n->lchild != NULL) {            n->ltag = 0;            InOrderTree(n->lchild);        }else{            n->ltag = 1;            n->lchild = Pre;        }        if (Pre!=NULL && Pre->rchild == NULL) {            Pre->rtag = 1;            Pre->rchild = n;        }        Pre = n;        if (n->rchild != NULL) {            n->rtag = 0;            InOrderTree(n->rchild);        }else{            n->rtag = 1;        }    }}

Test code (main ):

int main(int argc, const char * argv[]) {    node head = (node)malloc(sizeof(struct TreeNode));    head->data = 1;    node node1 = (node)malloc(sizeof(struct TreeNode));    node node2 = (node)malloc(sizeof(struct TreeNode));    node node3 = (node)malloc(sizeof(struct TreeNode));    node node4 = (node)malloc(sizeof(struct TreeNode));    node1->data = 2;    node2->data = 3;    node3->data = 4;    node4->data = 5;    head->lchild = node1;    head->rchild = node2;    node1->lchild = node3;    node1->rchild = node4;    node2->lchild = NULL;    node2->rchild = NULL;    node3->lchild = NULL;    node3->rchild = NULL;    node4->lchild = NULL;    node4->rchild = NULL;    InOrderTree(head);    return 0;}


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.