0% found this document useful (0 votes)
25 views

2 - 4 Expression Trees and BST

The document discusses expression trees and binary search trees (BSTs). It describes how expression trees can be used to represent mathematical expressions and how they can be traversed. It also explains how to convert between postfix, prefix, and infix notations using expression trees. The document then defines BSTs and describes common operations on them like insertion, searching, and deletion of nodes. It provides algorithms for inserting a new node and deleting a node in a BST, covering the different cases involved.

Uploaded by

bhavya mittal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

2 - 4 Expression Trees and BST

The document discusses expression trees and binary search trees (BSTs). It describes how expression trees can be used to represent mathematical expressions and how they can be traversed. It also explains how to convert between postfix, prefix, and infix notations using expression trees. The document then defines BSTs and describes common operations on them like insertion, searching, and deletion of nodes. It provides algorithms for inserting a new node and deleting a node in a BST, covering the different cases involved.

Uploaded by

bhavya mittal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

11/3/2022

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

Conversion of Postfix Expression into an


Expression Tree
■ An expression tree can be generated for the infix and postfix expressions.
■ An algorithm to convert a postfix expression into an expression tree is as
follows:
1. Read the expression one symbol at a time.
2. If the symbol is an operand, we create a one-node tree and push a pointer
to it onto a stack.
3. If the symbol is an operator, we pop pointers to two trees T1 and T2 from
the stack (T1 is popped first) and form a new tree whose root is the
operator and whose left and right children point to T2 and T1 respectively.
A pointer to this new tree is then pushed onto the stack.

2
11/3/2022

Converting Expressions with Expression Trees


■ Postfix to Infix: The following algorithm works for the expressions whose infix form does not
require parenthesis to override conventional precedence of operators.
– Create the expression tree from the postfix expression
– Run inorder traversal on the tree.
■ Postfix to Prefix: The following algorithm works for the expressions to convert postfix to prefix:
– Create the expression tree from the postfix expression
– Run preorder traversal on the tree.
■ Prefix to Infix: The following algorithm works for the expressions whose infix form does not
require parenthesis to override conventional precedence of operators.
– Create the expression tree from the prefix expression
– Run inorder traversal on the tree.
■ Prefix to postfix: The following algorithm works for the expressions to convert postfix to prefix:
– Create the expression tree from the prefix expression
– Run postorder traversal on the tree.
5

Binary Search Tree (BST)


■ A binary search tree is a binary
tree.
■ It may be empty.
■ If it is not empty, then it satisfies
the following properties:
a) The key in the left child of a
node (if exists) is less than
the key in its parent node.
b) The key in the right child of a
node (if exists) is greater
than the key in its parent
node.
c) The left and right subtrees of
the root are again binary
search trees.

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

Insertion Operations on BST


■ A binary search tree is constructed by the repeated insertion of new nodes into a binary
tree structure.
■ Insertion must maintain the order of the tree.
■ The value to the left of a given node must be less than that node and value to the right
must be greater.
■ In inserting a new node, the following two tasks are performed :
– Tree is searched to determine where the node is to be inserted.
– completion of search, the node is inserted into the tree
Steps:
1. Start from root.
2. Compare the inserting element with root, if less than root, then recurse for left, else
recurse for right.
3. After reaching end, just insert that node at left(if less than current) else right.

4
11/3/2022

Deletion Operations on BST


■ The algorithm to delete a node with key from a binary search tree is
not simple whereas many cases needs to be considered.
– If the node to be deleted has no sons, then it may be deleted
without further adjustment to the tree.
– If the node to be deleted has only one subtree, then its only son
can be moved up to take its place.
– The node p to be deleted has two subtrees, then its inorder
successor s must take its place. The inorder successor cannot
have a left subtree. Thus, the right son of s can be moved up to
take the place of s.

Deletion Operations on BST (Contd…)


When we delete a node, three possibilities arise.
1) Node to be deleted is leaf: Simply remove from the tree.

50 50
/ \ delete(20) / \
30 70 ---------> 30 70
/ \ / \ \ / \
20 40 60 80 40 60 80

10

5
11/3/2022

Deletion Operations on BST (Contd…)


2) Node to be deleted has only one child: Copy the child to the node and delete the
child

50 50
/ \ delete(30) / \
30 70 ---------> 40 70
\ / \ / \
40 60 80 60 80

11

Deletion Operations on BST (Contd…)


3) Node to be deleted has two children: Find inorder successor of the node. Copy
contents of the inorder successor to the node and delete the inorder successor.
Note that inorder predecessor can also be used.

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

You might also like