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