- By
**tarak** - Follow User

- 167 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about ' Advanced Trees Part I' - tarak

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

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

Advanced Trees (Part I)

General Trees

- Implementations of Ordered Trees
- Multiple links
- first_child and next_sibling links
- Correspondence with binary trees

Advanced Trees (Part I)

Linked Implementation

Advanced Trees (Part I)

Rotated Form

first_child (left) links: black

next_sibling (right) links: red

Advanced Trees (Part I)

Multiway Search 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.

Advanced Trees (Part I)

Example Multiway Search Tree

Advanced Trees (Part I)

Balanced Multiway Trees (B-Trees)

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

Advanced Trees (Part I)

Example B tree

Advanced Trees (Part I)

Why Use B-Trees?

- 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

Advanced Trees (Part I)

2-3 Search Trees T is of the form: root, left subtree, middle subtree, right subtree

- 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

- 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

Advanced Trees (Part I)

Example

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

Advanced Trees (Part I)

Traversal

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.

Advanced Trees (Part I)

Retrieval

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

Advanced Trees (Part I)

Insertion into 2-3 Search Tree

- 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

Advanced Trees (Part I)

Advanced Trees (Part I)

Advanced Trees (Part I)

Advanced Trees (Part I)

Advanced Trees (Part I)

Advanced Trees (Part I)

Deletion from a 2-3 Tree

- 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

Advanced Trees (Part I)

Advanced Trees (Part I)

Advanced Trees (Part I)

Advanced Trees (Part I)

Tries – Lexicographical Search Tree

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

Advanced Trees (Part I)

Preprocessing the pattern speeds up pattern matching queries

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 StringsAdvanced Trees (Part I)

Definition of Trie

- 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

$

$

$

$

$

Advanced Trees (Part I)

Standard Trie (1)

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

Advanced Trees (Part I)

Standard Trie (2)

- 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

Advanced Trees (Part I)

Word Matching with a Trie

- We insert the words of the text into a trie
- Each leaf stores the occurrences of the associated word in the text

Advanced Trees (Part I)

Compressed Trie

- A compressed trie has internal nodes of degree at least two
- It is obtained from standard trie by compressing chains of “redundant” nodes

Advanced Trees (Part I)

Suffix Trie (1)

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

Advanced Trees (Part I)

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

Advanced Trees (Part I)

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
- X =abracadabra
- T1 encodes X into 29 bits
- T2 encodes X into 24 bits

T1

T2

Advanced Trees (Part I)

Huffman’s Algorithm

AlgorithmHuffmanEncoding(X)

Inputstring X of size n

Outputoptimal encoding trie for X

C distinctCharacters(X)

computeFrequencies(C, X)

Qnew empty heap

for all c C

Tnew 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()

Tjoin(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

Advanced Trees (Part I)

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

ExampleX= abracadabra

Frequencies

Advanced Trees (Part I)

C++ Trie Declarations

- 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

};

Advanced Trees (Part I)

Trie Performance

- The number of steps required to search a trie or insert into it is proportional to the number of characters making up a key, not to a logarithm of the number of keys as in other tree-based searches.

Advanced Trees (Part I)

Download Presentation

Connecting to Server..