1 / 27

m -ary Trees

m -ary Trees. m -ary trees. Some trees need to be searched efficiently, but have more than two children parse trees game trees genealogical trees, etc. DISADVANTAGE: different directions that a search path may follow (rather than 2: left if less, right if greater) ADVANTAGE:

sitara
Download Presentation

m -ary 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. m-ary Trees

  2. m-ary trees Some trees need to be searched efficiently, but havemore than two children • parse trees • game trees • genealogical trees, etc. DISADVANTAGE: different directions that a search path may follow (rather than 2: left if less, right if greater) ADVANTAGE: Shorter search paths because there are fewer levels in the tree.

  3. m-Node An m-node in a search tree stores m - 1 data values k1 < k2, ... < km-1, (ascending order) and has links to m sub-trees T1, ... , Tm, where for each i, all data values in Ti < ki<= all data values in Ti+1 Example 3-Node

  4. 2-3-4 tree 2-3-4 tree is a tree with the following properties: 1. Each node stores at most 3data values (and four links) 2. Each internal node is a 2-node, a 3-node, or a 4-node 3. All the leaves are on the same level. Basic Operations: • Construct • Determine if empty • Search • insert a new item in the 2-3-4 tree so result is 2-3-4 tree • delete an item from the 2-3-4 tree so result is 2-3-4 tree

  5. 2-3-4 Tree Example http://www.cse.ohio-state.edu/~bondhugu/acads/234-tree/index.shtml

  6. Wasted Links in 2-3-4 trees Each node must have one link for each possible child, even though most nodes will not use all these links. The amount of “wasted” space may be quite large. IF a 2-3-4 tree has n nodes, linked representation requires 4 links for each node, 4n links. Only n – 1 of these links are used to connect n nodes. 4n - (n - 1) = 3n + 1 of the links are null unused links is (3n +1) / 4n, 75% of the links

  7. Red-Black Trees

  8. Red-Black Tree • A special kind of binary search tree • Using recoloring and AVL-like rotations to maintain height • Used to represent 2-3-4 trees • Without the disadvantage of wasted space for unused links

  9. Red-black trees A binary search tree with two kinds of links (nodes): red and black, which satisfies the following properties:

  10. Red-Black Insertions See Tutorial http://www.csanimated.com/animation.php?t=Red-black_tree

  11. http://www.youtube.com/watch?v=hm2GHwyKF1o min 40 Insertions • Inserted nodes are red • Only possible violation: • Two sequential red nodes • Violating property 3

  12. Red-black trees • http://people.ksp.sk/~kuko/bak/index.html BEST • http://www.ececs.uc.edu/~franco/C321/html/RedBlack/redblack.html • http://www.ibr.cs.tu-bs.de/lehre/ss98/audii/applets/BST/redblack.html • Insert the following nodes: • 55 25 77 11 44 50 98 66 88 5 8 3

  13. Red-black tree class enum ColorType {RED, BLACK}; class RedBlackTreeNode { public: TreeElementType data; ColorType parentColor; RedBlackTreeNode *parent, *left, *right; };

  14. AVL rotations AVL trees have been replaced in many apps by 2-3-4 or red-black trees AVL rotations are still used to keep a red-black tree balanced. To construct a red-black tree, use top-down 2-3-4 tree insertion with 4-node splitting during descent: • 1.    Search for a place to insert the new node. (Keep track of parent, grandparent, and great gp). 2. When 4-node q found along the search path, split it as follows: a. Change both links of q to black. b. Change the link from the parent to red: 3.  If there now are two consecutive red links (from grandparent gp to parent p to q), perform the appropriate AVL-type rotation as determined by the direction (LL, RR, LR, RL)

  15. Associative Container • A container that allows access of • its elements using an index or a key

  16. STL Associative Containers • set • multiset • map • multimap • All implemented with red-black trees • multi- allows multiple occurrences of object

  17. STL map • // key data • map<string, Student > map1; • STL map documentation • See map.cpp

  18. STL set • // data ordering function • set<string> C; • STL set documentation • See set.cpp

More Related