Algorithm learning-clue two fork Tree

Source: Internet
Author: User

Clue Two fork Tree

Clue Two The fork tree is more than a few things in a generic two-fork tree. Precursor and successor, turning the two-fork tree into a chain-like structure. Explanation: Usually in our two-fork tree, the leaf node is no child, so point to null that is NULL , in the Clue two fork tree, the left and right children of the leaf node respectively point to its own precursor and successor, and the precursor and successor is which node? is the previous node and the latter node of the tree traversal process. So the first traversal of the node is no precursor, the last node is no successor. Here is usually the middle sequence clues two fork tree, of course, there are first order clues two fork tree and post-sequence clues two fork tree.

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

Above is a binary tree, I have added a label on both sides of each node[]I have not added the contents of this label, there are only two kinds of values, one is0One is1, when the node has a child node,0, when there is no child for1
So the structure of the node is:

typedef struct TREENODE* node;struct treenode{    node lchild;    int Ltag;    int Rtag;    node rchild;    int data;};


When it is, point to the 0 child's node, and 1 point to the precursor or successor.
The following code is attached.

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;}


Algorithm learning-clue two fork Tree

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.