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.
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 answerHide 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
| Traversal | Order | Result |
|---|---|---|
| Pre-order | Root, Left, Right | A, B, D, E, C, F, G |
| In-order | Left, Root, Right | D, 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
0 · 5 marks
What are the different traversing methods in a binary tree? Explain with a clear example. [5]
Binary tree definition and applications
2082 · 5 marks
Define binary tree and binary search tree. List the applications of Binary tree. [2+3]
2080 · 5 marks
What are different applications of binary tree? Explain. [5]
Show solved answerHide 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:
Represents: (2 + 3) * 5
* / \ + 5 / \ 2 3
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
| Application | Use |
|---|---|
| Binary Search Tree | Searching and sorting |
| Expression Tree | Compiler design |
| Heap | Priority queue, Heap sort |
| Huffman Coding | Data compression |
| Decision Tree | AI, Machine Learning |
| File System | Directory traversal |
Binary trees are fundamental data structures that form the basis of many efficient algorithms and real-world systems.
2079 · 5 marks
Explain different applications of binary tree. [5]
Show solved answerHide 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) * ccan 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
| Application | Purpose |
|---|---|
| Binary Search Tree | Searching and sorting data |
| Expression Tree | Evaluating arithmetic expressions |
| Heap / Priority Queue | Scheduling and heap sort |
| Huffman Coding Tree | Data compression |
| Decision Tree | AI decision making |
Binary trees form the foundation of many efficient algorithms and data structures used in real-world software systems.
2078 · 5 marks
What is binary tree? Explain different application of binary tree. [5]
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]
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]
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]
Make Unit 8 stick
Practice BIT201 with flashcards & quizzes