1 / 49

# Advanced Trees Part I - PowerPoint PPT Presentation

Advanced Trees Part I. Briana B. Morrison Adapted from Alan Eugenio & William J. Collins. Topics. Part I General Trees Multi-way Search Trees 2-3 Search Trees (and B trees) Tries* Part II AVL Trees Part III 2-3-4 Search Trees Red Black Trees. General Trees.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Briana B. Morrison

William J. Collins

• Part I

• General Trees

• Multi-way Search Trees

• 2-3 Search Trees (and B trees)

• Tries*

• Part II

• AVL Trees

• Part III

• 2-3-4 Search Trees

• Red Black Trees

• Implementations of Ordered Trees

• Correspondence with binary trees

• An m-way search tree is a tree in which, for some integer m called the order of the tree, each node has at most m children.

• If k <= m is the number of children, then all the nodes contain 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.

• A B-tree of order m is an m-way search tree in which

• All leaves are on the same level.

• All internal nodes except the root have at most m non-empty children, and at least ceil(m/2) non-empty children.

• The number of keys in each internal node is one less than the number of its non-empty children, and these keys partition the keys in the children in the fashion of a search tree.

• 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.

• Remember that performance is related to the height of the tree

• We want to minimize the height of the tree

• Used to process external records (information too large to put into memory), minimizes number of accesses to secondary peripheral

• B-Tree of degree 3

• Balanced search tree

• Either the tree is empty or T has two children:

• root contains 1 data item

• r is greater than each value in left subtree

• r is less than each value in right subtree

• T is of the form: root, left subtree, middle subtree, right subtree

• r has two data items

• smaller value in r is greater than everything in left subtree and smaller than everything in middle subtree

• larger value in r is greater than everything in middle subtree and small than everything in right subtree

50, 90

20 70 120, 150

10 30, 40 60 80 100, 110 130,140 160

Nodes with 2 children must have 1 item

Nodes with 3 children must have 2 items

Leaves may contain 1 or 2 items

50, 90

20 70 120, 150

10 30, 40 60 80 100, 110 130, 140 160

Inorder traversal is same as BST,

But have to also take care of two value nodes and middle trees.

50, 90

20 70 120, 150

10 30, 40 60 80 100, 110 130, 140 160

Find the value 80

Find the value 140

Find the value 95

• Find where it would belong

• If leaf has 1 value, add as second value

• If leaf has 2 values:

• split into leaves

• give parent middle value

• if parent now has 3 values,

• split, and move middle value up

Insert 30…

Now insert 70…

• Locate

• If internal node, find successor and swap

• if leaf contains more than 1 value, delete

• else if left has 1 value, but sibling contains more than 1 value, redistribute values

• if no sibling has 2 values, merge and bring down value and recurse

• Name comes from middle letters of word “retrieval”, rhymes with “pie”

• Represents strings of any type such as characters

• Each path from root to a leaf is one word

• To avoid confusion, can use endmarker symbol \$

After preprocessing the pattern, KMP’s algorithm performs pattern matching in time proportional to the text size

If the text is large, immutable and searched for often (e.g., works by Shakespeare), we may want to preprocess the text instead of the pattern

A trie is a compact data structure for representing a set of strings, such as all the words in a text

A tries supports pattern matching queries in time proportional to the pattern size

Preprocessing Strings

• A trie of order m is either empty or consists of an ordered sequence of exactly m tries of order m.

S

T

I

H

I

E

I

N

N

\$

S

N

N

\$

G

\$

\$

\$

\$

\$

• The standard trie for a set of strings S is an ordered tree such that:

• Each node but the root is labeled with a character

• The children of a node are alphabetically ordered

• The paths from the external nodes to the root yield the strings of S

• Example: standard trie for the set of strings

S = { bear, bell, bid, bull, buy, sell, stock, stop }

• A standard trie uses O(n) space and supports searches, insertions and deletions in time O(dm), where:

n total size of the strings in S

m size of the string parameter of the operation

d size of the alphabet

• We insert the words of the text into a trie

• Each leaf stores the occurrences of the associated word in the text

• A compressed trie has internal nodes of degree at least two

• It is obtained from standard trie by compressing chains of “redundant” nodes

• The suffix trie of a string X is the compressed trie of all the suffixes of X

d

e

b

c

Encoding Trie (1)

• A code is a mapping of each character of an alphabet to a binary code-word

• A prefix code is a binary code such that no code-word is the prefix of another code-word

• An encoding trie represents a prefix code

• Each leaf stores a character

• The code word of a character is given by the path from the root to the leaf storing the character (0 for a left child and 1 for a right child

a

d

b

b

r

a

c

r

d

Encoding Trie (2)

• Given a text string X, we want to find a prefix code for the characters of X that yields a small encoding for X

• Frequent characters should have long code-words

• Rare characters should have short code-words

• Example

• T1 encodes X into 29 bits

• T2 encodes X into 24 bits

T1

T2

AlgorithmHuffmanEncoding(X)

Inputstring X of size n

Outputoptimal encoding trie for X

C distinctCharacters(X)

computeFrequencies(C, X)

Qnew empty heap

for all c  C

Tnew single-node tree storing c

Q.insert(getFrequency(c), T)

while Q.size()> 1

f1 Q.minKey()

T1 Q.removeMin()

f2 Q.minKey()

T2 Q.removeMin()

Tjoin(T1, T2)

Q.insert(f1+ f2, T)

return Q.removeMin()

• Given a string X, Huffman’s algorithm construct a prefix code the minimizes the size of the encoding of X

• It runs in timeO(n + d log d), where n is the size of X and d is the number of distinct characters of X

• A heap-based priority queue is used as an auxiliary structure

4

a

c

d

b

r

5

6

a

b

c

d

r

2

4

5

2

1

1

2

11

a

c

d

b

r

6

a

5

2

2

4

a

b

c

d

r

c

d

b

r

5

2

2

Example

Frequencies

• Every record has a Key that is an alphanumeric string.

class Trie {

public: …

private:

Trie_node *root;

};

struct Trie_node {

Record *data;

Trie_node *branch[num_chars];

// or vector

};