MCS-208 · Unit 7

Advanced Trees

Section 1 of 9

Block 3 · Unit 7Advanced Trees12 min · Section 1 of 9

7.1Binary Search Trees: Definition, Search and Insertion

Covers IGNOU MCS-208 §7.2, 7.2.1, 7.2.2

A binary search tree (BST) keeps keys in order so that a search can discard half of the remaining tree at each step, as binary search does in a sorted array. This page gives the exact definition and the basic operations.

Definition

Traversals

A BST is a binary tree, so it has the three traversals of Unit 6. For the BST of Figure 7.1 (root K; K’s children J and S; J’s left child F, whose right child is G; S’s children M and U; M’s children L and P):

  • preorder: K J F G S M L P U
  • inorder: F G J K L M P S U (always sorted for a BST)
  • postorder: G F J L P M U S K

Searching, minimum and maximum

  • Search for k: start at the root; if k equals the key, stop; if smaller, go left; if larger, go right; reaching NULL means “not found”.
  • Minimum: follow left links until there is no left child. Maximum: follow right links.

Each takes at most hh steps, where hh is the height of the tree.

Insertion

To insert k, search for it; when the search falls off the tree at a NULL link, put a new node there. A new key always becomes a leaf. For example, inserting 5 into the tree 10(7(3), 15) goes 10 → 7 → 3 → right of 3, so 5 becomes the right child of 3.

Why height matters

All these operations are O(h)O(h). A BST built from random keys has expected height O(log⁡n)O(\log n), but inserting keys in sorted order produces a chain of height nn, and every operation becomes O(n)O(n). The rest of this unit is about trees that prevent this.

Key takeaways

  • BST: left subtree < node < right subtree, for every node.
  • Inorder traversal of a BST lists the keys in sorted order.
  • Search, insert, min, max: one root-to-leaf path, O(h)O(h).
  • Sorted input makes a plain BST degenerate into a chain of height nn.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which of these is not a valid BST?

Show answer

Answer: C. 8(3(1, 9), 10)

9 is in the left subtree of 8 but larger than 8. Every node is correct relative to its own parent (9 > 3), which is why checking only children is not enough.

Question 2 · multiple choice

What is the inorder traversal of any BST containing the keys 40, 15, 70, 5?

Show answer

Answer: B. 5 15 40 70

Inorder always gives sorted order for a BST. The shape (and so pre- and postorder) depends on insertion order, but inorder does not.

Question 3 · multiple choice

Keys 1, 2, 3, 4, 5, 6 are inserted in this order into an empty BST. What is its height (number of levels)?

Show answer

Answer: D. 6

Each key is larger than all before it, so each becomes the right child of the previous one: a chain of 6 levels. This is the worst case for a BST.

Question 4 · multiple choice

Where is the smallest key in a non-empty BST?

Show answer

Answer: C. In the node reached by following left links from the root until there is no left child

Smaller keys are always to the left. That node may have a right child, so it need not be a leaf.

Question 5 · multiple choice

What does this program print?

C · q7-search.c
#include <stdio.h>
#include <stddef.h>

typedef struct n { struct n *l; int k; struct n *r; } N;

int main(void)
{
    N a = {NULL, 20, NULL}, b = {NULL, 35, NULL}, c = {&a, 30, &b};
    N d = {NULL, 60, NULL}, root = {&c, 50, &d};
    int key = 35, steps = 0;
    const N *t = &root;
    while (t != NULL && t->k != key) {
        steps++;
        t = key < t->k ? t->l : t->r;
    }
    printf("%s after %d moves\n", t ? "found" : "missing", steps);
    return 0;
}
Show answer

Answer: B. found after 2 moves

35 < 50: move to 30 (1); 35 > 30: move to 35 (2); found. The count is of moves between nodes, not comparisons.

Question 6 · short answer

Define a binary search tree and write a recursive algorithm to insert a key into it.

Show worked answer

A BST is a binary tree that is either empty or whose root key is larger than every key in its left subtree and smaller than every key in its right subtree, both subtrees being BSTs. Insert(t, k): if t is NULL, create a node with key k and return it; if k < t.key, set t.left = Insert(t.left, k); else if k > t.key, set t.right = Insert(t.right, k); (if k = t.key the key is already present); return t. The new key becomes a leaf; the cost is O(h)O(h).

↑ ↓ move↵ openTab switch type