we learnt what are forests, trees, bintrees, binary search trees. and some operation on the tree...
TRANSCRIPT
![Page 1: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/1.jpg)
![Page 2: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/2.jpg)
![Page 3: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/3.jpg)
We learnt what are forests, trees, bintrees, binary search trees.
And some operation on the tree i.e. insertion, deletion, traversing
What we learnt
![Page 4: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/4.jpg)
Our new topic
![Page 5: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/5.jpg)
Optimal Binary search
Trees
![Page 6: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/6.jpg)
• 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
![Page 7: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/7.jpg)
• For this we need o know what are binary search trees
• We`ll get a quick review about the• binary search trees
![Page 8: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/8.jpg)
• 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
![Page 9: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/9.jpg)
• 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
1 2 3 4 5 6 7 8 9 10
Greater than 5less than 6
Comparing bst with sorted array
![Page 10: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/10.jpg)
4
62
71 3 5
Less
than
4Greater than 4
Searching in a binary search tree
![Page 11: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/11.jpg)
• 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
![Page 12: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/12.jpg)
Suppose we take a binary tree on a sorted list (5,10,15)
5 15
10
• 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
![Page 13: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/13.jpg)
10
5
15
15
5
10
5
15
10
10
15
5
10
5 15
Possible bstRoot 10
Root 5 Root 15
Root 15
Root 5
![Page 14: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/14.jpg)
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
15 25
5 20
10
5 25
10
15
20
Fig a Fig b
![Page 15: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/15.jpg)
Give your opinion !• So which one tree will be the most
optimal(desirable) for any search ?????
15 25
5 20
10
5 25
10
15
20
Fig a Fig b
![Page 16: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/16.jpg)
•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
![Page 17: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/17.jpg)
Difference between fig a and fig b* Fig a • This tree requires atmost 4
comparisions
Fig b• This tree requires atmost 3
comparisionsIf 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
5 25
10
15
20 15 25
5 20
10Compr 1
Compr 2
Compr 3
Compr 4
Fig.a Fig.b
![Page 18: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/18.jpg)
Fig a
• 1st comparison with 10• 2nd with 25• 3rd with 20• 4th with 15 • Total 4 comparisons
• Avg no. of comparisons• 1+2+2+3+4 =2.4• 5
Fig b• 1st comparison with 10• 2nd with 20• 3rd with 15 • Total 3 comparisons
• Avg no. of comparisons• 1+2+2+3+3 =2.2• 5
Hence for equal probability Fig.b is more desireable
![Page 19: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/19.jpg)
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)
![Page 20: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/20.jpg)
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
![Page 21: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/21.jpg)
Now we understood why we need and optimal bst
• Starting with our topic • OBST
![Page 22: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/22.jpg)
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
• 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
![Page 23: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/23.jpg)
• 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
![Page 24: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/24.jpg)
Extended binary trees
15 25
5 20
10
5 25
10
15
20
Fig bFig a
![Page 25: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/25.jpg)
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
![Page 26: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/26.jpg)
• .
5 25
10
15
20
L=0
L=1
L=2
L=3
L=4
Internal path lengthI=0+1+1+2+3=7
External path lengthE=2+2+2+3+4+4=17
E=I+2(no of nodes)
Tree with max E will have max I
![Page 27: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/27.jpg)
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.
If the user searches a particular key in the tree, 2
cases can occur:
![Page 28: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/28.jpg)
Cost of a bst when the searches are successful
Probability of node i
• Cost =
Level of node i
![Page 29: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/29.jpg)
Cost of a bst when the searches are unsuccessful
• Cost =
![Page 30: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/30.jpg)
Total cost
• As we know that there is a possibility of both successful and unsuccessful searches
• Cost= +
![Page 31: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/31.jpg)
Understanding obst
with an example
![Page 32: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/32.jpg)
k2
k1 k4
k3 k5d0 d1
d2 d3 d4 d5
k2
k1 k5
k4
k3
d0 d1
d2 d3
d4
d5
Figure (a)
i 0 1 2 3 4 5
PiKnode
0.15 0.10 0.05 0.10 0.20
Qidnode
0.05 0.10 0.05 0.05 0.05 0.10Figure (b)
![Page 33: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/33.jpg)
Node# Depth probability costk1 1 0.15 0.30k2 0 0.10 0.10k3 2 0.05 0.15k4 1 0.10 0.20K5 2 0.20 0.60d0 2 0.05 0.15d1 3 0.10 0.30d2 3 0.05 0.20d3 3 0.05 0.20d4 3 0.05 0.20d5 3 0.10 0.40
Cost=
Probability * (Depth+1)
We can calculate the expected search cost node by node:
Cost=
Probability * (Depth+1)
![Page 34: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/34.jpg)
• And the total cost = (0.30 + 0.10 + 0.15 + 0.20 + 0.60 + 0.15 + 0.30 + 0.20 + 0.20 + 0.20 + 0.40 ) = 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.
![Page 35: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/35.jpg)
• 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
k1 k5
k4
k3
d0 d1
d2 d3
d4
d5
Figure (b)
![Page 36: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/36.jpg)
• So itz not necessary that always the key with highest probablityshould be the root
![Page 37: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/37.jpg)
Going in depth of obst
![Page 38: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/38.jpg)
Property of an obst
Subtree
Subtree
Subtree
Subtree
Optimal
Optimal
Optimal
![Page 39: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/39.jpg)
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 - 5 dummy nodes from 0 - 5
![Page 40: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/40.jpg)
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
![Page 41: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/41.jpg)
Computing Ei,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 ;
![Page 42: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/42.jpg)
Remarks•A slight change in the algorithm allows usto get the root of each subtree, and thusthe structure of OBST (how?)
•The powerful technique of storingcomputed is calledDynamic Programming
•Knuth observed a further property sothat we can compute OBST in O(n2) time(search wiki for more information)
![Page 43: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/43.jpg)
Any questions?
![Page 44: We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649f005503460f94c161df/html5/thumbnails/44.jpg)
Thank Queue