Problem 26
Question
Tournament sort is a sorting technique that reflects the structure of a tournament. For a list of \(n\) items where \(n\) is a power of \(2,\) a full, complete binary tree springs from the leaves to the root. For example, consider the list \(13,8,5,1,21,3,34,2,\) store the numbers as leaves in a binary tree (Figure 9.104 ). At each level move up the larger of the siblings to its parent (Figure 9.105\()\) . Now the root contains the largest element \(m .\) Output it and store 0 in the leaf that contained \(m\) . Repeat this procedure until all elements are output. Give the final output.
Step-by-Step Solution
Verified Answer
The final sorted list using the Tournament sort technique is: \([13, 8, 5, 1, 34, 21, 3, 2]\).
1Step 1: Store the numbers as leaves in a complete binary tree
First, let's store the given list of numbers as leaves in a complete binary tree from left to right. For our example, the tree would look like this:
```
?
/ \
? ?
/ \ / \
? ? ? ?
/ \/ \/ \/ \
13 8 5 1 21 3 34 2
```
2Step 2: Move up the larger of the siblings to their parents
Now we need to move up the larger of the siblings to their parents. We will work through each level one by one:
Level 1 (leaves):
```
?
/ \
? ?
/ \ / \
13 8 5 1
/ \ / \ / \
13 8 5 1 21 3 34 2
```
Level 2:
```
?
/ \
13 5
/ \ / \
13 8 5 1
/ \ / \ / \
13 8 5 1 21 3 34 2
```
Level 3 (root):
```
13
/ \
13 5
/ \ / \
13 8 5 1
/ \ / \ / \
13 8 5 1 21 3 34 2
```
3Step 3: Output the largest element and store 0 in its leaf
Now we have the largest element (13) at the root. We will output this number, and then replace the leaf containing this number with 0:
```
?
/ \
? ?
/ \ / \
0 8 5 1
/ \/ \/ \/ \
0 8 5 1 21 3 34 2
```
4Step 4: Repeat steps 2 and 3 until all elements are output
Now we just need to repeat steps 2 and 3 until we have output all elements. After each step, the tree will look like this:
Second largest element (8):
```
?
/ \
? 5
/ \ / \
0 8 5 1
/ \/ \/ \/ \
0 0 5 1 21 3 34 2
```
Third largest element (5):
```
?
/ \
? 0
/ \ / \
0 0 5 1
/ \/ \/ \/ \
0 0 0 1 21 3 34 2
```
Fourth largest element (1):
```
?
/ \
? 0
/ \ / \
0 0 0 1
/ \/ \/ \/ \
0 0 0 0 21 3 34 2
```
Now, the remaining elements are: \(21,3,34,2\). We repeat the above steps for these elements:
Fifth largest element (34):
```
?
/ \
? 21
/ \ / \
0 0 3 0
/ \/ \/ \/ \
0 0 0 0 21 3 34 2
```
Sixth largest element (21):
```
?
/ \
? 0
/ \ / \
0 0 3 0
/ \/ \/ \/ \
0 0 0 0 21 3 0 2
```
Seventh largest element (3):
```
?
/ \
? 0
/ \ / \
0 0 0 0
/ \/ \/ \/ \
0 0 0 0 0 3 0 2
```
Eighth largest element (2):
```
?
/ \
? 0
/ \ / \
0 0 0 0
/ \/ \/ \/ \
0 0 0 0 0 0 0 2
```
There are no elements left. Thus, the sorted list is: \([13, 8, 5, 1, 34, 21, 3, 2]\).
Key Concepts
Binary TreeSorting AlgorithmComplete Binary Tree
Binary Tree
A binary tree is a fundamental data structure in computer science where each node has up to two children, known as the left child and the right child. It's much like a family tree but designed to organize data in a hierarchical pattern. Binary trees are used in various applications such as databases and expression parsing. The main features of a binary tree include:
- Each node can have 0, 1, or 2 children.
- Leaves are nodes with no children.
- Internal nodes have one or two children.
Sorting Algorithm
Sorting algorithms are methods or processes used to arrange elements of a list in a certain order. These are crucial in computer science as they optimize data retrieval and processing. Tournament sort is one such sorting technique, inspired by the logic of competitive tournaments where the winner moves up the ladder. This sorting involves:
- Placing elements in the form of a complete binary tree as leaves.
- Comparing sibling elements and moving the larger or smaller element to the parent, depending on the sorting order.
- Extracting the root element (which bears the highest or lowest value) and replacing its position with a neutral element (like zero) at its leaf.
- Repeating the process until all elements are sorted.
Complete Binary Tree
A complete binary tree is a specialized form of binary tree in which all levels, except possibly the last one, are filled completely, and all nodes are as left as possible. This structure is ideal for tournament sort, as it ensures an even distribution of nodes, facilitating the smooth upward movement of compared elements. Key characteristics of complete binary trees include:
- Every level is filled except the last, which is filled from left to right.
- Perfectly balances workloads, making operations like insertion or deletion efficient.
- Suitable for representation in arrays due to their sequential nature, minimizing storage needs.
Other exercises in this chapter
Problem 25
Write an algorithm to print the contents of a binary search tree in lexicographic order.
View solution Problem 26
Compute the maximum number of leaves in a full ternary tree of height 5.
View solution Problem 27
Using the adjacency matrix of a connected graph with \(n\) vertices, write an algorithm to determine if it is a tree.
View solution Problem 27
Using the BFS method, construct a spanning tree for each graph.
View solution