Unit 1: Trees
Data Structures-II notes · PTU syllabus (UGCC2510)
On this page
Unit summary
A tree is a non-linear data structure that stores data in a hierarchy, like a family tree or a folder structure. Trees allow fast searching, sorting and organisation of data. This unit covers tree terminology, binary trees, their memory representation, traversals, threaded binary trees and binary search trees.
After this unit you can
- Define tree terminology: root, node, degree, level, height, leaf
- Represent a binary tree using arrays and linked lists
- Traverse a binary tree in preorder, inorder and postorder
- Insert, delete and search in a binary search tree
PTU syllabus topics
- Definition and terminology
- binary trees
- array and linked-list memory representation
- recursive and non-recursive traversal
- threaded binary tree
- binary search tree — insertion
- deletion
- searching
- Preorder
- Root → Left → Right
- Inorder
- Left → Root → Right (sorted order in a BST)
- Postorder
- Left → Right → Root (used to delete trees)
- Level order
- Level by level using a queue
Topic 1
Tree terminology
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
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
Memory representation
- 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
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
Threaded binary trees
In a linked binary tree with n nodes, n + 1 pointers are NULL. A threaded binary tree replaces these NULL pointers with threads — pointers to the inorder predecessor (left) or successor (right). This allows inorder traversal without a stack or recursion. Each node keeps a flag to tell a thread from a real child link.
Topic 6
Binary search tree (BST)
A binary search tree is a binary tree in which, for every node, all values in the left subtree are smaller and all values in the right subtree are larger. Inorder traversal of a BST gives the values in sorted order.
- 1Start at the root
- 2Equal?
Found
- 3Key smaller?
Go to the left subtree
- 4Key larger?
Go to the right subtree
- 5Reach NULL?
Not found
- Insertion: search for the key; insert the new node where the search ends (at a NULL link).
- Deletion — three cases: (1) leaf node: just remove it; (2) one child: link the parent to that child; (3) two children: replace the node with its inorder successor (smallest in the right subtree) or predecessor, then delete that node.
- Search, insert and delete take O(h) time: O(log n) for a balanced tree, O(n) for a skewed one.
Key terms
- Binary tree
- A tree in which each node has at most two children
- Complete binary tree
- All levels full except the last, filled from the left
- Traversal
- Visiting every node of a tree exactly once
- Threaded binary tree
- A tree whose NULL links point to inorder neighbours
- Binary search tree
- A binary tree with smaller values on the left and larger on the right
Quick revision
- Array representation: children at 2i and 2i + 1.
- Preorder: Root-Left-Right; inorder: Left-Root-Right; postorder: Left-Right-Root.
- Inorder of a BST is sorted.
- BST deletion with two children: replace with the inorder successor.
- BST operations are O(h).
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 binary tree and a complete binary tree.
- Q2.What is the maximum number of nodes in a binary tree of height h?
- Q3.Write the inorder traversal rule.
- Q4.What is a threaded binary tree?
- Q5.Define a binary search tree.
- Q6.Why does inorder traversal of a BST give sorted output?
Long-answer questions
- Q1.Explain array and linked representations of binary trees with diagrams.
- Q2.Explain the three traversal methods with a worked example and recursive algorithms.
- Q3.Construct a BST by inserting 50, 30, 70, 20, 40, 60, 80 and show the deletion of 30 and 50.
- Q4.What is a threaded binary tree? Explain its advantages.
Stuck on this unit?
Message SBS on WhatsApp for help with Data Structures-II, or to ask about studying BCA at Synetic.
