8 Trees And Binary Search Trees

Data Structures and Algorithms · Unit 8

Trees and Binary Search Trees

Exam-focused notes for Trees and Binary Search Trees (Data Structures and Algorithms, BIT201): what the TU syllabus asks and how it has actually been tested, with 9 solved past questions from this unit.

Trees and Binary Search Trees in short

A tree is a non-linear structure that shows a parent and child relationship, like a family tree or the folders on a computer. Binary search trees keep data in order so that searching, insertion and deletion are fast.

Trees and Binary Search Trees: notes

Binary tree definition and applications

A tree is a set of nodes joined by edges, with one special node called the root. A binary tree is a tree in which every node has at most two children, called the left child and the right child.

Terms:

  • Parent and child: a node and the nodes directly below it.
  • Leaf: a node with no children.
  • Siblings: nodes with the same parent.
  • Level: the root is at level 0, its children at level 1, and so on.
  • Height: the number of edges on the longest path from the root to a leaf.
  • Degree of a node: the number of its children (0, 1 or 2 in a binary tree).
struct Node {
    int data;
    struct Node *left, *right;
};

Useful facts: level $i$ has at most $2^i$ nodes. A binary tree of height $h$ has at most $2^{h+1} - 1$ nodes.

Applications:

  • Binary search trees for fast searching and sorting.
  • Expression trees for evaluating arithmetic expressions.
  • Heaps for priority queues and heap sort.
  • File systems and folder structures.
  • Huffman trees for data compression, and decision trees.

Complete binary tree definition with examples

A complete binary tree has every level completely filled, except possibly the last level. The nodes of the last level are as far left as possible.

        1
      /   \
     2     3
    / \   /
   4   5 6

Levels 0 and 1 are full. Level 2 has 4, 5 and 6 filled from the left, so the tree is complete. Because there are no gaps, a complete tree with $n$ nodes can be stored in an array. The children of the node at index $i$ (starting at 1) are at $2i$ and $2i+1$.

Not complete: a tree where node 2 has only a right child. Its left slot is empty while a node to its right exists, so the left-to-right filling is broken.

Some books use the word "complete" for a tree in which all levels are full, including the last. That tree is also called a full or perfect binary tree. It has exactly $2^{h+1} - 1$ nodes.

Almost complete binary tree definition with examples

An almost complete binary tree (also called a nearly complete tree) has all levels full except the last. The last level is filled from left to right with no gaps. Binary heaps have this shape.

          10
        /    \
       20     30
      /  \   /  \
     40  50 60

Here the last level holds 40, 50 and 60 from the left, so the tree is almost complete. The tree below is not, because the left child of 20 is missing but its right child exists.

          10
        /    \
       20     30
         \    /
         50  60

Compare the shapes: a full tree has every level completely filled. An almost complete tree may have a partly filled last level. A skewed tree has all nodes on one side.

What this unit covers

  • Binary tree definition and applications
  • Complete binary tree definition with examples
  • Almost complete binary tree definition with examples
  • Binary search tree (BST) definition
  • BST search algorithm
  • BST insertion algorithm
  • BST deletion algorithm
  • Pre-order traversal method
  • In-order traversal method
  • Post-order traversal method
  • Binary tree traversal examples

Pre-order traversal method

2082 · 5 marks

Traverse the following tree in pre-order and in-order. [5]

Show solved answer

Given

Task: Traverse a given tree in pre-order and in-order.

Tree structure: The tree diagram/image is NOT provided in the question. Only the instruction and the mark allocation ([5]) are given.

Missing data: The actual tree (nodes and their parent/child relationships) is missing. Without the specific tree, an exact, verifiable set of traversal sequences cannot be produced. The correct output depends entirely on the omitted diagram.

Since the tree cannot be recovered, I will state the required methods precisely and demonstrate them on a clearly labelled assumed sample tree (so the traversal logic can still be checked), while noting this is illustrative only.


Solution

Assumed Sample Tree

        A
       / \
      B   C
     / \ / \
    D  E F  G

1. Pre-order Traversal (Root → Left → Right)

Rule: visit Root, then Left subtree, then Right subtree.

  • Visit A (root)
  • Left subtree of A (rooted at B): visit B, then D, then E
  • Right subtree of A (rooted at C): visit C, then F, then G

$$\text{Pre-order: } A \to B \to D \to E \to C \to F \to G$$

2. In-order Traversal (Left → Root → Right)

Rule: traverse Left subtree, visit Root, traverse Right subtree.

  • Left subtree of A: D, B, E
  • Visit A
  • Right subtree of A: F, C, G

$$\text{In-order: } D \to B \to E \to A \to F \to C \to G$$

Summary

TraversalOrderResult
Pre-orderRoot, Left, RightA, B, D, E, C, F, G
In-orderLeft, Root, RightD, B, E, A, F, C, G

Important note: These results are valid only for the assumed sample tree shown above. Because the actual tree from the exam paper was not supplied, the definitive answer cannot be produced. If the real tree differs, apply the same rules:

  • Pre-order = Root, Left, Right
  • In-order = Left, Root, Right
Open this answer on its own page →

0 · 5 marks

What are the different traversing methods in a binary tree? Explain with a clear example. [5]

Tree traversal means visiting every node in a binary tree exactly once in a specific order. There are three standard depth-first traversal methods plus level-order traversal. --- For any node, the traversal involves three actions: - L = Visit Left subtree - N = Visit (process) the Node (root) - R...

Open this answer on its own page →

Binary tree definition and applications

2082 · 5 marks

Define binary tree and binary search tree. List the applications of Binary tree. [2+3]

--- A binary tree is a tree data structure in which each node has at most two children, referred to as the left child and the right child. Formal Definition: A binary tree is either: - An empty tree (null), OR - A node consisting of a root, a left subtree, and a right subtree, where both subtrees...

Open this answer on its own page →

2080 · 5 marks

What are different applications of binary tree? Explain. [5]

Show solved answer

Applications of Binary Tree

A binary tree is a tree data structure in which each node has at most two children, referred to as the left child and the right child. Binary trees have numerous important applications in computer science.


Applications of Binary Tree

1. Binary Search Tree (BST)

  • A binary tree is used to implement BST, where the left subtree contains nodes with values less than the root and the right subtree contains nodes with values greater than the root.
  • Used for efficient searching, insertion, and deletion operations with O(log n) average time complexity.
  • Example: Dictionary implementations, symbol tables.

2. Expression Tree

  • Binary trees are used to represent arithmetic expressions.
  • Leaf nodes contain operands (numbers/variables) and internal nodes contain operators (+, -, *, /).
  • Used in compilers to parse and evaluate expressions.
  • Example:
        *
       / \
      +   5
     / \
    2   3
    
    Represents: (2 + 3) * 5

3. Heap (Priority Queue)

  • A binary heap is a complete binary tree used to implement priority queues.
  • Used in heap sort algorithm and scheduling algorithms.
  • Min-heap: parent is smaller than children.
  • Max-heap: parent is larger than children.

4. Huffman Coding Tree

  • Used in data compression algorithms (e.g., ZIP, JPEG).
  • A binary tree is built where frequently occurring characters are placed closer to the root (shorter codes) and less frequent characters are placed deeper (longer codes).
  • This minimizes the total number of bits required to encode data.

5. Decision Tree

  • Used in Artificial Intelligence and Machine Learning for classification and decision-making.
  • Each internal node represents a condition/test, each branch represents an outcome, and each leaf node represents a decision/result.
  • Also used in game trees (e.g., chess, tic-tac-toe).

6. Tree Traversal in File Systems

  • Binary trees model hierarchical file systems and directory structures.
  • Traversal techniques (Inorder, Preorder, Postorder) are used to search, list, or process files and directories.

Summary Table

ApplicationUse
Binary Search TreeSearching and sorting
Expression TreeCompiler design
HeapPriority queue, Heap sort
Huffman CodingData compression
Decision TreeAI, Machine Learning
File SystemDirectory traversal

Binary trees are fundamental data structures that form the basis of many efficient algorithms and real-world systems.

Open this answer on its own page →

2079 · 5 marks

Explain different applications of binary tree. [5]

Show solved answer

Applications of Binary Tree


Applications of Binary Tree

A binary tree is a tree data structure in which each node has at most two children, referred to as the left child and the right child. Binary trees have numerous important applications in computer science.


1. Binary Search Tree (BST)

  • A binary tree is used to implement BST, where the left subtree contains nodes with values less than the root and the right subtree contains nodes with values greater than the root.
  • Used for efficient searching, insertion, and deletion of data in O(log n) time.
  • Example: Dictionary implementations, database indexing.

2. Expression Tree

  • Binary trees are used to represent arithmetic expressions.
  • Leaf nodes store operands (numbers/variables) and internal nodes store operators (+, -, *, /).
  • Used in compilers to evaluate and parse mathematical expressions.
  • Example: Expression (a + b) * c can be represented as a binary tree.

3. Heap (Priority Queue)

  • A binary heap is a complete binary tree used to implement priority queues.
  • Used in heap sort algorithm and scheduling algorithms in operating systems.
  • Supports efficient retrieval of the minimum or maximum element in O(log n) time.

4. Huffman Coding Tree

  • Binary trees are used in data compression algorithms such as Huffman encoding.
  • Frequently occurring characters are assigned shorter binary codes and less frequent characters are assigned longer codes.
  • Used in file compression tools like ZIP, JPEG, and MP3 encoding.

5. Decision Tree

  • Binary trees are used in Artificial Intelligence and Machine Learning to model decisions.
  • Each internal node represents a condition/test, and each leaf node represents a decision/outcome.
  • Used in game playing algorithms, medical diagnosis systems, and classification problems.

Summary Table

ApplicationPurpose
Binary Search TreeSearching and sorting data
Expression TreeEvaluating arithmetic expressions
Heap / Priority QueueScheduling and heap sort
Huffman Coding TreeData compression
Decision TreeAI decision making

Binary trees form the foundation of many efficient algorithms and data structures used in real-world software systems.

Open this answer on its own page →

2078 · 5 marks

What is binary tree? Explain different application of binary tree. [5]

A binary tree is a tree data structure in which each node has at most two children, referred to as the left child and the right child. A binary tree consists of: - Root node - the topmost node of the tree - Left subtree - a binary tree on the left side - Right subtree - a binary tree on the right...

Open this answer on its own page →

Almost complete binary tree definition with examples

2080 · 10 marks

Explain almost complete binary tree with example. How do you insert, search, and delete nodes in a binary search tree? Explain with suitable example?[10]

--- An Almost Complete Binary Tree (also called a Nearly Complete Binary Tree) is a binary tree in which: - All levels are completely filled except possibly the last level. - The last level has all nodes as far left as possible. This is the structural property used by Binary Heaps. --- Property D...

Open this answer on its own page →

Complete binary tree definition with examples

2079 · 5 marks

Explain complete binary tree with example. Starting with an empty binary search tree, show the effect of successively adding the following elements: 47, 50, 25, 27, 17, 61, 5, and 26. Also, traverse the resulting tree in pre-order, in-order, and post-order.[5]

  • Insertion sequence into an empty BST: 47, 50, 25, 27, 17, 61, 5, 26 - Required: definition + example of complete binary tree, BST construction, and pre-order, in-order, post-order traversals. --- A complete binary tree is a binary tree in which: - Every level, except possibly the last, is compl...
Open this answer on its own page →

BST search algorithm

0 · 5 marks

What is a Binary Search Tree? Write an algorithm for searching an item in a binary search tree. [5]

A Binary Search Tree (BST) is a binary tree data structure in which each node contains a key (value), and the following properties hold for every node: 1. The left subtree of a node contains only nodes with keys less than the node's key. 2. The right subtree of a node contains only nodes with key...

Open this answer on its own page →