Unit 4 of 4 · B.Sc IT Sem 3

Unit 4: Trees

Data Structures notes · PTU syllabus (BSIT302/BSBC302)

3 min read5 topics10 exam questions
On this page
  1. Unit summary
  2. Definition and concepts of trees
  3. Basic trees and binary trees
  4. Binary tree representations
  5. Binary tree traversals
  6. Applications of trees
  7. Key terms
  8. Quick revision
  9. Important questions

Unit summary

Trees represent hierarchies and allow fast searching. This unit covers the definition and concepts of trees, basic trees, binary tree representations, binary tree traversals and applications of trees.

After this unit you can

  • Define tree terminology
  • Represent binary trees in memory
  • Traverse binary trees
  • Explain applications of trees

PTU syllabus topics

  • Definition and concepts
  • basic trees
  • binary tree representations
  • binary tree traversals and applications of trees
Key termsBinary tree traversals
Inorder
Left, Root, Right: gives sorted order in a BST
Preorder
Root, Left, Right: copies a tree
Postorder
Left, Right, Root: deletes a tree
Level order
Level by level, using a queue
1

Topic 1

Definition and concepts of trees

A tree is a finite set of nodes with one special node called the root, and the remaining nodes divided into disjoint subtrees.

Key termsTree vocabulary
Root
Top node with no parent
Parent and child
A node and the nodes directly below it
Leaf (terminal node)
A node with no children
Degree
Number of children of a node
Level
Root is level 0 (or 1); children are one level lower
Height (depth)
Number of levels in the tree
Siblings
Nodes with the same parent
2

Topic 2

Basic trees and binary trees

A binary tree is a tree in which each node has at most two children: a left child and a right child.

  • Strictly binary tree: every non-leaf node has exactly two children.
  • Complete binary tree: all levels are full except possibly the last, which is filled from the left.
  • Full (perfect) binary tree: every level is completely filled.
  • Maximum nodes at level l = 2ˡ (root at level 0); maximum nodes in a tree of height h = 2ʰ − 1.
3

Topic 3

Binary tree representations

  • Array (sequential) representation: root at index 1; for a node at index i, the left child is at 2i and the right child at 2i + 1; the parent is at i/2. Efficient for complete trees, wasteful for skewed trees.
  • Linked representation: each node has three fields — left pointer, data, right pointer.
cstruct node {
    struct node *left;
    int data;
    struct node *right;
};
4

Topic 4

Binary tree traversals

Traversal means visiting every node exactly once.

Key termsThe three depth-first traversals
Preorder
Root, Left, Right
Inorder
Left, Root, Right
Postorder
Left, Right, Root

Example

For the tree with root A, left child B (children D, E) and right child C: Preorder = A B D E C; Inorder = D B E A C; Postorder = D E B C A.

cvoid inorder(struct node *t) {
    if (t != NULL) {
        inorder(t->left);
        printf("%d ", t->data);
        inorder(t->right);
    }
}

Non-recursive traversal uses an explicit stack in place of recursion. Level-order traversal uses a queue.

Exam tip

A tree can be rebuilt uniquely from its inorder plus preorder (or inorder plus postorder) sequences — a frequent long question.

5

Topic 5

Applications of trees

  • Binary search trees: fast search, insert and delete (O(log n) when balanced).
  • Expression trees: represent arithmetic expressions; traversals give prefix, infix and postfix forms.
  • Heaps: priority queues and heap sort.
  • Huffman trees: data compression.
  • B-trees and B+ trees: database and file indexes.
  • Tries: dictionary and autocomplete; decision trees: machine learning; file system directories and the DOM of web pages.

Example

The expression (a + b) × c forms a tree with × at the root; its postorder traversal gives a b + c ×.

Key terms

Root
Topmost node of a tree
Binary tree
Tree with at most two children per node
Inorder traversal
Left, root, right
Complete binary tree
All levels full except possibly the last, filled left to right
Expression tree
Tree representing an arithmetic expression

Quick revision

  • Terminology: root, parent, child, leaf, level, height, degree.
  • Binary, full, complete, skewed trees.
  • Array (2i + 1, 2i + 2) and linked representations.
  • Preorder, inorder, postorder, level order.
  • Applications: BST, expression trees, heaps, Huffman, B-trees, tries.

Important exam questions

Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).

Short-answer questions

  1. Q1.Define a leaf node.
  2. Q2.What is a complete binary tree?
  3. Q3.In an array representation, where are the children of node i stored?
  4. Q4.Write the inorder traversal rule.
  5. Q5.What is an expression tree?
  6. Q6.Name three applications of trees.

Long-answer questions

  1. Q1.Explain tree terminology and types of binary trees.
  2. Q2.Explain array and linked representations of binary trees.
  3. Q3.Explain binary tree traversals with an example.
  4. Q4.Discuss applications of trees.

Stuck on this unit?

Message SBS on WhatsApp for help with Data Structures, or to ask about studying B.Sc IT at Synetic.

WhatsApp us