1 / 89

Chapter 11 MULTIWAY TREES

Chapter 11 MULTIWAY TREES. 1. Orchards, Trees, and Binary Trees. 2. Lexicographic Search Trees: Tries. 3. External Searching: B-Trees. 4. Red-Black Trees. Pointers and Pitfalls. 11.1 On the Classification of Species. 1. On the Classification of Species. Definition:.

Télécharger la présentation

Chapter 11 MULTIWAY TREES

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 11 MULTIWAY TREES 1. Orchards, Trees, and Binary Trees 2. Lexicographic Search Trees: Tries 3. External Searching: B-Trees 4. Red-Black Trees Pointers and Pitfalls

  2. 11.1 On the Classification of Species 1. On the Classification of Species Definition: A (free) tree is any set of points (called vertices) and any set of pairs of distinct vertices (called edgesor branches) such that (1) there is a sequence of edges (a path) from any vertex to any other, and (2) there are no circuits, that is, no paths starting from a vertex and returning to the same vertex.

  3. 2. Ordered Trees A rooted treeis a tree in which one vertex, called the root, is distinguished. An ordered treeis a rooted tree in which the children of each vertex are assigned an order. A forestis a set of trees. We usually assume that all trees in a forest are rooted. An orchard(also called an ordered forest) is an ordered set of ordered trees.

  4. See pg.522 Fig.11.1

  5. 3. Forests and Orchards Multiple links first child and next sibling links Correspondence with binary trees

  6. Recursive Definitions Definition A rooted tree consists of a single vertex v, called the root of the tree, together with a forest F , whose trees are called the subtrees of the root. A forest F is a (possibly empty) set of rooted trees. Definition An ordered tree T consists of a single vertex v, called the root of the tree, together with an orchard O,whose trees are called the subtrees of the root v. We may denote the ordered tree with the ordered pair T = {v,O}. An orchard O is either the empty set ;, or consists of an ordered tree T , called the first tree of the orchard, together with another orchard O’ (which contains the remaining trees of the orchard). We may denote the orchard with the ordered pair O = (T ,O’).

  7. 4. The Formal Correspondence Definition A binary tree B is either the empty set ; or consists of a root vertex v with two binary trees B1 and B2 . We may denote the binary tree with the ordered triple B = [v;B1;B2]. 数学归纳法 Theorem 11.1 Let S be any finite set of vertices. There is a one-to-one correspondence f from the set of orchards whose set of vertices is S to the set of binary trees whose set of vertices is S. Proof. Define f () = Define f ({v,O1},O2) =[ v,f(O1) ,f(O2) ] Show by mathematical induction on the number of vertices that f is a one-to-one correspondence.

  8. 5.Rotations  Draw the orchard so that the first child of each vertex is immediately below the vertex.  Draw a vertical link from each vertex to its first child, and draw a horizontal link from each vertex to its next sibling.  Remove the remaining original links.  Rotate the diagram 45 degrees clockwise, so that the vertical links appear as left links and the horizontal links as right links.

  9. 6. Summary Orchards and binary trees correspond by any of:  first child and next sibling links,  rotations of diagrams,  formal notational equivalence.

  10. 11.2 Lexicographic Search Trees: Tries • TriesDefinitions pg.530 Definition: A trie of order m is either empty or consists of an ordered sequence of exactly m tries of order m.

  11. 2. Seach for a Key pg.530

  12. 3. C++ Tries Declarations pg.531-532 Every Record has a Key that is an alphanumeric string. Method char key letter(int position) returns the character in the given position of the key or returns a blank, if the key has length less than position. Auxiliary function int alphabetic order(char symbol) returns the alphabetic position of the character symbol, or 27 for nonblank, nonalphabetic characters, or 0 for blank characters.

  13. data members data members constructors const int num_chars = 28; struct Trie_node { Record *data; Trie_node *branch[num_chars]; Trie_node(); }; class Trie { public: Trie(); bool empty(); Error_code trie_search(const Key &target, Record &x) const; Error_code insert(const Record &new_entry);

  14. void inorder(void (*visit)(Record *)); int size(); int height(); void clear(); ~Trie(); void depth_traverse(void (*visit)(Record *)); Error_code remove(const Key &target); private: Trie_node *root; };

  15. 4. Searching aTriespg.532 Terminate search for a blank in the target Move down the appropriate branch of the trie Terminate search for a NULL location Move to the next character of the target indexes letters of key Error_code Trie::trie_search(const Key &target, Record &x) /* Post: If the search is successful, a code of success is returned, and the output parameter x is set as a copy of the Trie's record that holds target. Otherwise, a code of not_present is returned. Uses: Methods of class Key. */ { int position = 0; char next_char; Trie_node *location = root; while (location != NULL && (next_char=target.key_letter(position)) != ' ') { location=location->branch[alphabetic_order(next_char)]; position++; }

  16. if (location != NULL && location->data != NULL) { x = *(location->data); return success; } else return not_present; } Create a new empty Trie 5. Insertion into aTriespg.533 Error_code Trie::insert(const Record &new_entry) /* Post: If the Key of new_entry is already in the Trie, a code of duplicate_error is returned. Otherwise, a code of success is returned and the Record new_entry is inserted into the Trie. Uses: Methods of classes Record and Trie_node.*/ { Error_code result = success; if (root==NULL) root = new Trie_node;

  17. int position = 0; char next_char; Trie_node *location = root; while (location != NULL && (next_char=target.key_letter(position)) != ' ') { int next_position = alphabetic_order(next_char); if (location->branch[next_position] == NULL) location->branch[next_position]=new Trie_node; location = location->branch[next_position]; position++; } if (location->data != NULL) result = duplicate_error; else location->data = new Record(new_entry); return result; } At this point, we have tested for all non blank characters of new_entry. indexes letters of new_entry moves through the Trie

  18. The number of steps required to search a trie or insert into it is proportional to the number of characters making up a key, not to a logarithm of the number of keys as in other tree-based searches.

  19. 6. Deletion from aTriespg.533 Error_code Trie::trie_search(const Key &target, Record &x) /* Post: If the search is successful, a code of success is returned, and the output parameter x is set as a copy of the Trie's record that holds target. Otherwise, a code of not_present is returned. Uses: Methods of class Key. */ { int position = 0; char next_char; Trie_node *location = root; while (location != NULL && (next_char=target.key_letter(position)) != ' ') { location=location->branch[alphabetic_order(next_char)]; position++; }

  20. 11.3 External Searching: B-Trees 阶 1. Multiway Search Trees  An m-way search tree is a tree in which, for some integer m called the orderof the tree, each node has at most m children.  If k≤m is the number of children, then the node contains exactly k-1 keys, which partition all the keys into k subsets consisting of all the keys less than the first key in the node, all the keys between a pair of keys in the node, and all keys greater than the largest key in the node.

  21. 定义:一颗m阶查找树,或者是一颗空树,或者是满足如下性质的m路查找树:定义:一颗m阶查找树,或者是一颗空树,或者是满足如下性质的m路查找树: 1)每个结点最多有m棵子树、m-1个关键字,其结构为: 其中n为关键字个数,Pi 为指向子树根结点的指针,Ki为关键字。 2) Ki<Ki+1, 3) 子树Pi中的所有关键字均大于Ki、小于Ki+1, 4) 子树P0中的关键字均小于Ki,而子树Pn中的所 有关键字均大于Kn。 5)子树Pi也是m阶查找树,0≤i≤n。

  22. 2. Balanced Multiway Trees (B-Trees) Definition A B-tree of order m is an m-way search tree in which 1. All leaves are on the same level. 2. All internal nodes except the root have at most m nonempty children, and at least nonempty children. 3. The number of keys in each internal node is one less than the number of its nonempty children, and these keys partition the keys in the children in the fashion of a search tree. 4. The root has at most m children, but may have as few as 2 if it is not a leaf, or none if the tree consists of the root alone.

  23. 定义:一棵m阶的B-树,或为空树,或为满足下列特性的m叉树:定义:一棵m阶的B-树,或为空树,或为满足下列特性的m叉树: ① 每个结点至多有m棵子树; ② 除根结点和叶结点外,其它每个结点至少有m/2棵子树; ③ 除非根结点为最低层非终端结点,否则至少有两棵子树; ④ 所有非终端结点中包含有下列信息: (n,A0,k1,A1,K2,A2,…,Kn,An) 其中,Ki(1 i n)为关键字,且Ki<Ki+1(1 i <n),Ai(0 i n)为指向子树根结点的指针,且指针Ai(0 i<n)所指子树中所有结点的关键字均小于Ki+1,指针An所指子树中的关键字均大于Kn,n(n= m/2  m-1)为关键字的个数; ⑤所有的叶子结点都出现在同一层上,并且不带信息(可以看作是外部结点或查失败的结点,实际上这些结点不存在,指向这些结点的指针为空)。

  24. 3. Insertion into a B-Tree In contrast to binary search trees, B-trees are not allowed to grow at their leaves; instead, they are forced to grow at the root. General insertion method: 1. Search the tree for the new key. This search (if the key is truly new) will terminate in failure at a leaf. 2. Insert the new key into to the leaf node. If the node was not previously full, then the insertion is finished. 3. When a key is added to a full node, then the node splits into two nodes, side by side on the same level, except that the median key is not put into either of the two new nodes.

  25. 4. When a node splits, move up one level, insert the median key into this parent node, and repeat the splitting process if necessary. 5. When a key is added to a full root, then the root splits in two and the median key sent upward becomes a new root. This is the only time when the B-tree grows in height. Growth of a B-Tree The growth of a B-Tree of order 5 pg.538 Fig.11.10

  26. 4. B-Tree Declarations in C++ We add the order as a second template parameter. For example, B_tree<int, 5> sample tree; declares sample tree as a B_tree of order 5 that holds integer records. B_tree class declaration: template <class Record, int order> class B_tree { public: // Add public methods. private: // data members B_node<Record, order> *root; // Add private auxiliary functions here. };

  27. B-Tree Node declaration: 记录数 记录数组 指针数组 template <class Record, int order> struct B_node { B_node( ); private: // data members: int count; Record data[order-1]; B_node<Record, order> *branch[order]; }; Conventions: •count gives the number of records in the B_node.

  28. •If count is nonzero then the node has count+1 children. • branch[0] points to the subtree containing all records with keys less than that in data[0]. •For 1  position  count-1, branch[position] points to the subtree with keys strictly between those in the subtrees pointed to by data[position-1] and data[position]. •branch[count] points to the subtree with keys greater than that of data[count-1]. 参看“幻灯片25”中文定义。 5. Searching and insertion pg.539

  29. template <class Record, int order> Error_code B_tree<Record, order>:: search_tree(Record &target) { B_node<Record, order> *sub_root=root; while(sub_root != NULL) { int position; if (search_node(sub_root, target, position) == not_present ) sub_root=sub_root->branch[position]; else { target = sub_root->data[position]; return success; } } return not_present; }

  30. template <class Record, int order> Error_code B_tree<Record, order>::search_node ( B_node<Record, order> *current, const Record &target, int &position) {position=0; while(position<current->count && target>current->data[position]) position++; if ( position<current->count && target==current->data[position] ) return success; else return not present; } Perform a sequential search through the keys 引用参数 带回target 在结点中的位置 • For B-trees of large order, this function should be modified to use binary search instead of sequential search. •Another possibility is to use a linked binary search tree instead of a sequential array of entries for each node.

  31. Insertion: Parameters and push down • Insertion is done with recursion in a function called push down. • We require that the record new_entry being inserted is not already present in the tree. The recursive function push_down uses three more output parameters. • current is the root of the current subtree under consideration. • If *current splits to accommodate new entry, push down returns a code of overflow, and the following come into use: •The old node *current contains the left half of the entries. •median gives the median record. •right branch points to a new node containing the right half of the former *current.

  32. Public Insertion Method

  33. template <class Record, int order> Error_code B_tree<Record, order>::insert(const Record &new_entry) { Record median; B_node<Record, order> *right_branch, *new_root; Error_code result = push_down(root, new_entry, median, right_branch); if ( result==overflow) { new_root = new B_node<Record, order>; new_root->count=1; new_root->data[0]=median; new_root->branch[0]=root; new_root->branch[1]=right branch; root=new_root; result = success; } return result; } The whole tree grows in height Make a brand new_root for the whole B-tree

  34. Recursive Insertion into a Subtree Since we can’t insert in an empty tree, the recursion terminates. template <class Record, int order>Error_code B_tree<Record, order>::push_down ( B_node<Record, order> *current, const Record &new_entry, Record &median, B_node<Record, order>* &right_branch ) { Error_code result; int position; if(current==NULL) { median=new_entry; right_branch= NULL; result = overflow; } else { if (search_node(current, new_entry, position)==success) result=duplicate_error; else Search the current node.

  35. Record extra_entry now must be added to current Record median and its right branch will go up to a higher node { Record extra_entry; B_node<Record, order> *extra_branch; result=push_down(current->branch[position], new_entry, extra_entry, extra branch); if( result==overflow) if(current->count<order-1) { result=success; push_in(current, extra_entry, extra_branch, position); } else split_node( current, extra_entry, extra_branch, position, right_branch, median); } return result; }

  36. template <class Record, int order> void B_tree<Record, order>:: push_in ( B_node<Record, order> *current , const Record &entry , B_node<Record, order> *right_branch , int position )

  37. /* Pre: current points to a node of a B_tree . The node *current is not full and entry belongs in *current at index position . Post: entry has been inserted along with its right-hand branch right_branch into *current at index position. */ { for(int i=current->count; i>position; i--) { current->data[i] = current->data[i-1]; current->branch[i+1] = current->branch[i]; } current->data[position] = entry; current->branch[position+1] = right_branch; current->count++; } Shift all later data to the right

  38. Example of Splitting a Full Node

  39. See pg.547 fig.11.13

  40. Function split node, Action new_entry to insert Point to subtree on right of extra_entry Point to node to be split index in node where extra_entry goes Point to new node for right_half of entries median entry (in neither half) template <class Record, int order> void B_tree<Record, order>::split_node ( B_node<Record, order> *current, const Record &extra_entry, B_node<Record, order> *extra_branch, int position, B_node<Record, order> * &right_half, Record &median ) /* Pre: current points to a node of a B_tree. The node *current is full, but if there were room, the record extra_entry with its right-hand pointer extra_branch would belong in *current at position position ,0 position < order . Post: The node*current with extra_entry and pointer extra_branch at position position are divided into nodes *current and *right_half separated by a Record median. */

  41. { right_half = new B_node<Record, order>; int mid = order/2; if ( position <= mid ) { for(int i=mid; i<order-1; i++) { right_half->data[i-mid] = current->data[i]; right_half->branch[i+1-mid] = current->branch[i+1]; } current->count = mid; right_half->count = order-1-mid; push_in(current, extra_entry, extra_branch, position); } else { mid++; for(int i=mid; i<order-1; i++) Temporarily leave the median in left half Second case: extra_entry belongs in right_half. Move entries to right_half The entries from mid on will go to right_half First case: extra_entry belongs in left half Move entries to right_half

  42. { right_half->data[i-mid] = current->data[i]; right_half->branch[i+1-mid] = current->branch[i+1]; } current->count = mid; right_half->count = order-1-mid; push_in(right_half, extra_entry, extra_branch, position-mid); } median = current->data[current->count-1]; right_half->branch[0] = current->branch[current->count]; current->count--; } Remove median from left half Remove median from left half Remove median from left half

  43. 6. Deletion from a B-Tree pg.548 •If the entry that is to be deleted is not in a leaf, then its immediate predecessor (or successor) under the natural order of keys is guaranteed to be in a leaf. •We promote the immediate predecessor (or successor) into the position occupied by the deleted entry, and delete the entry from the leaf. •If the leaf contains more than the minimum number of entries, then one of them can be deleted with no further action. •If the leaf contains the minimum number, then we first look at the two leaves (or, in the case of a node on the outside,one leaf) that are immediately adjacent to each other and are children of the same node. If one of these

  44. has more than the minimum number of entries, then one of them can be moved into the parent node, and the entry from the parent moved into the leaf where the deletion is occurring. •If the adjacent leaf has only the minimum number of entries,then the two leaves and the median entry from the parent can all be combined as one new leaf, which will contain no more than the maximum number of entries allowed. •If this step leaves the parent node with too few entries, then the process propagates upward. In the limiting case, the last entry is removed from the root, and then the height of the tree decreases.

  45. pg.549 fig.11.14

More Related