We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Introduction to Computer Science 2 Lecture 7: Extended binary trees
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
Chapter 19: Searching and Sorting Algorithms
CS Data Structures Chapter 10 Search Structures (Selected Topics)
BST Data Structure A BST node contains: A BST contains
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
E.G.M. Petrakissearching1 Searching  Find an element in a collection in the main memory or on the disk  collection: (K 1,I 1 ),(K 2,I 2 )…(K N,I N )
Chapter 10 Search Structures Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures.
Optimal binary search trees
E.G.M. PetrakisTrees in Main Memory1 Balanced BST  Balanced BSTs guarantee O(logN) performance at all times  the height or left and right sub-trees are.
Index Structures Parin Shah Id:-207. Topics Introduction Structure of B-tree Features of B-tree Applications of B-trees Insertion into B-tree Deletion.
Binary Trees Chapter 6.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
Recursion and Binary Tree ICS 51 – Introductory Computer Organization.
CHAPTER 71 TREE. Binary Tree A binary tree T is a finite set of one or more nodes such that: (a) T is empty or (b) There is a specially designated node.
Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1.
MA/CSSE 473 Days Optimal BSTs. MA/CSSE 473 Days Student Questions? Expected Lookup time in a Binary Search Tree Optimal static Binary Search.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
CS Data Structures Chapter 10 Search Structures.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
MA/CSSE 473 Days Optimal linked lists Optimal BSTs Greedy Algorithms.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Data Structures and Algorithms Lecture (BinaryTrees) Instructor: Quratulain.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
COSC 2007 Data Structures II Chapter 15 External Methods.
CSC 211 Data Structures Lecture 13
CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
1 Searching Searching in a sorted linked list takes linear time in the worst and average case. Searching in a sorted array takes logarithmic time in the.
AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.
Prepared by- Jatinder Paul, Shraddha Rumade
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Chapter 12 B+ Trees CS 157B Spring 2003 By: Miriam Sy.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
Foundation of Computing Systems
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Internal and External Sorting External Searching
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
MA/CSSE 473 Day 30 Optimal BSTs. MA/CSSE 473 Day 30 Student Questions Optimal Linked Lists Expected Lookup time in a Binary Tree Optimal Binary Tree (intro)
MA/CSSE 473 Days Optimal linked lists Optimal BSTs.
Data Structures and Algorithms Instructor: Tesfaye Guta [M.Sc.] Haramaya University.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Chapter 25 Binary Search Trees
Binary Search Trees A binary search tree is a binary tree
Advanced Algorithms Analysis and Design
Binary Search Tree (BST)
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
Binary Search Tree Chapter 10.
Tree data structure.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Tree data structure.
B- Trees D. Frey with apologies to Tom Anastasio
Chapter 15: Dynamic Programming III
B- Trees D. Frey with apologies to Tom Anastasio
B- Trees D. Frey with apologies to Tom Anastasio
Optimal Binary Search Tree. 1.Preface  OBST is one special kind of advanced tree.  It focus on how to reduce the cost of the search of the BST.  It.
Presentation transcript:

We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt

Our new topic

Optimal Binary search Trees

In this chapter there is no insertion no deletion Today we `ll need only one tree its possible combination so that we can find out the best one from it

For this we need o know what are binary search trees We`ll get a quick review about the binary search trees

Binary search trees are simple binary trees with only difference that it is a sorted binary tree The root may contain any value But the left subtree contains value less than the root value And the right sub tree contains value greater than the root value And left and right subtree are itself binary search trees

A sorted list (array) can be searched by using binary search We divide the list in half and search And we divide it again and repeat the process Greater than 5less than 6 Comparing bst with sorted array

Less than 4 Greater than 4 Searching in a binary search tree

To search a tree we have two methods 1. Itersearch (which is the iteration method) 2. search (which is a recurrsive function) Itersearch is similar to binary search

Suppose we take a binary tree on a sorted list (5,10,15) Although this tree is full it may not be a optimal bst What if I never search for 10 but only for 15 ……., I have to do 2 comparisions all the time Soo itz not optimal for my requirement

Possible bst Root 10 Root 5 Root 15 Root 5

If U R given a set of nos {5, 10, 15, 20, 25} there are many binary search trees that can b formed For eg Fig a Fig b

Give your opinion ! So which one tree will be the most optimal(desirable) for any search ????? Fig a Fig b

Whatever may be your answer Itz wrong !!!!!! B`coz` we cant decide it until we know the probablity that how much times a number is searched

Difference between fig a and fig b * Fig a This tree requires atmost 4 comparisions Fig b This tree requires atmost 3 comparisions If we consider the worst case i.e. for 15 fig.b is more desirable *Considering each element has equal probablity *every search is a successful search Compr 1 Compr 2 Compr 3 Compr 4 Fig.a Fig.b

Fig a 1 st comparison with 10 2 nd with 25 3 rd with 20 4 th with 15 Total 4 comparisons Avg no. of comparisons = Fig b 1 st comparison with 10 2 nd with 20 3 rd with 15 Total 3 comparisons Avg no. of comparisons = Hence for equal probability Fig.b is more desireable

If probablity of the elements are different ? P(5) =0.3 (prob of searching 5) P(10)=0.3 (prob of searching 10) P(15)=0.05 (prob of searching 15) P(20)=0.05 (prob of searching 20) P(25)=0.3 (prob of searching 25)

Fig a. Avg no of comparisons =1.85 Fig a has low cost Fig. b Avg no of comparisons =2.05 Fig b has more cost Soo the probability of searching a particular element does affects the cost Now fig a seems to be desirable

Now we understood why we need and optimal bst Starting with our topic OBST

When dealing with obst An optimal binary search tree is a binary search tree for which the nodes are arranged on levels such that the tree cost is minimum An optimal binary search tree is a binary search tree for which the nodes are arranged on levels such that the tree cost is minimum In each binary tree there are NULL links at the leaf node, and they are denoted by square nodes A tree with n nodes will have (n+1) NULL links

The square nodes are called as External nodes, b`coz`they are not a part of the tree The inner round nodes are called as Internal nodes Each time we search a element which is not in the tree the search ends at External nodes Hence external nodes are also called as failure nodes A tree with external nodes is called as extended binary tree

Extended binary trees Fig bFig a

Path length affects the cost Internal path length: - sum of path length of each internal node External path length: - sum of path length of each external node

L=0 L=1 L=2 L=3 L=4 Internal path length I= =7 External path length E= =17 E=I+2 (no of nodes) Tree with max E will have max I

1 – the key is found, so the corresponding weight ‘p’ is incremented; 2 – the key is not found, so the corresponding ‘q’value is incremented.

Cost of a bst when the searches are successful Probability of node i Level of node i

Cost of a bst when the searches are unsuccessful

Total cost

Understanding obst with an example

k2 k1k4 k3k5 d0d1 d2d3d4d5 k2 k1k5 k4 k3 d0d1 d2d3 d4 d5 Figure (a) i Pi Knode Qi dnode Figure (b)

Node#Depthprobabilitycost k k k k K d d d d d d Cost= Probability * (Depth+1) We can calculate the expected search cost node by node: Cost= Probability * (Depth+1)

And the total cost = ( ) = 2.80 (Fig a) So Figure (a)(complete bst) costs 2.80,on another, the Figure (b) costs 2.75, and that tree is really optimal.

We can see the height of (b) is more than (a), and the key k5 has the greatest search probability of any key, yet the root of the OBST shown is k2.(The lowest expected cost of any BST with k5 at the root is 2.85) k2 k1k5 k4 k3 d0d1 d2d3 d4 d5 Figure (b)

So itz not necessary that always the key with highest probablityshould be the root

Going in depth of obst

Property of an obst Sub tree Sub tree Sub tree Sub tree Sub tree Sub tree Sub tree Sub tree Optimal

To find the OBST, our idea is to decide its root, and also the root of each subtree To help our discussion, we define : Ei,j = expected time searching keys in (k i ; d j) Real nodes from 1 - 5dummy nodes from 0 - 5

Deciding Root of OBST E[i,j] = minr { Ei,r-1 + Er+1,j + wi,j } Here r lies between i and j Corollary: Let r be the parameter that minimizes { Ei,r-1 + Er+1,j + wi,j } Then the root of the OBST for keys ( ki, ki+1, …, kj; di-1, di, …, dj ) should be set to kr

Computing E i,j Define a function Compute_E(i,j) as follows: Compute_E(i, j) /* Finding Ei,j */ 1. if (i == j+1) return qj; /* Exp time with key dj */ 2. min = 1; 3. for (r = i, i+1, …, j) { g = Compute_E(i,r-1) + Compute_E(r+1,j) + wi,j ; if (g <min) min = g; } 4. return min ;

Remarks A slight change in the algorithm allows us to get the root of each subtree, and thus the structure of OBST (how?) The powerful technique of storing computed is called Dynamic Programming Knuth observed a further property so that we can compute OBST in O(n2) time (search wiki for more information)

Any questions?

Thank Queue