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

Data sturcture and algorithm week 8

Uploaded by

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

Data sturcture and algorithm week 8

Uploaded by

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

ASSIGNMENT WEEK – 8

Name – HIMANSHU RAJ

Register No. - EA2331201010152

Program - B.C.A.-Data Science

Subject – Data Structure & Algorithm (DSA)

Semester – II Semester

EA2331201010152
1. Construct an expression tree for the following expression. (5+9) / (2*7)

Ans. The expression tree for the expression (5+9) / (2*7):

/
/ \
+ *
/ \ / \
5 9 2 7

Explanation:

1. The main operator is the division (/). It becomes the root node of the tree.
2. The left subtree represents the numerator, which is the addition (+) of 5 and 9.
o The addition node becomes the left child of the root.
o The operands 5 and 9 become the left and right children of the addition node,
respectively.
3. The right subtree represents the denominator, which is the multiplication (*) of 2 and
7.
o The multiplication node becomes the right child of the root.
o The operands 2 and 7 become the left and right children of the multiplication
node, respectively.

This tree structure reflects the order of operations (division happens after addition and
multiplication).

2. Check the following Binary tree is balanced or not.

Ans. Balanced Binary Tree:

A binary tree is considered balanced if the heights of the left and right subtrees of any node
differ by at most 1. This ensures efficient searching and operations in the tree.

1. Identify the Root Node: Locate the node with no parent (usually at the top).
2. Check Subtree Heights: Recursively calculate the heights of the left and right
subtrees for each node. The height of a subtree is the maximum number of edges from
the node to a leaf node (a node with no children).
3. Compare Heights: For each node, compare the absolute difference between the
heights of its left and right subtrees. If the difference is greater than 1, the tree is not
balanced.

EA2331201010152
4. Base Case: If a node is a leaf node (has no children), its height is considered 0.

Example:

If the root node in the image has a left subtree height of 2 and a right subtree height of 3, the
tree is not balanced (absolute difference of 1 exceeds the limit).

EA2331201010152

You might also like