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

Post on 04-Jan-2016

221 views 1 download

Tags:

Transcript of M-ary Trees. m-ary trees Some trees need to be searched efficiently, but have more than two children...

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

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

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

2-3-4 Tree Example

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

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

Red-Black 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

Red-black trees

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

which satisfies the following properties:

Red-Black Insertions

See Tutorial

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

Insertions

Inserted nodes are red Only possible violation:

– Two sequential red nodes • Violating property 3

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

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

Red-black tree class

enum ColorType {RED, BLACK};

class RedBlackTreeNode

{

public:

TreeElementType data;

ColorType parentColor;

RedBlackTreeNode

*parent, *left, *right;

};

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)

Associative Container

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

STL Associative Containers

set multiset map multimap

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

STL map

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

STL map documentation

See map.cpp

STL set

// data ordering function

set<string> C;

STL set documentation

See set.cpp