Professional Documents
Culture Documents
Trees
Trees
its Applications(18IS33)
Lecture Slides by
Prof. Swetha S
Types of Data Structures
Trees
"A tree may grow a
thousand feet tall, but
its leaves will return to
its roots."
-Chinese Proverb
Tree – a Hierarchical Data Structure
• Trees are non linear data structure that can be represented in a
hierarchical manner.
A tree contains a finite non-empty set of elements.
Any two nodes in the tree are connected with a relationship of
parent-child.
Every individual elements in a tree can have any number of sub
trees.
Roo A
t
B C
Left Righ
Sub t
Tree D E F Sub
Tree
Tree – Terminology.
Root : The basic node of all nodes in the tree. All operations on the tree are performed with passing root
node to the functions.( A – is the root node in above example.)
Child : a successor node connected to a node is called child. A node in binary tree may have at most two
children. ( B and C are child nodes to the node A, Like that D and E are child nodes to the node B. )
Parent : a node is said to be parent node to all its child nodes. ( A is parent node to B,C and B is parent
node to the nodes D and F).
Leaf : a node that has no child nodes. ( D, E and F are Leaf nodes )
Siblings : Two nodes are siblings if they are children to the same parent node.
Ancestor : any predecessor node on the path from root to that node ( A,B is ancestor node to D ).
Descendent : any Successor node on the path from that node to leaf node( B,C,D, E and F are descendent
nodes of node A)
Level of a node : The distance of a node from the root node, The root is at level – 0,( B and C are at Level 1
and D, E, F have Level 2 ( highest level of tree is called height of tree )
Degree of node : The number of nodes connected to a particular parent node.
Tree – Terminology.
B C D
E F G H I J
K L M
N O
CS314 Binary Trees 7
Binary Trees
• There are many variations on trees but we will start with binary trees
• binary tree: each node has at most two children
• the possible children are usually referred to as the left child and the right
child
parent
log2(n+1) -1
Find the maximum height of the tree given minimum number of
nodes?
n-1
Types of Binary tree
• Full/proper/strict Binary Tree
• Complete binary tree
• Perfect binary tree
Full/proper/strict Binary Tree
• full binary tree: a binary tree is which each node has exactly 2 or 0
children
log2(n+1) -1
• Find the maximum height of the tree given minimum number of
nodes?
(n-1)/2
Complete Binary Tree
• complete binary tree: In a complete binary tree, all the levels of a
tree are filled entirely except the last level. In the last level, nodes
might or might not be filled fully. Also, let’s note that all the nodes
should be filled from the left.
CS 103 20
Binary Tree Traversal Techniques
• Three recursive techniques for binary tree traversal
• In each technique, the left subtree is traversed recursively, the right
subtree is traversed recursively, and the root is visited
• What distinguishes the techniques from one another is the order of
those 3 tasks
CS 103 21
Preoder, Inorder, Postorder
• In Preorder, the root Preorder Traversal:
1. Visit the root
is visited before (pre)
2. Traverse left subtree
the subtrees traversals 3. Traverse right subtree
• In Inorder, the root is Inorder Traversal:
visited in-between left 1. Traverse left subtree
2. Visit the root
and right subtree traversal 3. Traverse right subtree
• In postorder, the root Postorder Traversal:
is visited after (post) 1. Traverse left subtree
2. Traverse right subtree
the subtrees traversals
3. Visit the root
CS 103 22
Illustrations for Traversals
• Assume: visiting a node 1
• Preorder:
5 8 9
1 3 5 4 6 7 8 9 10 11 12
10
4 6
• Inorder:
4 5 6 3 1 8 7 9 11 10 12 11 12
• Postorder:
4 6 5 3 8 11 12 10 9 7 1
CS 103 23
Illustrations for Traversals (Contd.)
• Assume: visiting a node 15
8 20
is printing its data
• Preorder: 15 8 2 6 3 7 2 11 27
11 10 12 14 20 27 22 30 6 10 12 22 30
• Inorder: 2 3 6 7 8 10 11
3 7 14
12 14 15 20 22 27 30
• Postorder: 3 7 6 2 10 14
12 11 8 22 30 27 20 15
CS 103 24
Binary Search Tree
• Binary Search Tree is a node-based binary tree data structure which
has the following properties:
• The left subtree of a node contains only nodes with keys lesser than
the node’s key.
• The right subtree of a node contains only nodes with keys greater
than the node’s key.
• The left and right subtree each must also be a binary search tree.
There must be no duplicate nodes.
Binary Search Trees (BSTs)
• Binary Search Tree Property:
The value stored at
a node is greater than
the value stored at its
left child and less than
the value stored at its
right child
Binary Search Tree Example
8
5
1
5
4 8 1
1 1
1 2 7 6
1 7 0 8
1
1 2
4
3 5 0
NOT A 2
BINARY SEARCH TREE BINARY SEARCH TREE 1
• Properties of Binary Search Tree provides an ordering among keys so
that the operations like search, minimum and maximum can be done
fast.
• If there is no ordering, then we may have to compare every key to
search for a given key.
Binary Search Trees (BSTs)
Where is the
smallest element?
Ans: leftmost element
12/10/2021 38
Preorder Traversal
void preorder(Node root)
{
if (root != null)
{
printf(“%d”, root->data);
preorder(root->left);
preorder(root->right);
}
}
12/10/2021 39
Postorder Traversal
void postorder(Node root)
{
if (root != null) {
postorder(root->left);
postorder(root->right);
printf(“%d”, root->data);
}
12/10/2021 40
Application of Traversal Sorting a
BST
• Observe the output of the inorder traversal of
the BST example two slides earlier
• It is sorted
• As a general rule, if you output the keys (data)
of the nodes of a BST using inorder traversal,
the data comes out sorted in increasing order
CS 103 44
Find the inorder ,preorder,postorder for the
following BST
Tree
Traversals:
another
example
Delete Operation in BST
• First, find the item; then, delete it
• Binary search tree property must be preserved!!
• We need to consider three different cases:
(1) Deleting a leaf
(2) Deleting a node with only one child
(3) Deleting a node with two children
(1) Deleting a leaf
(2) Deleting a node with
only one child
Inorder predecessor
Inorder successor
(3) Deleting a node with two
children(find max in left subtree)
(3) Deleting a node with two
children(find min in right subtree)
After deletion
Before deleting 15
Try the implementation!!!!
Expression Trees
• A binary expression tree represents an arithmetic
expression.
• In an expression tree each operator is an interior node
whose children are operands or subexpressions. Operands
are in leaf nodes.
Algorithm to Generate Expression Tree from
Infix expression
1) Initialize Two stack 1) tree stack 2) operator stack
2) Scan the infix expression from Left to Right.
3) If scanned symbol is
->operand: construct a node, push node on to the tree stack
->operator:
a. if operator stack is empty
then construct a node and push the node on
operator stack
b. else if operator stack is not empty
Algorithm to Generate Expression Tree from Infix
expression
Preorder(Prefix): - + a / * b c d e
Inorder(Infix): a + b * c / d - e
Postorder(Postfix): a b c * d / + e -