Trees second part in data structures with examples
rupanaveen24
42 views
95 slides
May 19, 2024
Slide 1 of 95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
About This Presentation
Trees 2nd part in data structures
Size: 1.55 MB
Language: en
Added: May 19, 2024
Slides: 95 pages
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 +)
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.