2 - 4 Expression Trees and BST
2 - 4 Expression Trees and BST
CIC – 209:
DATA STRUCTURES
Unit – II
Expression Trees and Binary Search Trees
Expression Trees
■ Expression tree is a binary tree, because all of the operations are binary.
■ It is also possible for a node to have only one child, as is the case with the
unary minus operator.
■ The leaves of an expression tree are operands, such as constants or variable
names, and the other (non leaf) nodes contain operators.
■ Once an expression tree is constructed, we can traverse it in three ways:
– Inorder Traversal
– Preorder Traversal
– Postorder Traversal
1
11/3/2022
Expression Trees
2
11/3/2022
3
11/3/2022
Operations on BST
■ Creation of an empty tree
■ Traversing the BST
■ Counting internal nodes (non-leaf nodes)
■ Counting external nodes (leaf nodes)
■ Counting total number of nodes
■ Finding the height of tree
■ Insertion of a new node
■ Searching for an element
■ Finding smallest element
■ Finding largest element
■ Deletion of a node.
7
4
11/3/2022
50 50
/ \ delete(20) / \
30 70 ---------> 30 70
/ \ / \ \ / \
20 40 60 80 40 60 80
10
5
11/3/2022
50 50
/ \ delete(30) / \
30 70 ---------> 40 70
\ / \ / \
40 60 80 60 80
11
50 60
/ \ delete(50) / \
40 70 ---------> 40 70
/ \ \
60 80 80
The important thing to note is, inorder successor is needed only when right child is
not empty. In this particular case, inorder successor can be obtained by finding the
minimum value in right child of the node.
12