#include
#include
#include
# define the error 0
# define OK 1
//design and implementation of an existing sequence clues in the binary tree algorithm, and gives the sequence clues to traverse the binary tree traversal sequence of
Typedef enum {Link, Thread} PointerTag;
//to enumerate the link==0 pointer, clues to the Thread==1
Typedef struct BiThrNode {char data; Struct BiThrNode * lchild; Struct BiThrNode * rchild; PointerTag LTag RTag;//sign
} BiThrNode, * BiThrTree;
BiThrTree pre.//to have just visited node, given a global variable
Void InitList (BiThrTree * l)//initializes the
{* l=(BiThrTree) malloc (sizeof (BiThrNode)); (* l) - & gt; Lchild=NULL; (* l) - & gt; Rchild=NULL;
} void CreatBiTree BiThrTree * (T)
{char ch//create a binary tree; Ch=getchar ();//ch read in characters if (ch=='. ') * T=NULL;//if read in the '. ', then the node is empty the else {the if (! (* (T)=(BiThrTree) malloc (sizeof (BiThrNode))))//read the characters for exit node space distribution (0); (* T) - & gt; data=https://bbs.csdn.net/topics/ch;//as node data fields assignment CreatBiTree (& (* T) -> lchild);//recursive CreatBiTree (& (* T) -> rchild); (* T) -> LTag=Link; (* T) -> RTag=Link; }
}
Int visit (char c)
{printf (" % \ t "c, c); Return OK;
} void InOrderTraverse (BiThrTree T)
{if (T) {InOrderTraverse (T - & gt; Lchild); Visit (T - & gt; The data); InOrderTraverse (T - & gt; Rchild); }
}
Void InThreading (BiThrTree p)
{//p as the root node of the tree, for clues to change operation the if (p) {InThreading (p - & gt; Lchild);//left subtree clues, search to the left of the last node if (! P - & gt; Lchild) {//not left the child to add precursor clues p - & gt; LTag=Thread; P - & gt; Lchild=pre; }//precursor clues if (! P - & gt; Rchild) {//not right child to add subsequent pre - & gt; RTag=Thread; The pre - & gt; Rchild=p; }//subsequent clues pre=p;//pre point to a tree in the sequence traversal of the last node InThreading (p - & gt; Rchild);//right subtree clues}
}
Void InOrderThreading (BiThrTree * Thrt, BiThrTree T)
{//sequence traversal of binary tree and the sequence clues//create a head node, Thrt points to head node * Thrt=(BiThrNode *) malloc (sizeof (BiThrNode)); (* Thrt) - & gt; LTag=Link;//set up head node (* Thrt) - & gt; RTag=Thread;//subsequent to find clues (* Thrt) - & gt; Rchild=* Thrt;//when the initial right cursor back if (! T)//if the binary tree is empty, the left cursor back (* Thrt) - & gt; Lchild=* Thrt; The else {//is not empty (* Thrt) - & gt; Lchild=T;//head nodes left pointer to the root of the pre=* Thrt;//pre point to just visited node (because only know a pair of nodes can clues, only when the traverse to node p, just know the pre is its immediate predecessor nodes) InThreading (T);//in the sequence traversal sequence clues in the pre - & gt; Rchild=* Thrt;//in the last node leads to (make it and head node into contact) toward the head of the pre - & gt; RTag=Thread;//and set as clues (* Thrt) - & gt; Rchild=pre;//head node rchild pointing at the same time sequence traversal of the last node}
}
//clue is the essence of the binary null pointer to point to the node in the list of precursor or subsequent clues
//a successor and precursor information only when traversing can find, when p, traverse the nodes if its precursor node pre right child, to set the pre - & gt; Rchild=p similarly
//the clues process is in the process of traversing the modification process of null pointer int InOrderTraverse_Thr (BiThrTree T, int (* visit) (char), c)
{//sequence traversal clues in the binary tree recursively, for each data element function call visit ()//T to head node, head node lchild points to the root node BiThrTree p; P=T - & gt; Lchild;//p to binary tree root node while (p!=T) {//or empty tree traversal p==T at the end of the while (p - & gt; LTag==Link) p=p - & gt; Lchild;//p - & gt; LTag=0 has left children have been left child traverse the if (! Visit (p - & gt; Data)) return the error;//access left child node of the tree is empty return error while (p - & gt; RTag==Thread& & P - & gt; Rchild!=T)//p - & gt; RTag=1. The children show that no right, and not the last node, right child points to the subsequent {p=p - & gt; Rchild; Visit (p - & gt; The data); }//access the subsequent nodes p=p - & gt; Rchild;//if p - & gt; RTag==0, has the right child, from right subtree began to repeat the above process} return OK;
} int main ()
{BiThrTree T; BiThrTree Thrt=NULL; Printf (" please input from the user to expand the first sequence traversal sequence (loophole in dots tree) : \ n "); InitList (& amp; T); CreatBiTree (& amp; T); Printf (" \ n clues before: \ n "); InOrderTraverse (T); Printf (" \ n after a clue: \ n "); InOrderThreading (Thrt, T); InOrderTraverse_Thr (Thrt, visit); return 0;
}
CodePudding user response:
Should be the pointer cannot be changed, not malloc is assignment problemsParameter and argument is not a concept
CodePudding user response:
I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10581430.html
I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10768339.html