Definition of clew binary tree: it is still created according to the method of chain binary tree, except that the left pointer of the node which is originally empty is changed to point to the precursor of the node in the middle order traversal, and the right pointer of the node which is originally empty is changed to point to the successor of the node in the middle order traversal, that is to say, the null pointer is used.
1. Define structure
Different from the chain binary tree, the node adds two data to determine whether the next link of the pointer is a tree or a clue
typedef enum{Link,Thread}PointerTag; //0 Indicates connection, 1 indicates clue typedef struct BiTNode{ char data; struct BiTNode *lchild,*rchild; //Left and right child trees PointerTag ltag; //Link Representative connection,Thread Representative clue PointerTag rtag; }BiTNode,*BiTree; BiTree pre; //Global variable, always pointing to the visited node
2. Creation of binary tree
//The establishment of binary tree void createBiTree(BiTree *T) { char c; scanf("%c",&c); if(c=='$') { *T=NULL; } else { *T=(BiTree)malloc(sizeof(BiTNode)); (*T)->data=c; (*T)->ltag=Link; //Let's set all have left subtrees (*T)->rtag=Link; //Let's set that all have right subtrees createBiTree(&(*T)->lchild); //Create a binary tree by traversing first createBiTree(&(*T)->rchild); } }
3. The empty finger needle in the binary tree is cabled, and pre points to the last node
In the beginning, pre points to the head node
void createThread(BiTree T) { if(T) //Clue binary tree is not empty { createThread(T->lchild); if(!T->lchild) //If the node has no left subtree { T->ltag=Thread; //Clue the left side of the node T->lchild=pre; //The precursor of this node points to the previous node } if(!pre->rchild) //If the previous node has no right subtree { pre->rtag=Thread; //Clue the right side of the node pre->rchild=T; //Point the successor of the previous node to the node } pre=T;//Downward cycle createThread(T->rchild); } }
4. Add head node
The steps are as follows:
(1) . create a header node first,
(2) . the left pointer of the header node points to the root node of the binary tree, which is represented as Link
(3) The left pointer of the first node traversed by the middle order is represented as Thread, pointing to the head node
(4) The right pointer of the tail node traversed in the middle order is expressed as thread, pointing to the head node
(5) The right pointer of the head node points to the end node of the order traversal in the binary tree, which is represented as Thread
//Add head node to clue binary tree void createHeadNode(BiTree *thrt,BiTree T) { *thrt=(BiTree)malloc(sizeof(BiTNode)); //Create head node if(!thrt) { printf("Failed to create space\n"); } (*thrt)->ltag=Link; //On the left is the connection (*thrt)->rtag=Thread; //On the right is the clue (*thrt)->rchild=*thrt; //The right pointer points to itself if(!T) //If binary tree is empty { (*thrt)->lchild=*thrt; } else //If binary tree is not empty { (*thrt)->lchild=T; //The left pointer of the head node points to the root node pre=*thrt; //Point the value of the precursor to the head node createThread(T); //Middle order traversal is used for middle order clue, pre Point to the last node of middle order traversal pre->rtag=Thread; //The right mark of the last node is the clue pre->rchild=*thrt; //The right pointer of the last node points to the head node (*thrt)->rchild=pre; //The right pointer of the head node points to the last node } }
5. Non recursive traversal in the middle order
Middle order traversal: first find the left most node, then access its root node, and then access the right node
Here's how:
1. First find the leftmost node, and then print it
2. If there is a right subtree in this node, access the right subtree, and return the loop
2. If there is no right subtree, access the clue, print out the node, access its right subtree, and then loop
void middleSort(BiTree T) { BiTree p; p=T->lchild;//p Is the root node of the binary tree while(p!=T) //At the end of an empty tree or traversal, p==T { while(p->ltag==Link) //Find the leftmost node first { p=p->lchild; } printf("%c ",p->data); while(p->rtag==Thread&&p->rchild!=T) { p=p->rchild; printf("%c ",p->data); } p=p->rchild; //if p->rchild It's not a clue(It's the right child),p Point to the right child, return to the loop } }
All codes are as follows:
#include<stdio.h> #include<stdlib.h> typedef enum{Link,Thread}PointerTag; //0 Indicates connection, 1 indicates clue typedef struct BiTNode{ char data; struct BiTNode *lchild,*rchild; //Left and right child trees PointerTag ltag; //Link Representative connection,Thread Representative clue PointerTag rtag; }BiTNode,*BiTree; BiTree pre; //Global variable, always pointing to the visited node //The establishment of binary tree void createBiTree(BiTree *T) { char c; scanf("%c",&c); if(c=='$') { *T=NULL; } else { *T=(BiTree)malloc(sizeof(BiTNode)); (*T)->data=c; (*T)->ltag=Link; //Let's set all have left subtrees (*T)->rtag=Link; //Let's set that all have right subtrees createBiTree(&(*T)->lchild); //Create a binary tree by traversing first createBiTree(&(*T)->rchild); } } //Middle order traversal of binary tree to clue ,After the completion of clue pre Point to the last node void createThread(BiTree T) { if(T) //Clue binary tree is not empty { createThread(T->lchild); if(!T->lchild) //If the node has no left subtree { T->ltag=Thread; //Clue the left side of the node T->lchild=pre; //The precursor of this node points to the previous node } if(!pre->rchild) //If the previous node has no right subtree { pre->rtag=Thread; //Clue the right side of the node pre->rchild=T; //Point the successor of the previous node to the node } pre=T;//Downward cycle createThread(T->rchild); } } //Add head node to clue binary tree void createHeadNode(BiTree *thrt,BiTree T) { *thrt=(BiTree)malloc(sizeof(BiTNode)); //Create head node if(!thrt) { printf("Failed to create space\n"); } (*thrt)->ltag=Link; //On the left is the connection (*thrt)->rtag=Thread; //On the right is the clue (*thrt)->rchild=*thrt; //The right pointer points to itself if(!T) //If binary tree is empty { (*thrt)->lchild=*thrt; } else //If binary tree is not empty { (*thrt)->lchild=T; //The left pointer of the header node points to the root node pre=*thrt; //Point the value of the precursor to the head node createThread(T); //Middle order traversal is used for middle order clue, pre Point to the last node of middle order traversal pre->rtag=Thread; //The right mark of the last node is the clue pre->rchild=*thrt; //The right pointer of the last node points to the head node (*thrt)->rchild=pre; //The right pointer of the head node points to the last node } } //Middle order traversal clue binary tree T Non recursive algorithm of void middleSort(BiTree T) { BiTree p; p=T->lchild;//p Is the root node of the binary tree while(p!=T) //At the end of an empty tree or traversal, p==T { while(p->ltag==Link) //Find the leftmost node first { p=p->lchild; } printf("%c ",p->data); while(p->rtag==Thread&&p->rchild!=T) { p=p->rchild; printf("%c ",p->data); } p=p->rchild; //if p->rchild It's not a clue(It's the right child),p Point to the right child, return to the loop } } int main() { printf("Please input binary tree, in the order of traversal, empty finger needle $Sign means\n"); BiTree T,K; createBiTree(&T); //Generate a binary tree in the order of traversal createHeadNode(&K,T); //Add a head node to a binary tree and thread it middleSort(K); //Non recursive output value of method traversing in middle order }
This is the complete clue binary tree. Maybe the clue binary tree has other representations. You can also try to write it.