Presentation is loading. Please wait.

Presentation is loading. Please wait.

M-ary Trees. m-ary trees Some trees need to be searched efficiently, but have more than two children l parse trees l game trees l genealogical trees,

Similar presentations


Presentation on theme: "M-ary Trees. m-ary trees Some trees need to be searched efficiently, but have more than two children l parse trees l game trees l genealogical trees,"— Presentation transcript:

1 m-ary Trees

2 m-ary trees Some trees need to be searched efficiently, but have more than two children l parse trees l game trees l 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 k 1 < k 2,... < k m-1, (ascending order) and has links to m sub-trees T 1,..., T m, where for each i, all data values in T i < k i <= all data values in T i+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 3 data 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: l Construct Determine if empty Search l insert a new item in the 2-3-4 tree so result is 2-3-4 tree l 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

7

8

9

10 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

11 Red-Black Trees

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

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

14

15

16

17

18 Red-Black Insertions See Tutorial http://www.csanimated.com/animatio n.php?t=Red-black_tree

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

20 Red-black trees l http://people.ksp.sk/~kuko/bak/index.html BEST http://people.ksp.sk/~kuko/bak/index.html BEST l http://www.ececs.uc.edu/~franco/C321/html/RedBlack/redblack.html http://www.ececs.uc.edu/~franco/C321/html/RedBlack/redblack.html l http://www.ibr.cs.tu-bs.de/lehre/ss98/audii/applets/BST/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

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

22 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. 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)

23 Associative Container l A container that allows access of l its elements using an index or a key

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

25 STL map l // key data l map map1; l STL map documentation STL map documentation l See map.cpp

26 STL set l // data ordering function set C; l STL set documentation STL set documentation l See set.cpp

27

28

29

30

31

32

33


Download ppt "M-ary Trees. m-ary trees Some trees need to be searched efficiently, but have more than two children l parse trees l game trees l genealogical trees,"

Similar presentations


Ads by Google