Unit 4: Trees
Data Structures notes · PTU syllabus (BSIT302/BSBC302)
On this page
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
- 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
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.
- 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
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.
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;
};Topic 4
Binary tree traversals
Traversal means visiting every node exactly once.
- 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.
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
- Q1.Define a leaf node.
- Q2.What is a complete binary tree?
- Q3.In an array representation, where are the children of node i stored?
- Q4.Write the inorder traversal rule.
- Q5.What is an expression tree?
- Q6.Name three applications of trees.
Long-answer questions
- Q1.Explain tree terminology and types of binary trees.
- Q2.Explain array and linked representations of binary trees.
- Q3.Explain binary tree traversals with an example.
- 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.
