m-ary trees. m-ary trees some trees need to be searched efficiently, but have more than two children...

27
m-ary Trees

Upload: valentine-montgomery

Post on 04-Jan-2016

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 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,

m-ary Trees

Page 2: 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,

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:Shorter search paths because there are fewer levels in the tree.

Page 3: 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,

m-NodeAn 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 Ti+1

 

Example 3-Node

Page 4: 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,

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: 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

Page 5: 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,

2-3-4 Tree Example

http://www.cse.ohio-state.edu/~bondhugu/acads/234-tree/index.shtml

Page 6: 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,
Page 7: 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,
Page 8: 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,
Page 9: 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,
Page 10: 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,

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

Page 11: 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,

Red-Black Trees

Page 12: 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,

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

Page 13: 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,

Red-black trees

A binary search tree with two kinds of links (nodes): red and black,

which satisfies the following properties:

Page 14: 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,
Page 15: 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,
Page 16: 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,
Page 17: 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,
Page 18: 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,

Red-Black Insertions

See Tutorial

http://www.csanimated.com/animation.php?t=Red-black_tree

Page 19: 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,

Insertions

Inserted nodes are red Only possible violation:

– Two sequential red nodes • Violating property 3

http://www.youtube.com/watch?v=hm2GHwyKF1o min 40

Page 20: 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,

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

Page 21: 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,

Red-black tree class

enum ColorType {RED, BLACK};

class RedBlackTreeNode

{

public:

TreeElementType data;

ColorType parentColor;

RedBlackTreeNode

*parent, *left, *right;

};

Page 22: 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,

AVL rotationsAVL 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)

Page 23: 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,

Associative Container

A container that allows access of its elements using an index or a key

Page 24: 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,

STL Associative Containers

set multiset map multimap

All implemented with red-black trees multi- allows multiple occurrences of object

Page 25: 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,

STL map

// key data map<string, Student > map1;

STL map documentation

See map.cpp

Page 26: 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,

STL set

// data ordering function

set<string> C;

STL set documentation

See set.cpp

Page 27: 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,