Collection operation of binary tree

xiaoxiao2021-03-05  29

The following program source code: #include

#include

#include

#define NULL 0 #define DataType char typedef struct BinTreeNode * PBinTreeNode; typedef PBinTreeNode * PBinTree; struct BinTreeNode {DataType info; PBinTreeNode llink; PBinTreeNode rlink;}; PBinTreeNode Create_BinTree (void); PBinTree Create_BinTreeRoot (void) {PBinTree pbtree; pbtree = (PBinTree) malloc (sizeof (struct BinTreeNode)); if (pbtree == NULL) pbtree = (PBinTree) realloc (pbtree, sizeof (struct BinTreeNode)); * pbtree = Create_BinTree (); return (pbtree);} PBinTreeNode Create_BinTreeNode (void) {PBinTreeNode pbnode; pbnode = (PBinTreeNode) malloc (sizeof (struct BinTreeNode)); if (pbnode == NULL) pbnode = (PBinTreeNode) realloc (pbnode, sizeof (struct BinTreeNode)); else pbnode-> llink = Pbnode-> rlink = (pbintreenode) null; return (pbnode);} int isalphabet (char i) {if (i> = 'a' && i <= 'Z' || i> = 'a' && i <= 'Z' || i == '@') Return 1; Else Return 0;} Pbintreenode Create_bintree (void) {Pbintreenode PBNode; DataType I; Printf ("Please Input A Char: / T"; FFlush (stdin); Scanf ("% C", & i); fflush (stdin); while (! isalphabet (i)) {Printf ("Sorry, Your Input Agin:"); scanf ("% c", & i); fflush (stdin);} if (i == '@') PBNODE = NULL; Else {PBNode = (Pbintreenode); if (pbnode == null); {printf ("out of space! / n"); return pbnode;} Pbnode-> info = i ; pbnode-> llink = Create_BinTree (); pbnode-> rlink = Create_BinTree ();} return pbnode;} void outputTree (PBinTreeNode pbnode, int totalSpace) {int i; if (! pbnode = NULL) {totalSpace = 5; outputTree (PBNode->

RLINK, TOTALSPACE; for (i = 0; i

INFO); OUTPUTTREE (PBNode-> LLINK, TOTALSPACE);}}} void preorder {if (pbnode == null) return; printf ("% c / t", pbnode-> info); Preorder (PBNode- > llink); preorder (PBNode-> rlink);} void inorder (pbintreenode pbnode) {if (pbnode == null) return; inorder (pbnode-> llink); Printf ("% C / T", PBNode-> Info ); inorder (PBNODE-> rlink);} void postorder (pbintreenode pbnode) {if (pbnode == null) return; postorder (pbnode-> llink); PostORDER (PBNode-> rlink); Printf ("% C / T ", pbnode-> info);} void Leaves (pbintreenode pbnode) {if (pbnode-> llink! = null && pbnode-> rlink == null) Leaves (pbnode-> llink); if (pbnode-> rlink! = Null && pbnode-> llink == null) Leaves (pbnode-> rlink); if (pbnode-> llink! = Null && pbnode-> rlink! = Null) {Leaves (pbnode-> llink); Leaves (PBNode-> RLINK);}} (pbnode-> llink == null && pbnode-> rlink == null) {printf ("% c / t", pbnode-> info); return;}} void freeallnodes (pbintreenode pbnode) {ix (PBNode-> LLINK! = null && pbnode-> rlink == null) FreeAllNodes (PBNode-> LLINK); if (PBNode-> RLINK) ! = Null && pbnode-> llink == null) freeAllNodes; if (pbnode-> llink! = Null && pbnode-> rlink! = Null) {freeAllNodes (PBNode-> llink); FreeAllNodes (PBNode " -> rlink);} if (pbnode-> llink == null && pbnode-> rlink == null) {free (pbnode-> llink); free (pbnode-> rlink); pbnode = null; return;}} int Main () {pbintree pbtree; int tent = 0; Printf ("please input char to the binatree, @ @ @ @ @ @ @ @ @ @ @ @ @ @

to exit current node: / n "); pbtree = Create_BinTreeRoot (); printf (" Display the binatree data directly: / n "); outputTree (* pbtree, totalSpace); printf (" Please choose the mode you want to operate with THE BINATREE: / N "); Printf (" 1.display 2.preorder 3.inorder 4.postorder 5.Leaves 6.free Nodes 0 to EXIT: "); Scanf ("% D ", & I); while (i > 6 || i <0) {Printf ("/ NYOU Choice ILLEGAL PLEASE INPUT AGAIN: / N"); Printf ("1.display 2.preorder 3.Printr 4.postorder 5.Leaves 6.free Nodes 0 To EXIT: "); Scanf ("% D ", & I);} while (i! = 0) {while (i> 6 || i <0) {printf (" / NYOU choice is Illegal please input again: / n "); Printf (" 1.Display 2.preorder 3.inorder 4.postorder 5.leaves 6.free nodes 0 to exit: "); scanf ("% d ", & i);} while (i! = 0) {While (i> 6 || i <0) {Printf ("/ NYOU Choice ILLEGAL PLEASE INPUT AGAIN: / N"); Printf ("1.display 2.preorder 3.Inorder 4.postorder 5.leaves 6. Free Nodes 0 to EXIT: "); Scanf ("% D ", & I);} while (i! = 6) {while (i> 6 || i <0) {printf (" / NYOU Choice Is Illegal please input Again: / n "); Printf (" 1. Display 2.Preorder 3. Inorder 4.postorder 5.Leaves 6.free Nodes 0 to EXIT: "); scanf ("% d ", & i);} switch (i) {cas 0: printf (" / ndealing with the the Last Job, To Free All Nodes ... / N "); FreeAllNodes (* Pbtree); Printf (" All Node Have Been Freed SuccessFully / N "); EXIT (1); getCh (); Case 1: Printf (" / ndisplay binatree: / n "); OUTPUTTREE (* Pbtree, Totalspace); Break; Case 2: Printf (" / NData in Preorder: / N "); Preorder (* Pbtree); Printf (" / n / n ") Break; Case 3: Printf ("/ NData in inorder: / n"); inorder (* pbtree); Printf ("

/ N / N "); Break; Case 4: Printf (" / ndata in postorder: / n "); PostOrder (* pbtree); Printf (" / n / n "); Break; Case 5: Printf (" / NLLVES: / N "); Leaves (* pbtree); Printf (" / n / n ");} Printf (" please choort you want to operate with the binatree: / n "); printf (" 1.Display 2.PREORDER 3.INORDER 4.POSTORDER 5.LEVES 6.FREE NODES 0 to EXIT: "); scanf ("% d ", & i);} if (i == 6) {printf (" / nfree all nodes: / N "); freeallnodes (* pbtree); Printf (" All node has been freed successfully. ");} Printf (" / n / nnow create a new binatree ... / n "); Printf (" please input char to the binatree, @ to exit current node: / n "); pbtree = Create_BinTreeRoot (); printf (" Display the binatree data directly: / n "); outputTree (* pbtree, totalSpace); printf (" Please choose the mode You want to operate with the binatree: / n "); Printf (" 1.Display 2.preorder 3.inorder 4.postorder 5.Leaves 6.free Nodes 0 to EXIT: "); Scanf ("% D ", & i );}} printf ("/ ndealing with the last job, to free all nodes / n"); FreeAllNodes (* pbtree); Printf ("All Node Have Been Freed Success Fully / n "); getCh (); return 0;} --------------------------------- Here Detailed analysis of procedures ----------------------------------------------------------- The description and analysis of the problem prepares a program, including the binary tree, including the creation of a binary tree, showing the tree-type structure of the binary tree, and traverses the created two-forked tree, in three ways, and prints the leaves. Node, and you can delete the binary tree we create at any time, then encapsulate the above-described operation with a loop statement to make it repeatable, continuous operation. two. The algorithm is designed to achieve the above functions, we must first understand the data structure of the binary tree, then in accordance with its characteristics, start how to create a binary tree, then use different ways to different traversal, realize Other functions, including the implementation of user-friendly interfaces, etc. A series of follow-up operations.

three. A data structure of the Program designed to use the data type struct BinTreeNode {DataType info; PBinTreeNode llink; PBinTreeNode rlink;}; then define a pointer type typedef struct BinTreeNode * PBinTreeNode we need; binary tree nodes point to define a pointer type typedef PBinTreeNode * Pbintree; define a pointer type that points to the node node. The program needs to be used for custom functions 1. Create a binary root node pbintree create_bintreeroot (void) 2. Create a node of a binary tree PBINTREENODE CREATE_BINTREENODE (VOID) 3. Create a binary tree Pbintreenode Create_Bintree (Void) 4. Use the first method to traverse a binary tree Void Preorder (Pbintreenode PBNode). Use the middle root method to traverse a binaryate Void inorder (Pbintreenode PBNode) 6. Use the retrieval method to traverse a binaryate Void PostOrder (Pbintreenode PBNode) 7. Print out the tree structure of the binary tree we create Void OutputTree (INT TOTALSPACE) 8. Print the leaves of the binary tree void Leaves (Pbintreenode Pbnode) 9. Release all of the nodespaces we apply, Void FreeallNodes (Pbintreenode PBNode) 10. Determine if we entered is a qualified character, we define them as A-Z or A-Z characters, use '@' character as the identifier of the current node Int Isalphabet (CHAR I). Realization of specific procedures 1. PBINTREENODE CREATE_BINTREENODE (VOID) We define a pointer Pbintreenode pointing to the binary todping point, and then apply for a binary nodes size space, assigning the left and right sub-nodes (given the strict definition of the Visual C category, we assign it here For (Pbintreenode NULL - annotation) 2. Create a binary root node define a pointer to the pointer to the binary nodes: bintreenode ** Pbtree, or: pbintreenode * pbtree; used to store the root node of the tree, at the same time, transfer the address of the binary tree created to it That is: * pbtree = crete_bintree (); 3. Create a binary tree first we define a DataType type variable i that is used to store the characters we entered (ie, as a buffer), and use the scanf function to receive it. Due to the use of the SCANF function, it will absorb the carriage return character we entered. And the case where the car is used as the received character, in order to avoid this, we use the function fflush (stdin) to turn off the characters of the buffer, then absorb the characters we entered, you can completely avoid such problems. happened. We define the characters we enter from AZ or AZ, with character @ to end the current node left or right node, and then recursively call the left and right bonus, at this time we will create a binary tree full creation. . 4. Traversing a two-fork tree first accessing the root node, prints out the information in it, and then recursively call the left sub-tree and the right child.

5. Use the middle root method to traverse a binary tree, call the left subtree, print the information inside, print out the root point information, and then recursively call the right tree, print the information inside. 6. Use the back root method to traverse a binary tree first recursive call left subtree, print out the content, then recursively call the right tree, print the content inside, then the content of the root point 7. Print out the binary tree we created The tree structure is first recursted to call the right tree. If the node is not empty, the number of spaces plus 5. If it is empty, print the contents of the right child, and then recursively call the left sub-tree 8. Printing the junction of the binary tree If the left and right bon trees of the current node are empty, the information of this node is printed. If the left sub-tree is not empty, the recursive calls left subtree, if the right tree is not Empty words, recursive call the right tree. 9. If all the nodes we apply If the current left sub-tree is not empty, it is traversed to the left subtree. If the right tree is not empty, it traversed the right tree. If it is empty, if you are empty, call the left and right respectively. Sub trees, if you are empty, you will release the left and right node space and set the current node. 10. Arrangement of the main function: first create the binary tree we want, then we can do a variety of operations on this, we define 6 collection operations, and test the information entered by the user, the correct prompt error message If you select one of the operations we define, we set different CASE statements. If we choose to release all node space, we need to block all the menu information, prompting the user to recreate a binary tree and repeat it. If we choose to quit, but do not choose to release all node spaces, we need to consider this case, call Void FreeAllNodes (Pbintreenode PBNode) Automatically release all node space, normal exit.

转载请注明原文地址:https://www.9cbs.com/read-34289.html

New Post(0)