Find on the binary sequence tree

xiaoxiao2021-03-20  227

#include #include #define null 0 # define n 100typedef int keytype; typef struct node {keytype key; // binary sorting table keyword Struct Node * LCHILD, * RCHILD;} bstnode; // binary sort tree node structure typef bstnode * bstree; // binary sort tree type definition bstnode * searching (bstree Tree, keytype key) {// Find keywords in Tree binary The node of KEY IF ((Tree == Null) | (Key == Tree-> Key) // If the binary sort tree Tree is the keyword of the empty or root node, the same Return Tree as the KEY; / / Return root node pointer IF (key Key) // If Key is greater than the keyword of the keyword, Return Searching (Tree-> Lchild, Key); // Find Else in the left subtree of the root // If Key is smaller than the keyword of the keyword of the root node, Return Searching (Tree-> rchild, key); // Find in the right subtree of the root} // end of search main () // Master function {BSTREE T = NULL, Q = NULL; KeyType K; CreateBST (& T); // Create a two-fork sort tree T Printf ("/ n bst data:"); inorder (t); // Output bifun sequence tree T Seminal traveler sequence Printf ("/ n"); Printf ("Input Search Key: / N"); Scanf ("% D", & K); // Enter keywords to find K q = searching (t, k ); // lookup value of the value of K in the binary sequence tree t, / / ​​Return to find the pointer to find the node, if the Failed Failed Returns NULL IF (q == Null) // Returns NULL, the output lookup failed information Printf ("Search% D default! / n", k); ELSE // If Q is not empty, the output lookup successful information Printf ("Search% D Success! / N", Q-> Key);} / / End of main insertbst (BST Ree * tptr, keytype key) {// In the binary sort tree TPTR, the insert value is the node bstnode * f, * p = * tptr; while (p) {// If * p non-empty IF (P -> key == key) // If the data field of the * P node is equal to Key Return; // Returns f = p; // F pointer points to * P, the purpose is to let the F pointer points to the dual affiliency of the node to be compared p = (Key key) P-> LCHILD: P-> rchild; // If key> p-> key, // P point to the left child of * P, otherwise point to the right kid} // end of IF p = (bstnode *) malloc (sizeof (bstnode)); // After finding the insertion position, apply for a node space * p p-> key = key; p-> lchild = p-> rchild = null; // will Key is placed in the data field of * P and puts the left and right kids IF (* tptr == null) // If the binary sequence tree is empty * tptr = P;

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

New Post(0)