DS-Module 5 - Binary Trees
DS-Module 5 - Binary Trees
Module 5
Contents
Binary Trees: Introduction and definition,
constructing a Tree.
DEFINITION
• A tree is a finite set of one or more nodes such that
– There is a specially designated node called root.
– The remaining nodes are partitioned into n >= 0 disjoint set T1,…,Tn,
where each of these sets is a tree.
– T1,…,Tn are called the subtrees of the root
Introduction
Trees: Unlike Arrays, Linked Lists, Stack and queues, which are
linear data structures, trees are hierarchical data structures.
Tree Vocabulary: The topmost node is called root of the tree. The
elements that are directly under an element are called its children.
The element directly above something is called its parent. For
example, ‘a’ is a child of ‘f’, and ‘f’ is the parent of ‘a’. Finally,
elements with no children are called leaves.
Why Trees?
1. One reason to use trees might be to store
information that naturally forms a hierarchy. For
example, the file system on a computer
2. Trees (with some ordering e.g.,
BST) provide moderate
access/search (quicker than Linked
List and slower than arrays).
3. Trees provide moderate
insertion/deletion (quicker than
Arrays and slower than Unordered
Linked Lists).
4. Like Linked Lists and unlike Arrays,
Trees don’t have an upper limit on
Need for binary trees:
• In C, Binary trees have some exciting and useful
applications which can be implemented.
Case 1:
If a node is in the ith index: A
Case 2: H I
If a node is in the ith index:
Case 1:
If a node is in the ith index: A
Case 2: H I
If a node is in the ith index:
Case 1:
If a node is in the ith index: A
Case 1:
If a node is in the ith index: A
1 2 3 4 5 6 7 C
A - B - - - C
Case 2:
If a node is in the ith index:
Case 1:
If a node is in the ith index: A
Case 2: D
If a node is in the ith index:
Processed Element
INORDER TRAVERSAL ALGORITHM USING STACKS
2
TRAVERSAL ALGORITHM USING STACKS
Processed Element
TRAVERSAL ALGORITHM USING STACKS
TRAVERSAL ALGORITHM USING STACKS
-
PTR=-500
PTR=-PTR
=-(-500)
=5000
Processed Element
Binary Tree Traversals
• Arithmetic Expression using binary tree
– inorder traversal (infix expression)
A/B*C*D+E
– preorder traversal (prefix expression)
+**/ABCDE
– postorder traversal
(postfix expression)
AB/C*D*E+
– level order traversal
+*E*D/CAB
Binary Tree Traversals (3/9)
• Inorder traversal (LVR) (recursive version)
output A / B * C * D + E
:
ptr
L
V
R
Binary Tree Traversals (4/9)
• Preorder traversal (VLR) (recursive version)
output + * * / A B C D E
:
V
L
R
Binary Tree Traversals (5/9)
• Postorder traversal (LRV) (recursive version)
output A B / C * D * E +
:
L
R
V
Binary Tree Traversals (6/9)
• Iterative inorder traversal
– we use a stack to simulate recursion
5 8
4 11
3 14
2 17
1
A B
/ *C *D E+
L
V
output A /B *C *D + E node
:
Threaded Binary Trees
• Threads
– Do you find any drawback of the above tree?
– Too many null pointers in current representation of binary trees
n: number of nodes
number of non-null links: n-1
total links: 2n
null links: 2n-(n-1) = n+1
– Solution: replace these null pointers with some useful “threads”
Binary Tree
Threaded Binary Trees
• Rules for constructing the threads
– If ptr->left_child is null,
replace it with a pointer to the node that would be visited before ptr in
an inorder traversal that is Inorder Predecessor.
– If ptr->right_child is null,
replace it with a pointer to the node that would be visited after ptr in an
inorder traversal that is Inorder Successor.
Binary Tree
Inorder Traversal- D,B,E,G,A,C,F
Threaded Binary Trees
dangling D t E t F G
inorder traversal:
H I H D I B E A F C G
Threaded Binary Trees
• If we don’t want the left pointer of H and the right pointer of G to be dangling
pointers, we may create root node and assign them pointing to the root node
Threaded Binary Trees
Inorder traversal of a threaded binary tree
• By using of threads we can perform an inorder traversal without making
use of a stack (simplifying the task)
• Now, we can follow the thread of any node, ptr, to the “next” node of
inorder traversal
medium
smaller larger
Difference between BT and BST
•A binary tree is simply a tree in which each node can have at most
two children.
•A binary search tree is a binary tree in which the nodes are
assigned values, with the following restrictions :
1. No duplicate values.
2. The left subtree of a node can only have values less than the
node
3. The right subtree of a node can only have values greater than the
node and recursively defined
4. The left subtree of a node is a binary search tree.
5. The right subtree of a node is a binary search tree.
BST Operations
Four basic BST operations
1
Traversal
2
Search
3
Insertion
4
Deletion
BST Traversal
PreorderTraversal
Root Left Right
23 18 12 20 44 35 52
PostorderTraversal
Left Right Root
12 20 18 35 52 44 23
InorderTraversal
Left Root Right
12 18 20 23 35 44 52
17 88
32 65 97
28 54 82
29 76
80
S
e
a
r
c
h
A
l
g
o
ri
t
h
m
Searching in BST
• Searching a
binary search
tree
O(h)
BST Insertion Algorithm
Inserting into a binary search tree
An empty tree
Deletion from BST
• Deletion from a binary search tree
– Three cases should be considered
– case 1. leaf delete
– case 2. one child delete and
change the pointer to this child
– case 3. two child either the
smallest element in the right subtree
or the largest element in the left
subtree
BST Deletion Algorithm
BST Deletion Algorithm
Binary Search Trees
• In the end, the only element of the stack will be the root of
an expression tree.