The search tree data structure supports a variety of dynamic collection operations, including search, MINIMUM, MAXIMUM, predecessor, successor, INSRT, and delete operations. The basic search tree is a binary search tree.
12.1 What is a two-fork search tree
1. The nature of the two-fork search tree:
Set X is a node in a two-fork search tree. If Y is a node in the X left dial hand tree, then Y.key<=x.key. If Y is a node in the X right subtree, then Y.key>=x.key.
Three kinds of traversal time complexity is O (n), which is obvious.
12.1-3
1 voidInorder_tree_walk_nonrecursive_1 (node_t *x) {2stack<node_t *>S;3 BOOLMID =false;4 5printf"Using Stack to implement inorder_tree_walk:\n");6 s.push (x);7 while(!S.empty ()) {8 if(x->l = = NULL | |mid) {9printf"%d", x->key);Ten S.pop (); One if(X->r = =NULL) { A if(S.empty ()) - Break; -x =s.top (); theMID =true; -}Else { -x = x->R; - s.push (x); +MID =false; - } +}Else { Ax = x->l; at s.push (x); - } - } -printf"\ n"); - } - voidInorder_tree_walk_nonrecursive_2 (node_t *x) { inprintf"Using Nonstack to implement inorder_tree_walk:\n"); -node_t *p = x->p, *y =NULL; to BOOLRET =false, RETR =false; + - while(!retr | | y!=p) { the if(x->l = = NULL | |ret) { * if(!retr) $printf"%d", x->key);Panax Notoginseng if(X->r = = NULL | |retr) { -RETR = (x = = Y->R); thex =y; +y = y->p; ARET =true; the}Else { +y =x; -x = x->R; $RET =false; $ } -}Else { -y =x; thex = x->l; - }Wuyi } theprintf"\ n"); -}
12.1-5
12.2 Query Binary search tree
In addition to the basic search, it also includes minimum, MAXIMUM, successor, Predessor and other query operations.
12.2-5
12.2-6
12.2-7
12.2-8
12.2-9
12.3 Inserting and deleting
Insert operation is relatively simple, the deletion of basic binary tree needs to be discussed in categories. The book uses a sub-function transplant simplifies some basic operations, making the delete process clearer.
The time complexity of insertions and deletions is O (h).
12.3-2
12.3-3
12.3-4
Non-exchangeable, anti-for example:
12.3-5
The Chinese "Introduction to Arithmetic (3rd edition)" This problem completely translated wrong, went to see a bit of English to understand the topic to do.
As the hint shows, the insertion, deletion, and lookup functions are re-implemented using subsequent attributes.
The idea of finding the parent of node p is to get the maximal value of the subtree of P, and the successor of the node is the parent node of P. Rest
Operation requires the use of subsequent properties. Take insert as an example.
1 voidTree_insert (tree_t *t, node_t *z) {2node_t *y =NULL;3node_t *x = t->Root;4 while(X! =NULL) {5y =x;6 if(Z->key < x->key)7x = x->l;8 Else9x = x->R;Ten } One if(Y = =NULL) { AT->root = Z;//Tree T was empty -Z->SUCC =NULL; -}Else if(Z->key < y->key) { thenode_t *p =parentof (y); -Z.SUCC =y; -P.SUCC =Z; -}Else { +Z->SUCC = y->succ -Y->SUCC =Z; + } A}
12.4 randomly constructed binary search tree
Introduction to algorithms many chapters will talk about randomization of random algorithms on data structures. And it is a big paragraph of the proof, very sharp. Random algorithm I personally intend to spend time alone to study, so the topics involved in the second reading of the Guide to do again.
It is shown that the desired height of a randomly constructed two-fork search tree with n different keywords is O (lgn). The time complexity of many basic operations of the main random search tree is O (LGN). The efficiency of randomization is still very high, can actually write a random data generator, test it.
"Introduction to Algorithms" Learning notes--12th Chapter Two search tree