Clue binary tree and several operations, clue Binary Tree operations

1/* The traversal of a binary tree is to arrange the nodes in the binary tree into a linear 2 * sequence to obtain various traversal sequences of the Binary Tree nodes. The essence is: normalize A 3 * nonlinear structure. Each node in this access sequence has a direct precursor and a direct successor. 5 * the traditional chain structure can only reflect one parent-child relationship. ￥ cannot directly obtain the frontend and successor of the node in the 6 * traversal, we know that the Binary Tree represented by the binary linked list contains a large number of null pointers. When these null pointers are used to store the pointers pointing to the node's precursor and subsequent 8, this makes it easier to use some operations of Binary Trees. 9 * The purpose of introducing a clue binary tree is to speed up the search for the node's precursor and successor. 10*11 * the clue of the binary tree is to traverse the binary tree. During the traversal process, 12 * checks whether the Left and Right pointers of nodes are empty. If so, then they are changed to the clues for the forward 13 * drive and subsequent nodes. 14 **/15 # include <stdio. h> 16 # include <stdlib. h> 17 18 # define OK 1 19 # define ERROR 0 20 21 typedef char ElemType; 22 typedef int Status; 23 24/* defines the enumeration type, the value can only be Link and Thread 25 * Link. the pointer indicates that the child 26 * Thread indicates that the pointer also indicates the precursor or the suffix 27 **/28 typedef enum {29 Link, thread 30} PointerTag; 31 32 typedef struct ThrBiTrNode {33 ElemType data; 34 struct ThrBiTrNode * lchild, * rchild; 35 PointerTag rTag, lTag; 36} ThrBiTrNode, * ThrBiTree; 37 38 ThrBiTree Pre; 39 40 Status InitThreadBinaryTree (ThrBiTree * T) {41 * T = NULL; 42 return OK; 43} 44 // create a binary tree in the First Order. Both lchild and rchild indicate to be child and right child. Therefore, the lTag and rTag are Link 45 Status ThreadBinaryTree_PreOrderInput (ThrBiTree * T) {46 ElemType c; 47 scanf ("% c", & c); 48 if (''= c) {49 * T = NULL; 50} 51 else {52 * T = (ThrBiTrNode *) malloc (sizeof (ThrBiTrNode); 53 if (! * T) {54 return ERROR; 55} 56 (* T)-> data = c; 57 (* T)-> lTag = Link; 58 (* T) -> rTag = Link; 59 ThreadBinaryTree_PreOrderInput (& (* T)-> lchild); 60 ThreadBinaryTree_PreOrderInput (& (* T)-> rchild); 61} 62 return OK; 63} 64 // <central order traversal> performs <clue> On a binary tree, but does not provide the initial Pre value, but only provides a process of 65 // intermediate, recursive thinking and thinking methods. 66 // 1 clue to left subtree 67 // 2 to parent node processing // base 68 in recursion // 3 clue to right subtree 69 Status InOrderThread (ThrBiTree T) {70 if (T! = NULL) {71 InOrderThread (T-> lchild ); // The left subtree is 72 73. // The left subtree is 72. The left subtree is 74. if (T-> lchild = NULL) {// if the left subtree is empty, use lchild as the index 75 // Pre to point to the accessed node 76 T-> lTag = Thread; 77 T-> lchild = Pre; 78} 79 if (Pre-> rchild = NULL) {// It is not until now that the Pre suffix is T, so 80 // you must set the Pre, if the rchild of Pre is null 81 //, set the rchild of Pre as the suffix and point to T 82 Pre-> rTag = Thread; 83 Pre-> rchild = T; 84} 85 86 Pre = T; // mark the current node as the recently accessed node 87 // The Pre will finally point to the last 88 of the tree's central Traversal // One node 89 90 InOrderThread (T-> rchild); // right subtree 91} 92 return OK; 93} 94 // create a middle-order binary tree, pre 95 Status CreateInOrderThreadBinaryTree (ThrBiTree T, ThrBiTree * headOfTree) {96 * headOfTree = (ThrBiTrNode *) malloc (sizeof (struct ThrBiTrNode); 97 if (! HeadOfTree) 98 return ERROR; 99 (* headOfTree)-> lTag = Link; // point to T100 (* headOfTree)-> rTag = Thread; // use the rchild of the header node to index 101 (* headOfTree)-> rchild = * headOfTree; // point to itself 102 if (T = NULL) {103 (* headOfTree) -> lchild = * headOfTree; // If T is an empty tree, the lchild104 of the header node // points to itself 105} 106 else {107 (* headOfTree)-> lchild = T; 108 Pre = * headOfTree; // assigned the global variable Pre109 InOrderThread (T); // The result is 110 in the middle order. // printf ("\ n % c \ n ", pre-> data); 111 // After InOrderThread is executed, Pre points to the last 112 // a node 113 Pre-> rTag = Thread; 114 Pre-> rchild = * headOfTree; 115 // (* headOfTree) -> rchild = Pre; 116} 117 return OK; 118} 119 // use the <Non-recursive> code for the clue binary tree after the middle-order clue. 120 // and the original recursive code is no longer allowed here.. 121 // If the binary tree is not clues, the <Non-recursive> code is also used for 122 times/calendar, but the <stack>, however, after online tracing, <Non-recursive> traversal of the binary tree with clue 123 // eliminates the need for Stack support. 124 Status visit (ElemType c) {125 printf ("% c", c); 126 return OK; 127} 128 Status InOrderTeaverse_NoRecursion (ThrBiTree T, ThrBiTree headOfTree) {129 // headOfTree is the pointer to the T header node, headOfTree-> lchild = T; 130 while (T! = HeadOfTree) {131 while (T-> lTag = Link) {// locate the tree (subtree) the leftmost node of 132 // use while to replace if // 133 // use if to understand while // 134 T = T-> lchild; 135} 136 visit (T-> data); 137 138 while (T-> rTag = Thread & T-> rchild! = HeadOfTree) {139 // This while and the following T = T-> rchild140 // can be understood by ifelse statements. 141 // if (rchild is the clue & not the last node) 142 // else (rchild is not the clue) --> go to the right child 143 // code T = T-> rchild144 T = T-> rchild; 145 visit (T-> data ); 146} 147 T = T-> rchild; 148} 149 return OK; 150} 151 // calculate the first node of the central Ordered Binary Tree under the central ordered sequence 152 ThrBiTrNode * SeekFirstNode_InOrderThrBiTree (ThrBiTree T) {153 if (T! = NULL) {154 while (T-> lTag = Link) {155 T = T-> lchild; 156} 157 return T; 158} 159 return ERROR; 160} 161 // calculate the next node of node P in the central Ordered Binary Tree 162 ThrBiTrNode * GetNextNode (ThrBiTrNode * CurrentP) {163 if (CurrentP-> rTag = Link) {// when there is a right child, the next one is the bottom left element of the tree with 164 // right child as root. 165 // the return value of seekFirstNode. 166 return SeekFirstNode_InOrderThrBiTree (CurrentP-> rchild); 167} 168 else {// if no right child exists, rchild points to the suffix 169 return CurrentP-> rchild; 170} 171} 172 // use the above two functions, SeekFirstNode_InOrderThrBiTree and GetNextNode to traverse the binary tree in the middle order of 173 // 174 Status InOrderTraverse_NoRecursion_Method (ThrBiTree T, ThrBiTree head) {175 for (T = SeekFirstNode_InOrderThrBiTree (T); T! = Head; T = GetNextNode (T) 176 visit (T-> data); 177 return OK; 178} 179 180 // test181/* The purpose of the ShowThread function is to use the InOrderTraverse182 * function to perform sequential traversal after the forward clue of T, output the information in the node to test the clue, but failed. The reason is: after 183 * clue, the NULL pointer domain is applied, and the if (T) in all InOrderTraverse functions cannot exceed 184 *, so it fails. 185 Status ShowThread (ThrBiTree C) {186 printf ("% d \ n", (C-> lchild)-> data, (C-> rchild) -> data, C-> lTag, C-> rTag); 187 printf ("% d \ n", C-> lTag, C-> rTag ); 188 return OK; 189 **/190 191 // traverse binary tree in the central order 192 Status InOrderTraverse (ThrBiTree T) {193 if (T) {194 InOrderTraverse (T-> lchild ); 195 visit (T-> data); 196 InOrderTraverse (T-> rchild); 197} 198 return OK; 199} 200 int main () {201 ThrBiTree T, R, head = NULL; 202 Ini TThreadBinaryTree (& T); 203 204 printf ("Please Input Binary Tree By PreOrder \ n"); 205 ThreadBinaryTree_PreOrderInput (& T ); 206 207 printf ("InOrder Traverse before Thread with Recursion \ n"); 208 InOrderTraverse (T); 209 printf ("\ n"); 210 211 CreateInOrderThreadBinaryTree (T, & Head); 212 printf ("InOrder Traverse after Thread with no-Recursion \ n"); 213 InOrderTeaverse_NoRecursion (T, Head); 214 printf ("\ n"); 215 2 16 printf ("The root is % c \ n", T-> data); // The value of The real pointer parameter cannot be changed by The value of The pointer parameter 217 // or, changes to pointer parameters do not apply to pointer 218 // real parameters. 219 220 ThrBiTrNode * firstOfInOrder = NULL, * secondOfInOrder = NULL; 221 if (SeekFirstNode_InOrderThrBiTree (T )! = ERROR) {222 firstOfInOrder = SeekFirstNode_InOrderThrBiTree (T); 223 printf ("the value of First Node of InOrderThreadBinaryTree is % c \ n", firstOfInOrder-> data ); 224} 225 secondOfInOrder = GetNextNode (firstOfInOrder); 226 printf ("the value of Node after D is: % c \ n", secondOfInOrder-> data ); 227 secondOfInOrder = GetNextNode (secondOfInOrder); 228 printf ("the value of Node after B is: % c \ n", secondOfInOrder-> data); 229 secondOfInOrder = GetNextNode (secondOfInOrder ); 230 printf ("the value of Node after E is: % c \ n", secondOfInOrder-> data); 231 secondOfInOrder = GetNextNode (secondOfInOrder ); 232 printf ("the value of Node after A is: % c \ n", secondOfInOrder-> data); 233 secondOfInOrder = GetNextNode (secondOfInOrder ); 234 printf ("the value of Node after C is: % c \ n", secondOfInOrder-> data); 235 secondOfInOrder = GetNextNode (secondOfInOrder ); 236 printf ("the value of Node after G is: % c \ n", secondOfInOrder-> data); 237 secondOfInOrder = GetNextNode (secondOfInOrder ); 238 printf ("the value of Node after root is: % c \ n", secondOfInOrder-> data ); 239 240 printf ("InOrder Traverse after Thread with no-Recursion Method_2 \ n"); 241 InOrderTraverse_NoRecursion_Method (T, Head); 242 243 return 0; 244}