Trees second part in data structures with examples

rupanaveen24 42 views 95 slides May 19, 2024
Slide 1
Slide 1 of 95
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89
Slide 90
90
Slide 91
91
Slide 92
92
Slide 93
93
Slide 94
94
Slide 95
95

About This Presentation

Trees 2nd part in data structures


Slide Content

IMPLEMENT AN EXPRESSION TREE USING TREE DATA
STRUCTURE.
• Expression Trees
• It is a special type of binary tree used to evaluate the expressions
• Expression tree is a binary tree in which each internal node corresponds to operator
and each leaf node corresponds to operand
• An Expression is composed of constants, variables and symbols (operators) which are
arranged in a specific format (according to some mathematical rules)
• Higher precedence operators is near to leaf and lower precedence operators is near
to root

EXPRESSION TREE
• One of the widely used application of Binary tree is the expression tree.
• Arithmetic expressions can be represented using binary trees.
• Expression: Made up of operands, operators and constants.
• Eg: A + B * C, a * c + 7, 2 + 5 * 6, etc.
Properties of an Expression Tree:
1. Root and all the internal nodes hold an operator
2. All the leaf nodes hold an operand/constant
3. Each subtree is an expression tree

STRUCTURE OF AN EXPRESSION TREE
• Expression tree for 3 + ((5+9)*2) would be:

TRAVERSAL ON EXPRESSION TREE
• Inorder traversal on an expression tree generates Infix expression
• (A + B)
• Preorder traversal on an expression tree generates Prefix expression
• (+ A B)
• Post-order traversal on an expression tree generates Postfix expression
(A B +)

• Infix:
(3 * 7) + 9
• Prefix:
+ * 3 7 9
• Postfix:
3 7 * 9 +
• Expression evaluation:
30

EXAMPLE

CONTD..
• An expression with negative integer (-2 * 7) – (4 ^ 2) can be
represented as:

• Find the infix, prefix
and postfix
expressions from the
given tree :
• Infix:
• Prefix:
• Postfix:
FORMATIVE ASSESSMENT
Q1

• Evaluate the given tree : • Postfix:
• Evaluated result:
• Balanced or not:

Q2

EXAMPLE

• Preorder Traversal ?????? Prefix expression
• Postorder Traversal ?????? Postfix expression

CONSTRUCTION OF EXPRESSION TREE
• For constructing expression tree, Stack is used.
• Loop through input expression and do following steps for every
character:
1. If character is operand push that into stack
2. If character is operator pop two values from stack make them its child
(top-right child;second top-left child) and push the subtree into stack again.
• At the end only element of stack will be root of expression tree.

BINARY EXPRESSION TREE FROM POSTFIX EXPRESSION

CREATING BINARY SEARCH TREE

BINARY SEARCH TREES
• Another important application of binary trees is their use in searching.
• Binary tree used for searching – Binary Search Tree (BST)
Properties of BST:
1. All the nodes in the left subtree have values less than root node
2. All the nodes in the right subtree have values greater than root node
3. Every subtree is a BST

STRUCTURE OF BST

BINARY SEARCH TREE

CONT..

DIFFERENCE BETWEEN BINARY TREE AND BINARY SEARCH
TREE

EXAMPLE

Q5
• The given trees are BSTs: True or False?

BST OPERATIONS

BST TRAVERSAL

PREORDER TRAVERSAL

POSTORDER TRAVERSAL

INORDER TRAVERSAL





























In-order traversal on BST generates
sorted order of the elements

BST SEARCH
• In a binary search tree, the search operation is performed
with O(logn) time complexity.
• The search operation is performed as follows
• Step 1 - Read the search element from the user.
• Step 2 - Compare the search element with the value of root node in the tree.
• Step 3 - If both are matched, then display "Given node is found!!!" and terminate the
function
• Step 4 - If both are not matched, then check whether search element is smaller or
larger than that node value.
• Step 5 - If search element is smaller, then continue the search process in left subtree.

BST SEARCH
• Step 6- If search element is larger, then continue the search process in right subtree.
• Step 7 - Repeat the same until we find the exact element or until the search element
is compared with the leaf node
• Step 8 - If we reach to the node having the value equal to the search value then
display "Element is found" and terminate the function.
• Step 9 - If we reach to the leaf node and if it is also not matched with the search
element, then display "Element is not found" and terminate the function.

EXAMPLE
• Find node with value 2 in the given tree:

ANOTHER EXAMPLE

CONTD..
• The smallest element of a BST is the leftmost element of the tree and the
largest element is the rightmost one:

Q7
• Find the largest node and second largest node
in the given BST:

ANSWER

Q6
• Find the largest node and second largest node in the
given BST:

ANSWER



























Second
largest

BST CREATION
• Given array:
• Step 1:

• Step 2:

CONTD..
• Step 3:


• Step 4:

• Step 5:
CONTD..

• Step 6:
CONTD..

Q8
• Create BST using the values:
43, 10, 79, 90, 12, 54, 11, 9, 50

ANSWER

BST INSERTION
• In a binary search tree, the insertion operation is performed with O(log
n) time complexity. In binary search tree, new node is always inserted as
a leaf node. The insertion operation is performed as follows...
• Step 1 - Create a newNode with given value and set its left and right to NULL.
• Step 2 - Check whether tree is Empty.
• Step 3 - If the tree is Empty, then set root to newNode.

BST INSERTION
⦿ Step 4 - If the tree is Not Empty, then check whether the value of newNode
is smaller or larger than the node (here it is root node).
• Step 5 - If newNode is smaller than or equal to the node then move to its left child. If
newNode is larger than the node then move to its right child.
• Step 6- Repeat the above steps until we reach to the leaf node (i.e., reaches to
NULL).
• Step 7 - After reaching the leaf node, insert the newNode as left child if the
newNode is smaller or equal to that leaf node or else insert it as right child.

BST INSERTION

BST INSERTION

BST INSERTION

BST INSERTION

BST INSERTION

BST INSERTION

BST DELETION
• In a binary search tree, the deletion operation is performed
with O(log n) time complexity. Deleting a node from Binary search
tree includes following three cases...
• Case 1: Deleting a Leaf node (A node with no children)
• Case 2: Deleting a node with one child
• Case 3: Deleting a node with two children

• Algorithm: Delete (TREE, ITEM)
• Step 1: IF TREE = NULL
Display "item not found in the tree"
ELSE IF ITEM < TREE -> DATA
Delete(TREE->LEFT, ITEM)
ELSE IF ITEM > TREE -> DATA
Delete(TREE -> RIGHT, ITEM)
ELSE IF TREE -> LEFT AND TREE -> RIGHT
SET TEMP = findLargestNode(TREE -> LEFT)
SET TREE -> DATA = TEMP -> DATA
Delete(TREE -> LEFT, TEMP -> DATA)
ELSE
SET TEMP = TREE
IF TREE -> LEFT = NULL AND TREE -> RIGHT = NULL
SET TREE = NULL
ELSE IF TREE -> LEFT != NULL
SET TREE = TREE -> LEFT
ELSE
SET TREE = TREE -> RIGHT
[END OF IF]
FREE TEMP
[END OF IF]
• Step 2: END

CASE 1: DELETING A LEAF NODE

• We use the following steps to delete a leaf node from BST...
• Step 1 - Find the node to be deleted using search operation
• Step 2 - Delete the node using free function (If it is a leaf) and terminate the
function.

BST DELETION

BST DELETION

BST DELETION

BST DELETION

BST DELETION

CASE 2: DELETING A NODE WITH ONE CHILD
• We use the following steps to delete a node with one child from BST...
• Step 1 - Find the node to be deleted using search operation
• Step 2 - If it has only one child then create a link between its parent node and
child node.
• Step 3 - Delete the node using free function and terminate the function.

BST DELETION

BST DELETION

BST DELETION

CASE 3: DELETING A NODE WITH TWO CHILDREN
• We use the following steps to delete a node with two children from BST...
• Step 1 - Find the node to be deleted using search operation
• Step 2 - If it has two children, then find the largest node in its left subtree (inorder
predecessor) (OR) the smallest node in its right subtree (inorder successor).
• Step 3 - Swap both deleting node and node which is found in the above step.
• Step 4 - Then check whether deleting node came to case 1 or case 2 or else goto step 2
• Step 5 - If it comes to case 1, then delete using case 1 logic.
• Step 6- If it comes to case 2, then delete using case 2 logic.
• Step 7 - Repeat the same process until the node is deleted from the tree.

Method 1

After deletion and replacing

Method 2

After deletion and replacing

Method 3:
Tags