MCS-208 · Unit 6

Trees

Section 8 of 8

Block 2 · Unit 6Trees14 min · Section 8 of 8

6.8Applications of Trees

Covers IGNOU MCS-208 §6.8, 6.9

Trees are used throughout computing. This page looks closely at two binary-tree applications (binary search trees and expression trees) and surveys the others.

Binary search trees

Searching starts at the root and goes left or right at each node, like binary search. For a tree of height hh a search takes O(h)O(h) comparisons: O(log⁡n)O(\log n) if the tree is balanced, O(n)O(n) if it is skewed. An inorder traversal of a BST lists the keys in sorted order. Unit 7 studies BSTs and balanced variants (AVL, red-black, B-trees) in detail.

Expression trees

An expression tree is evaluated bottom-up: a leaf’s value is its operand; an internal node’s value is its operator applied to the values of its two subtrees. This is a postorder computation. An expression tree can be built from a postfix expression with a stack of subtrees: push a leaf for each operand; for each operator, pop two subtrees and push a new node with them as children.

Other applications

AreaTree used
File systems and foldersGeneral trees (and B-trees on disk)
Databases and indexesB-trees, B+ trees (Unit 7)
Fast searchingBSTs, AVL and red-black trees (Unit 7)
CompilersSyntax trees and expression trees
Data compressionHuffman trees
Games and AIGame trees (e.g. tic-tac-toe), decision trees
GraphicsQuadtrees, octrees
Text searchTries (Unit 11)

A game tree has the current position at the root and the positions after each possible move as children. Its size grows very fast: with just two choices per move, a game of 10 moves already gives 210−1=10232^{10} - 1 = 1023 nodes, so real game programs search only part of the tree.

Key takeaways

  • BST: left subtree < node < right subtree; search O(h)O(h); inorder lists keys in sorted order.
  • Expression tree: leaves are operands, internal nodes operators; evaluate in postorder.
  • Build an expression tree from postfix with a stack of subtrees.
  • Trees also appear in file systems, databases, compilers, compression, games and graphics.

Practice questions

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

Question 1 · multiple choice

Keys 50, 30, 70, 20, 40 are inserted into an empty BST in this order. What is the inorder traversal?

Show answer

Answer: C. 20 30 40 50 70

Inorder of a BST is always sorted, whatever the insertion order. “50 30 20 40 70” is the preorder of this tree.

Question 2 · multiple choice

Which comparisons are made when searching for 40 in the BST of question 1?

Show answer

Answer: B. 50, 30, 40

40 < 50: go left to 30; 40 > 30: go right to 40: found. A search follows one root-to-node path.

Question 3 · multiple choice

Which operator is at the root of the expression tree for a - b * c + d?

Show answer

Answer: C. +

The expression is ((a - (b * c)) + d); the last operation is +. The root is the operator applied last.

Question 4 · multiple choice

What does this program print?

C · q6-eval.c
#include <stdio.h>
#include <stddef.h>

typedef struct n {
    struct n *l;
    char s;
    struct n *r;
} N;

int eval(const N *t)
{
    if (t->l == NULL)
        return t->s - '0';
    int a = eval(t->l), b = eval(t->r);
    return t->s == '+' ? a + b : t->s == '-' ? a - b : a * b;
}

int main(void)
{
    N two = {NULL, '2', NULL}, three = {NULL, '3', NULL};
    N four = {NULL, '4', NULL}, nine = {NULL, '9', NULL};
    N sum = {&two, '+', &three};
    N diff = {&nine, '-', &four};
    N root = {&sum, '*', &diff};
    printf("%d\n", eval(&root));
    return 0;
}
Show answer

Answer: D. 25

The tree is (2 + 3) * (9 - 4) = 5 * 5 = 25. 37 would come from reading the string without the tree structure as 2 + 3 * 9 - 4.

Question 5 · multiple choice

In the worst case, how many comparisons does a search need in a BST with nn keys?

Show answer

Answer: A. nn

If keys are inserted in sorted order, the BST is a chain of height nn. log⁡2n\log_2 n holds only for balanced trees (Unit 7).

Question 6 · short answer

Draw the expression tree for (a + b) * c - d / e and write its preorder and postorder traversals.

Show worked answer

The last operation is -, so it is the root. Its left subtree is (a + b) * c: * with left child + (children a, b) and right child c. Its right subtree is d / e: / with children d and e. Preorder: - * + a b c / d e. Postorder: a b + c * d e / -.

↑ ↓ move↵ openTab switch type