You are on page 1of 73

Data Structure &

Algorithms – CSC 221


Lecture 10
Instructor: Dr. Muhammad Asfand-e-yar

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Recall
A binary tree is a tree in which each node has at most 2
children

Array-Based Implementation:
An array can be used to store some binary trees. Number
the nodes level by level, from left to right,
0
O

1 2
M T

3 4 5 6
C E • P U


Store node # 0 in array location 0, node #1 in array location 1, etc.

i 0 1 2 3 4 5 6 . ..
t [i ] O M T C E P U . ..
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
E
But, unless each level of the tree is full
so there are no "dangling limbs," there C M

can be much wasted space in the array.


U
For example, this binary tree
contains the same characters as T

63 array
before but requires ___
P
positions for storage:
Max # nodes on level i: O

2i i 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
In array representation, children of i t[ i] E C M U T
are at:
2i + 1, 2i + 2 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
P
Parent of i is at:
(i - 1) / 2 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 …
i is the index of the a Parent node O …

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Linked Implementation:
Use nodes of the form
data

left right
root
Left child Right child
and maintain a pointer to the root. 75

75
60 80
60 80

58 65 92 58 65 92

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
C++ Implementation:
class BinaryTree
{
private:
class Node
{
public:
int data;
Node * left, * right;
};
Node * root; // pointer to root node
};
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Implementing Tree
Traversals

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Tree Traversal

Preorder:
root – left – right
Postorder:
left – right – root

Inorder:
left – root – right
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Implementing Tree Traversals
void Preorder(Node * ptr)
{
if(ptr!=NULL)
{
cout << ptr->data << “ “;
Preorder(ptr->left);
Preorder(ptr->right);
}
}
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Implementing Tree Traversals
void Postorder(Node * ptr)
{
if(ptr!=NULL)
{
Postorder(ptr->left);
Postorder(ptr->right);
cout << ptr->data << “ “;
}
}
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Implementing Tree Traversals
void Inorder(Node* ptr)
{
if(ptr!=NULL)
{
Inorder(ptr->left);
cout << ptr->data << “ “;
Inorder(ptr->right);
}
}
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Traversal exercises: finding output
14
15 void Traversal(Node* temp)
4 {
3 18 if(temp!=NULL)
9 14 {
20 cout<<temp->data;
7 9 16 Traversal(temp->left);
5 Traversal(temp->right);
17 cout<<temp->data;
4 5 }
}

14 4 3 3 9 7 5 4 4 5 5 5 7 9 9 9 4 15 14 14 18 16 17 17 16 20 20 18 15 14
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Traversal exercises: finding output
void Traversal(Node* temp, int count) {
if(temp!=NULL) {
14
if (count%2 == 0)
15
4 cout<<temp->data;
3 18
9 14 count++;
20 Traversal(temp->left, count);
7 9 16
Traversal(temp->right, count);
5
17 }
4 5 }

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Binary Search Tree

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Binary Search Trees

A Binary Search Tree (BST) is a binary tree in which the value in each
node is greater than all values in its left sub-tree and less than all values
in its right sub-tree.

75

60 80

58 65 92
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
BST As ADT
Collection of Data Elements:
A binary tree in which for each node x:
value in left child of x < value in x < value in right child of x

Basic Operations:
1. Construct an empty BST
2. Determine if the BST is empty
3. Search the BST for a given item
4. Insert an item in the BST maintaining the BST property
5. Delete an item from the BST maintaining the BST property
6. Traverse the BST

An inorder traversal of a BST retrieves values in ascending order.

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Examples

A binary search tree Not a binary search tree

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Example Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Example Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14

15

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Example Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14

4 15

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Example Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14

4 15

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Example Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14

4 15

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Example Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14

4 15

9 18

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Example Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14

4 15

3 9 18

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Example Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14

4 15

3 9 18

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Example Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14

4 15

3 9 18

16
7

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Example Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14

4 15

3 9 18

16
7

4
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Example Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14

4 15

3 9 18

16 20
7

4
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Example Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14

4 15

3 9 18

16 20
7

17
5

4
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Example Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14

4 15

3 9 18

16 20
7 9

17
5

4
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Example Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14

4 15

3 9 14 18

16 20
7 9

17
5

4
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Example Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14

4 15

3 9 14 18

16 20
7 9

17
5

4 5
“Binary Search Tree” of the given data set
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Implementing a BST
class Node
{
public:
int data;
Node *left, *right;

};

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Implementing a BST
Node()
{
left = right = NULL;
}

Node(int item)
{
data = item;
left = right = NULL;
}

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Implementing a BST
class BinaryTree
{
private:
Node *root; // pointer to root node
public:
BinaryTree();
bool Empty();

};

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Implementing a BST
BinaryTree() bool Empty( )
{ {
root = NULL; if (root == NULL)
} return true;
else
return false;
}

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Implementation of a bst
bool Search(Node * , int);
void Insert(int);
void Delete(Node *);

void InorderTraversal(Node *);


void PreorderTraversal(Node *);
void PostorderTraversal(Node *);

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Searching in Binary Search Tree (BST)

Three steps of searching


• The item which is to be searched is compared with the root node. If the
item is equal to the root, then we are done.
• If its less than the root node then we search in the left sub-tree.
• If its more than the root node then we search in the right sub-tree.

The above process will continue till the item is found or you
reached end of the tree.

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Implementing Binary Search Tree (BST)
1. Set pointer locPtr = root.
2. Repeat the following:
if locPtr is null
return false
if value < locPtr->data
locPtr = locPtr->left
else if value > locPtr->data
locPtr = locPtr->right
else
return true
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Implementing Binary Search Tree (BST)
bool Search(Data data) If(data < ptr->data)
{ ptr = ptr->left;
Node * ptr = root; else if (data > ptr->data)
bool found = false; ptr = ptr->right;
else
for( ; ; ) {
found = true;
If(found || ptr == NULL)
}
break; return found;
}
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Implementing BST search - recursive
void Search(Node* temp, int num)
{
if(temp==NULL)
cout<<“Number not found";
else if(temp->data == num)
cout<<"Number found";
else if(temp->data > num)
Search(temp->left, num);
else if(temp->data < num)
Search(temp->right, num);
}
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Exercise – BST
Write a function that finds a node from the tree and returns its parent’s address

Node* getParent(Node* curr, int num)


{
if((curr->left->data == num) ||
(curr->right->data == num))
return curr;
else if(num < curr->data)
return getParent(curr->left, num);
else //if(num > curr->data)
return getParent(curr->right, num);
}
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
What does the following do?

Node* getParent(Node* curr, node* prev, int num)


{
if(curr->data == num)
return prev;
else if(num < curr->data)
retun getParent(curr->left, curr, num);
else //if(num > curr->data)
return getParent(curr->right, curr, num);
}

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Exercise
Write a function that finds the node with minimum value.

Node* FindMin(Node* curr)


{
if(curr->left == NULL)
return curr;
else
return FindMin(curr->left);
}

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Insertion in BST

Three steps of insertion


1. If the root of the tree is NULL then insert the first node and root points to that node.
2. If the inserted number is lesser than the root node then insert the node in the left
sub-tree.
3. If the inserted number is greater than the root node then insert the node in the right
sub-tree.

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Modify the search algorithm so that a pointer parentPtr trails locPtr
down the tree, keeping track of the parent of each node being
checked:
1. Initialize pointers locPtr = root, parentPtr = NULL
2. While locPtr ≠ null pointer:
a. parentPtr = locPtr
b. if value < locPtr->data
locPtr = locPtr->left
else if value > locPtr->data
locPtr = locPtr->right
else
value is already in the tree; return a found indicator
3. Get a new node pointed to by newPtr, put the value in its
data part, and set left and right to null.
4. if parentPtr = null pointer // empty tree
Set root = newptr.
else if value < parentPtr->data
Set parentPtr->left = newPtr.
else
Set parentPtr->right = newPtr.
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
void Insert( int value) {
Node * ptr = root;
Node * prev = 0;
while (ptr!=0) {
prev = ptr;
if (value < ptr->data)
ptr = ptr->left;
else if (value > ptr->data)
ptr = ptr->right;
else if (value == ptr->data)
{cout<<"Value already exist"; return ;}
}
TNode * temp = new TNode;
temp->data=value; temp->left=0; temp->right=0;
if(prev==0)
root = temp;
else if (value < prev->data)
prev->left = temp;
else
prev->right = temp;
BS (IT) – Data}Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Deletion in BST

When we delete a node, we need to consider how we take care


of the children of the deleted node.

This has to be done such that the property of the search tree is
maintained.

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Deletion in BST

Deletion
To delete a node x from a BST, we have three cases:
1. x is leaf
2. x has one child
3. x has two children

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Deletion in BST
CASE 1:
 x is a leaf

Simply make the appropriate pointer in x’s parent a null pointer

75

60 80

x 58 65 92

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Deletion in BST
Make the left pointer of x’s parent null

Free x 75

60 80

x 58 65 92

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Deletion in BST
CASE II:
x is has one child
Set the appropriate pointer in x’s parent to point to this child
75

60 80

58 65 x 92

62

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Deletion in BST
75

60 80

58 65 x 92

62

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Deletion in BST
CASE III:
 x has two children
 Replace the value stored in node x by its in-order successor.
 Delete this successor

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Deletion in BST
Node to be removed has two children. This is the most complex case.
To solve it, let us see one useful BST property first.

5 x

2 21

-4 3
19 25

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Deletion in BST
5 x

2 21

-4 3
19 25

To transform first tree into second one, we can do following:


1. choose minimum element from the right subtree (19 in the example);
2. replace 5 by 19;
3. delete the 19 as a duplicate child node.
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Deletion in BST
5 x

21

19 25

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Deletion in BST
19

21
2

-4 3 19 x 25

The following approach can be utilized to remove a node, which has two children:
1. find a minimum value in the right subtree;
2. replace value of the node to be removed with found minimum. Now, right subtree contains a
duplicate!
3. apply remove to the right subtree to remove a duplicate.

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Deletion in BST
G

F J x

A
H O

I M P

xSucc K N

L
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Deletion in BST
G

F K x

A
H O

I M P

xSucc K N

L
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Deletion in BST
G

F K x

A
H O

I M P

xSucc K N

L
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Deletion in BST
G

F K

A
H O

I M P

L N

Node “J” is deleted from the Binary Search Tree.


BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Deletion in BST Code
void DeleteNode (Node* temp, int num) { // if number is found at a leaf node
//the number (i.e. num) will be searched in if((temp->left == NULL) &&
//this function. If the num is not found than
//the last condition will be check and ( temp->right == NULL)) {
//recall the (DeleteNode) function. parent=GetParent(root, temp->data);
if (temp==NULL) if(parent->left == temp)
cout<<"Number not Found"; parent->left= NULL;
else if((temp->data == num)) { else if (parent->right == temp)
//node found parent->right = NULL;
Node *parent, *min ; delete temp;
int number; }

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
SCENARIO: if(temp->left != NULL)cc

Temp could be a right son Temp could be a left son

75 75

parent 60 80 parent 60 80

58 65 temp 92 temp 58 65 92

62 52

if (parent->right == temp) if(parent->left == temp)


parent->right = temp->left; parent->left = temp->left;

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
// if node to be deleted has one child
else if(((temp->left == NULL) && (temp->right != NULL)) ||
((temp->left != NULL) && (temp->right == NULL))) {
parent = GetParent(root, temp->data);
if(temp->left != NULL) {
if(parent->left == temp)
parent->left = temp->left;
else if (parent->right == temp)
parent->right = temp->left; }

}
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
SCENARIO: if(temp->right != NULL)

Temp could be a right son Temp could be a left son

75 75

parent 60 80 parent 60 80

58 65 temp 92 temp 58 65 92

66 59

if (parent->right == temp) if(parent->left == temp)


parent->right = temp->right; parent->left = temp->right;

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
// if node to be deleted has one child
else if(((temp->left == NULL) && (temp->right != NULL)) ||
((temp->left != NULL) && (temp->right == NULL))) {
parent = GetParent(root, temp->data);
if(temp->left != NULL) {
if(parent->left == temp)
parent->left = temp->left;
else if (parent->right == temp)
parent->right = temp->left; }
else if(temp->right != NULL) {
if(parent->left == temp)
parent->left = temp->right;
else if (parent->right == temp)
parent->right = temp->right; }
delete temp;
}
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
//if node to be deleted has two children
else if((temp->left != NULL) && (temp->right != NULL))
{
min = FindMin(temp->right); //will return the min. node found in Right tree
number = min->data;
DeleteNode(temp->right, min->data); //calling to itself recursively
temp->data= number;
}
} // end node found

else if (num < temp->data)


DeleteNode(temp->left, num); //calling to itself recursively
else //if (number > temp->data)
DeleteNode(temp->right, num); //calling to itself recursively
}

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Conversion of an expression to tree
1. Scan the postfix expression from left to right.
2. Create a node Curr
3. Get the next symbol from the expression.
4. If the symbol is an operand
a) Set this operand as data member of the node Curr
b) Push the node on the stack
5. If the symbole is an operator
a) T1 = Pop()
b) T2 = Pop()
c) Attach T1 to the left and T2 to the right of Curr
d) Set the operator as data member of the node Curr
e) Push Curr (with child nodes attached) onto the stack
6. Repeat Steps I-V till end of expression
7. Pop the (only remaining) node from the stack which is a pointer to the root of the
expression tree.
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Example: Conversion to tree
Expression: a b + c d e + * *

‘a’ is an operand, a one-node tree is created


and pushed on the stack

‘b’ is again an operand, and is pushed on a b


the stack

Next, a ‘+’ is read, so two pointers to trees


are popped, a new tree is formed, and a +
pointer to it is pushed onto the stack.
a b

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Example: Conversion to tree
Expression: a b + c d e + * *

Next, c, d, and e are read, and for each a


one-node tree is created and a pointer
to the corresponding tree is pushed + c d e
onto the stack.
a b

Now a ‘+’ is read, so two trees are merged.

+ c +

a b d e

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Example: Conversion to tree
Expression: a b + c d e + * *

Continuing, a ‘*’ is read, so we pop two tree pointers and form a


new tree with a ‘*’ as root.

+ c + + *

a b d e a b c +

d e

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Example: Conversion to tree
Expression: a b + c d e + * *

Finally, the last symbol is read, two trees are merged, and a pointer
to the final tree is left on the stack.

+ * *

a b c + + *

d e a b c +

d e

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Q&A
That’s all for today’s Lecture

BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad

You might also like