Professional Documents
Culture Documents
Lecs 11 Trees - Intro
Lecs 11 Trees - Intro
Data Structures
& Algorithms
BITS Pilani
Sundaresan Raman
Pilani Campus
The following slides are adapted from Dr. Jagat Sesh Challa
2 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Agenda
• Trees: Definition and Examples
• Tree Traversals
3 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
5 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Trees: Example 1
Hierarchical
Organization of
Employees of a
Company
6 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Trees: Example 2
Table of Contents
of a Book
7 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Trees: Example 3
Unix/Linux File
System
8 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
10 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Recursive Definition of a Binary Tree
A binary tree is either a
• leaf or
• An internal node (the root) and one/two binary trees (left subtree and/or right subtree).
11 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Binary Tree: Example 1
A binary tree representing an arithmetic expression. This tree represents the expression
𝟑+𝟏 × 𝟑 / 𝟗−𝟓 +𝟐 – 𝟑× 𝟕−𝟒 +𝟔
12 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Binary Tree: Example 2
Decision Tree
13 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Perfect Binary Tree
• Level i has 2i nodes
• In a tree of height h
– leaves are at level h
– No. of leaves is 2h
– No. of internal nodes = 1+2+22+…+2h-1= 2h-1
– No of internal nodes = no of leaves -1
– Total no. of nodes is 2h+1-1 = n
• In a tree of n nodes
– No. of leaves is (n+1)/2
– Height = log2(no of leaves)
14 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Binary Tree
• A binary tree can be obtained from an appropriate perfect binary tree by pruning
15 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Complete Binary Tree
• All levels are full except the last level
16 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Full Binary Tree
• Binary tree in which all the nodes have 0 or two children.
17 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Minimum Height of a binary Tree
18 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Maximum Height of a binary Tree
19 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
No of leaves in a Binary Tree
• no of leaves <= 1+ no of internal nodes.
• Proof: By induction on no. of internal nodes
– Tree with 1 node has a leaf but no internal node.
– Assume stmt is true for tree with k-1 internal nodes.
– A tree with k internal nodes has k1 internal nodes in left subtree and (k-k1-1)
internal nodes in right subtree.
– No of leaves <= (k1+1)+(k-k1) = k+1
20 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
No of leaves in a Binary Tree (contd.)
21 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
23 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
ADTs for Binary Trees
24 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Node Structure of a Binary Tree
25 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Representing Rooted Trees
BinaryTree:
• Parent: BinaryTree
• LeftChild: BinaryTree
• RightChild: BinaryTree
26 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Trees Traversals
Tree Walks/ Traversals
• A tree walk or traversal is a way of visiting all the nodes in a tree in a specified
order.
• A pre-order tree walk processes each node before processing its children
• A post-order tree walk processes each node after processing its children
28 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Pre-order Traversal
Algorithm preOrder(v)
“visit” node v
for each child w of v do
recursively perform preOrder(w)
Example:
Reading a document from beginning to end
29 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Post-order Traversal
Algorithm postOrder(v)
for each child w of v do
recursively perform postOrder(w)
“visit” node v
Example:
du (disk usage) command of Unix
30 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Traversals of Binary Trees
preorder(v)
if (v == null) then return
else
visit(v)
preorder(v.leftchild())
preorder(v.rightchild())
postorder(v)
if (v == null) then return
else
postorder(v.leftchild()) Preorder Postorder
postorder(v.rightchild()) abcdfge cfgdbea
visit(v)
31 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Evaluating Arithmetic Expression
• Specialization of a post-order traversal
Algorithm evaluate(v)
if v is a leaf
return the variable stored at v
else
let o be the operator stored at v
x → evaluate(v.leftChild())
y → evaluate(v.rightChild())
return x o y
32 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Traversals of Binary Trees (contd.)
• Besides preorder and postorder, a third possibility arises when v is visted between
the visit to the left and the right subtrees.
Algorithm inOrder(v)
if (v == null) then return
else
inOrder(v.leftChild())
visit(v)
inOrder(v.rightChild())
Inorder
cbfdgae
33 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Euler Tour Traversal
34 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Printing an arithmetic expression
35 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
37 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Building tree from post and inorder
• Similar procedure can be applied to derive the tree from postorder and inorder
traversal.
• Exercise: Do it!
38 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Insufficiency of pre & postorder
• Given the pre and postoder traversal of a binary tree
• We cannot uniquely identify the tree.
• There can be two (or more) trees with the same pre and postorder traversals.
Preorder Postorder
abc cba
39 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
A Special Case
• If each internal node of the binary tree has at least two children then the tree can be
determined from the pre and post order traversals.
40 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Notion of Traversals in Generalized Trees
• In generalized trees with >= 2
children per node we can have:
• Preorder traversal
• Postorder traversal
• But NOT
• Inorder traversal
• Specific to binary trees only.
• A variant of inorder traversal is
possible wherein we visit all children
except the last one, which is visited Preorder: ABKNMJFDGECHIL
after visiting the parent Postorder: NMKJBFGDCHLIEA
Modified Inorder: NKMBJFDGACHEIL
41 Department of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Thank you
Q&A