The definition of the clue binary tree: it is still created according to the method of chain binary tree, but the left pointer that is originally empty at the node is changed to point to the precursor of the node in the middle sequence traversal, and the right pointer that is originally empty to the node The successor of the node in the middle-order traversal, that is to say, the empty pointer is used.
1. Define the structure
Different from the chain binary tree, the node adds two data to determine whether the next pointer is connected to the tree or the clue.
typedef enum {Link, Thread} PointerTag; // 0 means connection, 1 means clue typedef struct BiTNode { char data; struct BiTNode * lchild, * rchild; // left and right child tree PointerTag ltag; // Link means connection, Thread means clue PointerTag rtag; }BiTNode,*BiTree; BiTree pre; // Global variable, always points to the visited node
2. The creation of a 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; // First set to have a left subtree (* T)-> rtag = Link; // First set to have a right subtree createBiTree (& (* T)-> lchild ); // Create a binary tree by traversing in order first createBiTree (& (* T)-> rchild); } }
3. Clue the null pointer in the binary tree, pre points to the last node
It just clues the empty pointer in the binary tree, and the pre at the beginning points to the head node
void createThread(BiTree T) { if (T) // The clue binary tree is not empty { createThread(T->lchild); if (! T-> lchild) // If the node has no left subtree { T- > ltag = Thread; // Lead 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; // Lead the right of the node pre-> rchild = T; // Point the successor of the previous node to the node } pre = T; // loop down createThread (T- > rchild ); } }
4. Add head node
Proceed as follows:
(1). First create a head node,
(2). The left pointer of the head node points to the root node of the binary tree, expressed as Link.
(3) The left pointer of the first node traversed in the sequence is represented as Thread, pointing to the head node
(4) The right pointer of the tail node in the middle sequence traversal is represented as Thrad, pointing to the head node
(5). The right pointer of the head node points to the tail node of order traversal in the binary tree, expressed as Thread.
// The clue binary tree adds a head node 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; // The left is the connection (* thrt)-> rtag = Thread; // The right is the clue (* thrt)-> rchild = * thrt; // The right pointer points to yourself if (! T) // If the binary tree is empty { (*thrt)->lchild=*thrt; } else // If the 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); // Mid-order traversal in-order clues of, pre inorder traversal of the last point to a node pre-> = the rtag the Thread; // the right flag is the last node clue pre-> * = rchild THRT; // last right node pointer to the head node Point (* thrt)-> rchild = pre; // The right pointer of the head node points to the last node } }
5. Non-recursive traversal according to the method of in-order traversal
Mid-order traversal: first find the leftmost node, then visit its root node, then visit the right node
The method here is:
1. Find the leftmost node first, and then print the node
2. If there is a right subtree at the node, access the right subtree, then return to the loop
2. If there is no right subtree, then visit the clue, print out the node, visit 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 the 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 is not a clue (is the right child), p points 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 means connection, 1 means clue typedef struct BiTNode { char data; struct BiTNode * lchild, * rchild; // left and right child tree PointerTag ltag; // Link means connection, Thread means clue PointerTag rtag; }BiTNode,*BiTree; BiTree pre; // Global variable, always points 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; // First set to have a left subtree (* T)-> rtag = Link; // First set to have a right subtree createBiTree (& (* T)-> lchild ); // Create a binary tree by traversing in order first createBiTree (& (* T)-> rchild); } } // The mid-order traversal of the binary tree leads to clueing. After clueing , pre points to the last node void createThread (BiTree T) { if (T) // The clue binary tree is not empty { createThread(T->lchild); if (! T-> lchild) // If the node has no left subtree { T- > ltag = Thread; // Lead 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; // Lead the right of the node pre-> rchild = T; // Point the successor of the previous node to the node } pre = T; // loop down createThread (T- > rchild ); } } // The clue binary tree adds a head node 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; // The left is the connection (* thrt)-> rtag = Thread; // The right is the clue (* thrt)-> rchild = * thrt; // The right pointer points to yourself if (! T) // If the binary tree is empty { (*thrt)->lchild=*thrt; } else // If the 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); // Mid-order traversal in-order clues of, pre inorder traversal of the last point to a node pre-> = the rtag the Thread; // the right flag is the last node clue pre-> * = rchild THRT; // last right node pointer to the head node Point (* thrt)-> rchild = pre; // The right pointer of the head node points to the last node } } // Non-recursive algorithm for middle-order traversal clue binary tree T 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 the 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 is not a clue (is the right child), p points to the right child, return to the loop } } int main () { printf ( " Please enter the binary tree, in the order of traversal, the null pointer is represented by a $ sign \ n " ); BiTree T,K; createBiTree ( & T); // Generate a binary tree in the order of first-order traversal createHeadNode (& K, T); // Add head nodes to the binary tree and clue middleSort (K); // Non-recursive output according to the middle-order traversal method Value }
This is the complete clue binary tree. Perhaps the clue binary tree has other representations, you can also try to write it.