chapter tow search trees by hussein salim qasim wesam hrbi fadheel cs 6310 advance data structure...

30
Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

Upload: shanna-oconnor

Post on 11-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

1

Chapter Tow Search Trees

BY

HUSSEIN SALIM QASIM WESAM HRBI FADHEEL

CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM

DR. ELISE DE DONCKER

Page 2: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

2Outline

2.6 Dealing with Non-Unique Keys

2.7 Queries for the Keys in an Interval

2.8 Building Optimal Search Trees

2.9 Converting Trees into Lists

2.10 Removing a Tree

Page 3: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

3

Complete Binary Tree :

All level except leaves are completely filled.

Max # of nodes @ level i=2^i

Height (h) with n nodes = log2 n

Perfect Binary Tree:

All levels completely full

Max # of nodes in tree with height h is = 2^0 + 2^2 +…. +2^h

=2^(h+1) -1

= 2^(# of levels)-1

Height (h) with n nodes = log2 (n+1) -1

( n = 2^(h+1) -1 2^(h+1) = (n+1) h = log2 (n+1) -1 )

Time complexity = O(h) O(log2 n)

Terminology:

Page 4: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

4

Balanced binary tree :

Difference between height of left

and right subtree for every node is

not more than 1.

Height of a node : # of edges in longest path from the node to a leaf node>

Depth of a node: # of edges in path from root to the node

Terminology:

Page 5: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

5Traversal Orders The three traversal orders are:

A. Breadth-first

B. Depth-first:

1- Preorder: <root><left><right>

Process the value in the current node first, then its left and right subtrees.

Page 6: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

6Traversal Orders

2- Inorder (symmetric): <left><root><right>

process the left subtree first, then the value in the current node, then its right subtree.

Page 7: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

7Traversal Orders

3- Postorder:<left><right><root>

process the left and right subtrees first, then the value in the current node.

Page 8: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

8Linked data structure

Linked list

Chains

Doubly linked

Page 9: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

92.6 Dealing with Non-Unique Keys Store many objects with the same key value.

Keep all elements of the same key in a linked list

The correct reaction is as follows:

1- Find : produces all elements of that list O(h + k)

k is the number of elements that find returns.

2- Insert: inserts at the beginning of the list O(h).

Page 10: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

102.6 Dealing with Non-Unique Keys3- Delete: deletes all items of that key in time O(h).

1- Delete the references to the objects :

Additional node between the leaf and the linked list.

( contains pointers to the beginning and to the end of the list)

Transfer the entire list with O(1) operations to the free list.

2- Delete the objects

walking along this list, but not in O(1).

(time independent of the # of objects)

Page 11: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

112.7 Queries for the Keys in an Interval We store all those branches on a stack.

We have two keys [a,b[ left and right .

Use left as a Binary search tree key.

Largest end point of subtree in right .

Page 12: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

12Insert Interval [a,b[

Insert into BST, using a as the key.

Update max in each node on search path.

Page 13: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

13Search Interval [a,b[ If interval in node intersects query interval, return it. interval [a,

b[ (lo,hi).

Else if left subtree is null, go right

Else if max endpoint in left subtree is less than a, go right

Else go left.

Page 14: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

14Search Interval [a,b[

Page 15: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

152.7 Queries for the Keys in an IntervalWe Have tow Methods:

1- Method One Change Tree Structure:

Need to organize the leaves into a doubly linked list

Moving between nodes cost O(1).

Need to change the insertion and deletion functions.

(cost only O(1) additional time)

Query takes O(k) additional time if it lists, a total of k keys in the interval.

Page 16: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

162.7 Queries for the Keys in an Interval2- Method Two Change Query Function:

Down the tree with the query interval instead of the query key. Search for any one interval they intersects query, If interval in node intersects query interval, return it.

interval [a, b[:

Else if left subtree is null, go right

Else if max endpoint in left subtree is less than a, go right

Else go left.

We have i + 1 leaves, when i interior nodes between paths,

total number of nodes visited is at most twice the # of nodes visited in a normal find operation plus O(k) for k leaves.

Page 17: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

17

Page 18: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

18

2.8 Building Optimal Search Trees

We can construct an optimal search tree from a given set of (key, object) pairs.

The search tree will be as static data structure: there are no inserts and deletes, In this case :

No problem for rebalancing the tree, But if we build it, knowing the data in advance.

The primary issue is the height

Because a search tree of height h has at most 2^h leaves, an optimal search tree for a set of n items has height (log n) , where the log, as always, is taken to base 2.

Page 19: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

19How to construct search tree with optimal height?

Bottom-up. Top-down.

Assuming that the (key, object) pairs are given in sorted list (increased order).

Page 20: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

20Bottom-Up method The construction of Bottom_Up is easy.

One views the initial list as a list of one element trees.

Then, one goes repeatedly through the list, joining two consecutive trees. Until there is only one tree left.

Assume here that:

the list items are themselves of type tree_node_t.

Left entry pointing to the object.

The key containing the object key.

The right entry pointing to the next item. Or NULL.

Create a list, where all the nodes of the previous list are attached as leaves.

Then maintain a list of trees, where the key value is the smallest key value in the tree below it.

Page 21: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

21

O(n)

Page 22: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

22

Page 23: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

23The Bottom_Up method is O(n)

The disadvantage of this method is that: the tree is unbalanced although it has optimal height.

The first half of the algorithm, duplicating the list and converting all the original list nodes to leaves, takes obviously O(n); it is just one loop over the length of the list.

The second half has a more complicated structure, but in each execution of the body of the innermost loop,

One of the n interior nodes created in the first half is removed from the current list and put into a finished subtree,

So the innermost part of the two nested loops is executed only n times.

Page 24: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

24Top-down method The top-down construction is easiest to describe recursively:

divide the data set in the middle,

create optimal trees for the lower and the upper halves,

And join them together.

This division is very balanced; in each subtree the number of items left and right differs by at most one, and it also results in a tree of optimal height.

But if we implement it this way, and the data is given as list, it

takes (n log n) time, because we get an overhead of (n) in each recursion step to

find the middle of the list. But there is a nice implementation with O(n) complexity using

a stack.

Page 25: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

25How does it works

First construct the tree “in the abstract,” without filling in any key values or pointers to objects.

Then, find the middle of the list; by keep tracking the number of elements that should go into the left and right subtrees.

Building abstract tree using a stack:

1. We initially put the root on the stack, labeled with the required tree size;

2. Then we continue, until the stack is empty, to take nodes from the stack, attach them to two newly created nodes labeled with half the size,

3. And put the new nodes again on the stack.

If the size reaches one, we have a leaf, so node should not be put back on the stack

but should be filled with the next key and object from the list.

The problem is to fill in the keys of the interior nodes, which become available only when the leaf is reached.

Each item on the stack needs two pointers,

One to the node that still needs to be expanded and one to the node higher up in the tree, where the smallest key of leaves below that node should be inserted as comparison key.

Also, each stack item contains a number, the number of leaves that should be created below that node.

Page 26: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

26The Top_Down method is O(n) To analyze this algorithm:

In each step on the stack,

we create either two new nodes, and there are only n − 1 new nodes created in total,or we attach a list item as leaf,

and there are only n list items.

So the total complexity is O(n).

Page 27: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

27

Page 28: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

282.9 Converting Trees into Lists

Using a trivial depth-first search (Stack).

Converts tree of n leaves into a list of n elements.

Converting time complexity is O(n)

Using array as a preferred method If height of tree is not too large.

Array size needs to be at least as large as the height of the tree

Page 29: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

292.10 Removing a Tree

No longer need it

To avoid a memory leak

Time to Remove a tree structure related to the size.

Rotations in the root till the left-lower neighbor is a leaf;.

Returns that leaf.

Moves the root down to the right,

Returns the previous root.

Page 30: Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

30

Thanks