Title: Searching
1Searching Sorting
- Binary Trees and Treesort
- Lecture 3
2Start with tournament ladder
a is the winner
a
a
d
b
a
d
e
b
c
a
e
h
g
d
f
How can we also find the second in
order? Solution Remove a and re-compute
positions in ladder vacated by a as shown in
the next slide.
3Update the tournament ladder
b is the winner 2nd time
b
b
d
b
g
d
e
b
c
a
e
h
g
d
f
How can we also find the third in
order? Solution Repeat the process that was
used to find 2nd in order.
4Use tournament ladder for k best
- Count (n -1) comparisons to find the ladder.
- At most log n comparisons to update ladder.
- Conclusion (n - 1) (k - 1) log n to find k
best - Sorting All can be ordered in O(n log n)
time. Sorting - algorithm is TOURNAMENT SORT.
- In text Tree Sort (
since Tournament - ladder is a tree )
5Some basic definitions about trees
- A graph that is both acyclic connected is
called a tree. - A binary tree is a tree structure defined on a
finite set of nodes that either - contains no nodes or
- has three disjoint set of nodes a root node,
binary tree called left subtree and binary tree
called right subtree - Binary trees are drawn upside down with root at
the top. - Depth of tree maximum depth of any vertex from
the root. - A binary tree has at most 2d vertices at depth d.
6Some examples of binary trees
Complete binary tree with 7 vertices and depth
2
Incomplete binary tree with 11 vertices and depth
4
Left-to-rtight binary tree with 5 vertices and
depth 2
7Tournament Sort (Tree-Sort)
- Algorithm
- k log(n). Construct a complete binary tree of
depth k. Assign each element of the array to a
leaf. - Working up from the leaves, assign to each node
the minimum of the values at the root nodes of
the left and right sub-trees. - Remove the value at root.
- Put some larger number M at the leaf node of
the removed number. - Update the node values traversing up the tree
along the path of the removed value.
General idea based on binary trees.
8Trace of Tournament Sort
- Assign each element to leaf
- Elevate the minimum
- of two values at any
- level. Identify the smallest
- number at the root.
- Remove 2 from all
- nodes on the path to root.
- Elevate the next
- number in a similar fashion
9Trace of Tournament Sort cont...
- In the next iteration
- 3 comes to root and
- is removed.
- In the next iteration
- 4 comes to root and
- is removed.
Sorted numbers
2 3
Highest path
10Trace of Tournament Sort cont...
Highest path
- In the next iteration
- 5 comes to root and
- is removed.
- In the next iteration
- 7 comes to root and
- is removed.
2 3 4 5
Sorted numbers
11Trace of Tournament Sort cont...
- In the next iteration
- 8 comes to root and
- is removed.
- In the next iteration
- 9 comes to root and
- is removed.
Highest path
9
12Trace of Tournament Sort cont...
- In the next iteration
- 12 comes to root and
- is removed.
13Complexity
- Time complexity
- - Average case O(n log n)
- - Worst case O(n log n)
14k th largest elements
- Using the algorithm described for tournament sort
we can find the k largest elements from a large
list in - O( n k log n).
- Eg. Lets say we want to find the largest 20
numbers from a list of 1000 numbers. The order of
this search would be - - O(n k log n) ? 1000 20log(1000) 1,200 as
compared to - - O(n log n) ?1000log(1000) 10,000 for
complete sorting - - O(log n) ? 20,000 for sequentially
finding 20 largest.