0% found this document useful (0 votes)
10 views71 pages

All Lectures Data Structure 2020 Makia Hamad Compressed السنة الجاية

The document discusses non-linear data structures, specifically trees and their types, including binary trees and binary search trees. It explains tree terminology, representations, and traversals, including in-order, pre-order, and post-order methods. Additionally, it covers applications of binary trees in representing arithmetic expressions and provides examples of traversals and expression trees.

Uploaded by

salam.ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views71 pages

All Lectures Data Structure 2020 Makia Hamad Compressed السنة الجاية

The document discusses non-linear data structures, specifically trees and their types, including binary trees and binary search trees. It explains tree terminology, representations, and traversals, including in-order, pre-order, and post-order methods. Additionally, it covers applications of binary trees in representing arithmetic expressions and provides examples of traversals and expression trees.

Uploaded by

salam.ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

Second Term ( Data structure and Algorithm Analysis )

Lecture 1 / 2 May 2020 / Non Linear Data Structures

1. Tree
2. Graph
3. Network

1. Tree

A Tree: is a branching structure such as following figure:

Root node A
Subtree

B C D

E F G H I

J K
Leaves nodes Leaves nodes

 The point at which lines come together in a tree called “nodes”.


 The top most node at the tree is called a “root” and the bottom most node
is called a “leaf”. The lines connecting the nodes are called “branches”.
 ;A node is said to be the “parent” of these bellow it immediately which are
said to be “children”, children at the same parent called “brothers” or
“siblings” or “twins”.
 Any node of the tree is itself a tree, which is said to be “sub tree” at the origin
tree.
 The entire tree is sub tree of itself.
 Each node is a sub tree consisting of only a single node.
 Tree can be represented in a computer memory by linked structure that
corresponds directly to the diagram used to represent trees on printed page.
 We say that a node is visited when that node is processed. A visit to every
node in a tree is said to be “traversed”.
 The direction from the node to the leaf is down and the opposite direction is
up. Coming from the leaves to the root called “climbing” the tree while going
from the root to the leaf is called “descending” the tree.
 “Ancestor”, A node's parent is its first ancestor, the parent of the parent is
the next ancestor, and so on. The root is an ancestor of each other node.
 The “level” of a node refers to its distance from the root.
 Maximum number of levels is called the “depth” of tree .
 The “degree” of the node is the number of children that branched from it.
 The “degree” of the tree is the maximum degree of nodes.

 In tree below : Degree of node A= 2 , degree of node B=3, degree of node K=1
A

B C

D M N K L

V W S

General tree ( degree of tree= ? )


Binary Tree

A binary tree is a finite set of "nodes". The set might be empty (no nodes, which is
called empty tree). But if the set is not empty, it follows these rules:

There is one special node called the root.

1. Each node may be associated with up to two other different nodes, called
its “left child” and its “right child”. If a node c is the child of another node
p, then we say that "p is c's parent".
2. Each node, except the root, has exactly one parent; the root has no parent.

Sub trees

Binary Search Tree

Binary Search Tree: is a binary tree, in which left child (if any) of any node contains
a smaller value than does the parent node and the right child (if any) contains a larger
value than does the parent node.
Types of Binary Trees

1- Full Binary Tree: is a binary tree in which all of the leaves are on the same level
and every non leaf node has two children. The basic shape of a full binary tree is
triangular.

2- Complete Binary Tree: is the binary tree that is either full or full through the
next to the last level, which leaves on the last level as far left as possible. The shape
of complete binary tree is either triangular (if tree is full) or something like the
following:

3-Heap Tree: is a data structure that satisfies two properties one concerning it’s
shape: A heap tree must be a complete tree, and the other concerning the order of
its elements: For every node in the heap (Max Heap), the value stored in that node
is greater than or equal to the value in each of its children. The root node will always
contains the largest value in the heap and thus we always know where the
maximum value is in the root.

4-Strictly Binary Tree: is a binary tree in which each node except the leaf has two
children. A strictly binary tree which has (n) leaves always contains (2*n-1) nodes.

5-Balanced Binary Tree: In a binary tree, each node has a factor called “balance
factor”. Balance factor of a node is the height of the left subtree minus the height
of the right subtree. If each node in the binary tree has a balance factor equal to -
1 or 0 or 1 then this binary tree is called “balanced”.

*
+

+ /
-

+ ↑ E F

A B C D

4
a
2
b
2 5
c 0 0

d
1 2 4 5
e 8 5 0 5

7 a
2 3
3 0
a
b
3
7
b f
c

c m h l
d

d n x
e
500

300 200

A
100 130 120 160

D C

A
V M K

B C
W N S L

D M N K L

V W S
2
+

1 2
a *

* 1
/ ↑

+ ^ d *
b c
-

f e f
+ e /

2
f h
a b
c
1 2
33
4

23 54

15 30 44 60

20 58 67
8
Term 2 ( Data structure and Algorithm Analysis )

Lecture 2 / 9 may 2020


Application of binary tree
1- To represent any arithmetic expression.
2- To obtain sorted data by building Binary Search Tree (BST).
3- To find all duplicated data.
1- To represent any arithmetic expression:
Arithmetic expression can be represented as tree, such expression trees are
particularly interested because the prefix and postfix (suffix) notations for the arithmetic
expression correspond to important ways of traversing. When we use a binary tree to
represent an expression, parentheses are not needed to indicate precedence. The levels of
nodes in the tree indicate the relative precedence of evaluation implicitly. The operations
at higher levels of the tree are evaluated later than those below them. The operation at the
root will always be the last opearation performed.

Operator

Operand Operand
1 2

The simplest arithmetic expression consisting of single constant, The corresponding


tree consists of a single node.
Y=3
3

The simplest expression consisting of two constants compound by any operator such
as 3+5. We can represent the binary expression as a two level binary tree. The root
of tree is operator and its two children (subtrees) are operands.

3+5
3 5

A more complex expression can be represented as any root in a tree or any subtree
is operator and leaves contain operands (variables and constants).
The following examples show the representation of expressions as trees:-

1- 5+3*6-9 -
-

+ 9
-

5 *

3 6

2- a+(b–c)*d↑(e*f)
+

a *

- ↑

b c d *

e f

3- (a+b+c)*(e–f/h) *

+ -

+ c e /

b f h
a
c
Tree traversals
Tree traversals means visit all the nodes in a tree only once. There are 3 common ways
for traversals of binary tree: in-order traversal, pre-order traversal, and post-order
traversal.
1- Inorder traversal: each node is visited in between its left and right subtrees.
a. left subtree
A +
b. root
c. right subtree
B A C B C a b

a + b (infix expression)

2- Preorder traversal: each node is visited before its left and right subtrees.
a. Root
b. left subtree
c. right subtree
A B C
+ a b (prefix expression)

3- Postorder traversal: each node is visited after its left and right subtrees.
a. left subtree
b. right subtree
c. root
B C A
a b + (suffix expression)
Note that there are another methods to traverse general tree, a tree if we convert left by
right.
1- Converse inorder: each node is visited in between its right and left subtrees.
a. right subtree
b. root
c. left subtree
2- Converse preorder: each node is visited after its right and left subtrees.
a. Root
b. right subtree
c. left subtree
3- Converse postorder: each node is visited before its right and left subtrees.
a. right subtree
b. left subtree
c. root

Note that there are two methods depend on level concept to traverse general tree.
1- Level by Level:
A. Top-Down: Nodes are visited starting from top level (level 0) down to the
last level. In each level we start from left to right.
B. Bottom-Up: Nodes are visited from the last level up to the top level (level
0). In each level we start from left to right.

2- Converse Level by Level:


A. Top-Down: Nodes are visited starting from top level (level 0) down to the
last level. We start from right to left.
B. Bottom-Up: Nodes are visited from the last level up to the top level (level
0). In each level we start from right to left.
Example 1: Write all the traversals for the following trees:

Inorder: 18-20-22-25-30-33-37-40-50-55-70
30

Preorder: 30-20-18-25-22-50-40-33-37-55-70
20 50
00
Postorder: 18-22-25-20-37-33-40-70-55-50-30
18 25 40 55

22 33 70

Converse Inorder: 70-55-50-40-37-33-30-25-22-20-18


37
7
Converse Preorder: 30-50-55-70-40-33-37-20-25-22-18

Converse Postorder: 70-55-37-33-40-50-22-25-20-18-30

Level by Level (top-down): 30-20-50-18-25-40-55-22-33-70-37

Level by Level (bottom-up): 37-22-33-70-18-25-40-55-20-50-30

Converse Level by Level (top-down): 30-50-20-55-40-25-18-70-33-22-37

Converse Level by Level (bottom-up): 37-70-33-22-55-40-25-18-50-20-30


Example 2: Given the following tree, write the inorder, postorder and preorder traversals?

B C

R Z M L

H K

R B Z H A M C L K (inorder)
A B R Z H C M L K (preorder)
R H Z B M K L C A (postorder)

Example 3: ((A + B ) - ( C ↑ D )) + ( E / F )
Given the above expression, draw the expression tree then use the preorder and preorder
traversals to obtain prefix and suffix forms(notations)?
+
1
Inorder Traversal: A + B – C ↑ D + E / F
- /

Preorder Traversal: + - + A B ↑ C D / E F
+ ↑ E F

Postorder Traversal: A B + C D ↑ - E F / +

A B C D

Note that :
The preorder traversal obtain prefix form & postorder traversal obtain suffix form
The Inorder traversal give the original expression without parenthesis .
Example 4: Given the following tree, write the inorder, postorder, preorder traversals
and the original arithmetic expression?

A+B–C+B/Z↑R (inorder) +

AB+C–BZR↑/+ (postorder)
+-+ABC/B↑ZR (preorder) - /

+ C B ↑

A B Z R

((( A + B ) – C ) + ( B / ( Z ↑ R ))) (original arithmetic expression)

Example 5: Given the following tree:


Write the original arithmetic expression?

Solution:

+ 5
((( A * B ) – C ) + ( B / 3 )) ↑ 5
or - /
(A*B–C+B/3)↑5
(Remove not needed parenthesis)
* C B 3

Exercise:
A B
Write the suffix and prefix forms to
A given tree.
Example 6: Given the following trees, write the inorder, postorder and preorder
traversals?
1)
a
badhflck (inorder traversal)
bhlfdkca (postorder traversal) b c
abcdfhlk (preorder traversal)
d k

h l

2) a

dcbmanhxfl (inorder traversal)


b f
dcmbnxhlfa (postorder traversal)
abcdmfhnxl (preorder traversal)
c m h l

d n x
a
3)
b

abcde (inorder traversal) c


edcba (postfix traversal)
abcde (prefix traversal) d

e
4)
a

edcba (inorder traversal) b


edcba (postfix traversal)
abcde (prefix traversal) c

Remark1: We can know the root from postorder traversal then we can know the
left subtree and the right subtree from the inorder traversal.

Remark2: If the tree is in one side left or right then there are two equal traversals:
1- If inorder = preorder means that the tree is on the right.
2- If inorder = postorder means that the tree is on the left.
3- If inorder=postorder=preorder means that the tree consisted from
one node.

Remark3: If the word left interchanged by right in the traversal, we obtain


convert inorder, converse preorder and converse postorder.

Note that: We can drawing a tree from the suffix or prefix forms.
Exercises : Draw a trees corresponding to a given expressions ,

then perform its prefix (or suffix) forms in addition to the original expressions

1- A B + C – B Z R ↑ / +
2- G B H * + T W / M ^ -
3- + - A B * / C D ^ E F
4- * / ^ 8 H / - R B + C D ^ k 2
5- / / * T R + K L ^ – 10 R + C B

Note that : An expression tree must be strictly tree ? Why ?

Home work ( 1 )
Q2) Draw a binary tree to represent the expressions below ,
show how obtain the suffix and prefix forms ,
then allocate the tree in a suitable array.

1- (A+B/K) / L^(N* 5 *G)

2- ( A * B + K ) / 4 + ( N * L *G )

3- H^B /(E+ H/3)*N^5

4- A B / E H C / - * N R ^ +

5- - + + A* B C / D E

6- ( A / B + K ) ^ 4 ^ ( N - L *G )

state the relationship between parents and sons


Home work ( 1 )
Q1) Draw a binary tree to represent the expressions below
then write the original forms:

1- / + * A R ^ Z F - B ^ H L

2- Z R / B A * H / + M 2 ^ -

3- / + R 5 F * B - N * H L

4- A Z ^ B C * H / - L N / +

Q2) Draw a binary tree to represent the expressions below , show how obtain

the suffix and prefix forms , then allocate the tree in a suitable array.

1- (A+B/K) / L^(N* 5 *G)

2- ( A * B + K ) / 4 + ( N * L *G )

3- H^B /(E+ H/3)*N^5

4- A B / E H C / - * N R ^ +

5- - + + A* B C / D E

6- ( A / B + K ) ^ 4 ^ ( N - L *G )

state the relationship between parents and sons

Q) How a Binary tree represented in main memory ?


Ans. A binary tree can be represented in memory using two different methods
1- Sequential Representation ( Static Allocation )
2- Linked Representation ( Dynamic Allocation )
1-The Sequential Representation of Binary Tree
The complete binary Search Tree can be numbered from 1 to n so that the number
assigned to the left son is twice the number assigned to it’s parent and the number
assigned to the right son is one more than twice the number assigned to it’s parent. (i.e)
the node in position (p) is the parent of the nodes in position (2p) and (2p
+1). If the left child at position (p) then it’s right brother in position (p+1) and if the right
child at position (p) then it’s left brother in position (p-1). If any node in position (k) then
it’s parent in position trunc (k/2). Then we will store the tree in the array level by level
from left to right.
( the root in location 1 its left son in location 2 & its right son in location 3 and so on ).
Example 1:
N
A

Size of array = 2 - 1
Where N no. of levels B D
4

4 level , 2 -1 E H L
16-1 =15 location
F K M

A B D H L F K M
E

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Example 2: Represent a given the following tree in suitable array
A
The array representation of the tree
Array size = 15 location B C

R Z M L

A B C R Z M L … H … K
H K
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Example 3: ((A+B)-(C↑D))+(E/F)
Given the above expression, draw the expression tree then use the preorder and preorder
traversals to obtain prefix and suffix forms(notations)? Then allocate the tree in
Suitable array .

+
Inorder Traversal: A+B-C↑D+E/F 1

Preorder Traversal: +-+AB↑CD/EF - /


Postorder Traversal: AB+CD↑-EF/+
+ ↑ E F

A B C D

+ - / + ^ E F A B C D … …
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Example 4: Given the following tree, write the inorder, postorder, preorder traversals
and the original arithmetic expression?

+
A+B-C+B/Z↑R (inorder)
AB+C-BZR↑/+ (postorder) - /
+-+ABC/B↑ZR (preorder)
(((A+B)-C)+(B/(Z↑R))) (original arithmetic expression) + C B ↑

A B Z R

+ - / + C B ^ A B Z R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Example 5: Given the following tree, write the original


+ 5
Expression and allocate the tree in suitable array .
Solution :
- /
(((A*B)-C)+(B/3))↑5
(A*B-C+B/3)↑5
* C B 3

A B

^ + 5 - / * C B 3 … A B …
1 2 3 4 5 6 7 8 9 10 11 … 16 17 … 31

Note that : The binary tree can be obtained from its array representation

Example 6: Given the following array ,draw a binary tree then traversed it using
the inorder, postorder and preorder traversals?

a b c … … d k … f … h l …
1 2 3 4 5 6 7 13 26 27 … 31
Solution
a
badhflck (inorder traversal)
bhlfdkca (postorder traversal)
b c
abcdfhlk (preorder traversal)

d k

h l
Example 7:
Given the following arrays ,draw a corresponding binary tree then traversed it using
the inorder, postorder and preorder traversals?
A)

a b f c m h l d … … n x …
1 2 3 4 5 6 7 8 9 … 12 13 14 15

Solution : a

dcbmanhxfl (inorder traversal)


dcmbnxhlfa (postorder traversal) b f

abcdmfhnxl (preorder traversal)


c m h l

d n x

B)
N=5
a
N

The array size = 2 – 1


b
32- 1 = 31 location

a … b … c … d … e
d
1 2 3 … 7 … 15 … 31

e
abcde (inorder traversal)
edcba (postfix traversal)
abcde (prefix traversal)
C)

a b c … d … e … …
e
1 2 3 4 … 8 … 16 … 31

edcba (inorder traversal)


edcba (postfix traversal)
abcde (prefix traversal)
A
Linked Allocation of a Binary Tree
B C

R Z M L

A
H K

B C

/ R / / Z / M / / L

/ H / / K /

Note that :
Any binary tree contain N nodes have ( N+1 ) Null pointers
The tree above contain ( 9 ) nodes then it have (10 ) Null pointers
( 4 ) Level then the array size (15) location.

A B C R Z M L H K

1 2 3 4 5 6 7 … 11 … 15
The Static Allocation of the given tree
Root in location (1) its left sub tree in location (2) and right sub tree in location (3)
The children of node contain B in locations (4) and (5)
The children of node contain C in location (6) and (7)
The right child of node contain Z in location (11)
The right child of node contain L in location (15)
Q) Given a Binary tree with N levels , Count the minimum & maximum No. of :
Max. Min.
N

1- Nodes 2 -1 N ( Why ? )

N-1

2- Leaves (2 - 1 ) +1 1 ( Why ? )

3- No of empty sub trees 2 -1+1 N +1 ( Why ?)


( no. of Null pointers )

4- Interior nodes = No. of Nodes – No. of Leaves


N N-1

(2 -1) - ((2 - 1 ) +1 ) N–1

Sub trees

No. of Levels = 4 then


Max No. of Nodes = 15 Min No. of Nodes = 4
Max No. of leaves = 8 Min No. of Leaves = 1
Max No. of Interior nodes = 7 Min No. of Interior nodes = 3
Max No. of empty sub tree = 16 Min No. of empty sub tree = 5
Home Work :
3
0
Count Max . & Min. No. of
Nodes ,Leaves , Interior nodes , 2 5
0 0
empty sub trees .
then allocate the tree in a suitable 1 2 4 5
8 5 0 5
Array to the trees below :

3 7
3 0
a) b)
a
3
6
7
b f a

c m h l b

d n x c) c

d) d
2
0
a
e
1 2
7 40
c
e) 24
b

d
f) f

f l

y
0
1
Exercises : Draw a trees corresponding to a given expressions ,

then perform its prefix (or suffix) forms in addition to the original expressions

1- A B + C – B Z R ↑ / +
2- G B H * + T W / M ^ -
3- + - A B * / C D ^ E F
4- * / ^ 8 H / - R B + C D ^ k 2
5- / / * T R + K L ^ – 10 R + C B

Note that : An expression tree must be strictly tree ? Why ?

Home work ( 1 )
Q2) Draw a binary tree to represent the expressions below ,
show how obtain the suffix and prefix forms ,
then allocate the tree in a suitable array.

1- (A+B/K) / L^(N* 5 *G)

2- ( A * B + K ) / 4 + ( N * L *G )

3- H^B /(E+ H/3)*N^5

4- A B / E H C / - * N R ^ +

5- - + + A* B C / D E

6- ( A / B + K ) ^ 4 ^ ( N - L *G )

state the relationship between parents and sons

‫ من الممكن رسم األشجار بدون دوائر للسرعه وخصوصا باالمتحانات وكما مبين ادناه‬: ‫مالحظه‬
Exercises : +

1- A B + C – B Z R ↑ / + - /

+ C B ^

A B Z R

Prefix form : + - + A B C / B ^ Z R

Original Expression : (( A + B ) – C ) + ( B / ( Z^ R ) )

A+B–C+B/(Z^R)

2- G B H * + T W / M ^ - -

+ ^

G * / M

B H T W

Prefix form : - + G * B H ^ / T W M

Original exp. ( G + B* H ) – (( T / W ) ^ M )

G+B*H–(T/W)^M
3- + - A B * / C D ^ E F +
- *
A B / ^
C D E F
Suffix form : A B – C D / E F ^ * +
Original Exp. ( A – B ) + ( ( C / D ) * ( E ^ F ))
A–B+(C/D*(E^F))

4- * / ^ 8 H / - R B + C D ^ k 2 *
/ ^
^ / K 2
8 H - +
R B C D
Suffix form : 8 H ^ R B – C D + / / K 2 ^ *
Original Exp. (( 8 ^ H ) / (( R – B )/ ( C + D ))) * ( K ^ 2 )

5- / / * T R + K L ^ – 10 R + C B /
/ ^
* + - +
T R K L 10 R C B

Suffix form : T R * K L + / 10 R – C B + ^ /
Original Exp. (( T* R ) / ( K + L )) / (( 10-R ) ^ ( C + B ))
Home work solution ( Q2 )
1- (A+B/K) / L^(N* 5 *G)
/
+ ^
A / L *

B K * G

N 5
Preorder traversal give prefix form :
/ + A / B K ^ L * * N 5 G
postorder traversal give suffix form :
A B K / + L N 5 * G * ^ /
5

Array size = 2 - 1 = 32 -1
=31 location

/ + ^ A / L * … B K … * G … N 5 …
1 2 3 4 5 6 7 … 10 11 … 14 15 … 28 29 … 31
2- ( A * B + K ) / 4 + ( N * L *G )

/ *

+ 4 * G

* K N L

A B

Preorder traversal give prefix form :


+ / + * A B K 4 * * N LG
postorder traversal give suffix form :
A B * K + 4 /N L * G * +
5

Array size = 2 - 1 = 32 -1
=31 location

+ / * + 4 * G * K … N L … A B …
1 2 3 4 5 6 7 8 9 … 12 13 … 16 17 … 31
3- H ^ B / ( E + H / 3 ) * N ^ 5
*

/ ^

^ + N 5

H B E /

H 3

Preorder traversal give prefix form :


*/ ^ H B + E / H 3 ^ N 5

postorder traversal give suffix form :


H B^ EH 3 / + / N 5 ^ *

Array size = 2 - 1 = 32 -1
=31 location

* / ^ ^ + N 5 H B E / … H 3 …
1 2 3 4 5 6 7 8 9 10 11 … 22 23 … 31
4- A B / E H C / - * N R ^ +

* ^

/ - N R

A B E /

H C

Preorder traversal give prefix form :


+ * / A B – E / H C ^ N R

Add parenthesis for each sub tree to obtain the general form :
(( A / B ) * ( E – H / C )) + ( N ^ R )
Note that : The inorder traversal give the original but without parentheses
A/B*E–H/C+N^R

Array size = 2 - 1 = 32 -1
=31 location

+ * ^ / - N R A B E / … H C …
1 2 3 4 5 6 7 8 9 10 11 … 22 23 … 31

5 - + + A* B C / D E

Invalid expression ( no, of operator = no. of operands )


The valid expression must have operator less than operands by one.
6- ( A / B + K ) ^ 4 ^ ( N - G * L )
^

+ ^

/ K 4 -

A B N *

G L

Preorder traversal give prefix form :


^ + / A B K ^ 4 – N * G L
postorder traversal give suffix form :
A B / K + 4 N G L *_^ ^
Note that : the exponentiation is a right associative operator.

Array size = 2 - 1 = 32 -1
=31 location

^ + ^ / K 4 - A B … N * … G L
1 2 3 4 5 6 7 8 9 … 14 15 … 30 31

Q) State the relationship between parents and sons .

The answer : ( all the parents are operators and its children
( sons ) are operands.
The expression tree satisfy the equation below:
No. of nodes = ( 2 * No. of leaves -1 )
Home work solution ( Q1 )
Q1) Draw a binary tree to represent the expressions below
then write the original forms:

1- / + * A R ^ Z F - B ^ H L

2- Z R / B A * H / + M 2 ^ -

3- / + R 5 F * B - N * H L

4- A Z ^ B C * H / - L N / +

Solution :

/ + * A R ^ Z F - B ^ H L

+ -

* ^ B ^

A R Z F H L

The original Exp. (( A* R ) + ( Z ^ F )) / ( B – ( H ^ L ))


Equivalent to : ( A* R + Z ^ F ) / ( B – H ^ L )

Z R / B A * H / + M 2 ^ -

+ ^

/ / M 2

Z R * H

B A

The original Exp. (( Z / R ) + ( B * A / H )) – ( M ^ 2 )


Equivalent to : Z / R + B * A / H – M ^ 2
/ + R 5 F * B - N * H L
Invalid expression ( 7 operands and 5 operators ) ( to become valid add
operator or delete operands in suitable location )

A Z ^ B C * H / - L N / +

- /

^ / L N

A Z * H

B C

The original Exp. ( ( A ^ Z ) – ( B * C / H ) ) + ( L / N )


Equivalent to : A^Z–B*C /H+L/N
Home work Lec . 2 :Write the preorder ,inorder and postorder traversel to the trees below
:

c
b
a) d
f
b)
f
l

Preorder traversal : a , c, d, f
h
Inorder traversal : a, d, f, c
Post order traversal :f, d, c, a z

Preorder traversal : a , f , l , h , z
Inorder traversal : f , h , z , l , b
Post order traversal : z , h , l , f , b

50
a
c) 00

40 60

30 70
0

20 80

10 90
Preorder traversal : 50 , 40 , 30 , 20 , 10 , 60, 70 , 80, 90
Inorder traversal : 10, 20, 30, 40, 50, 60 , 70 , 80, 90
Post order traversal : 10 , 20, 30, 40, 90, 80, 70, 60 , 50

-
2- Using binary tree to obtain sorted data by building binary search tree
(BST):
Binary Search Tree: is a binary tree, in which left child (if any) of any node contains a
smaller value than does the parent node and the right child (if any) contains a larger value
than does the parent node.
20
Example 1: 20
20

Insert 24 Insert 17 17 24
24

insert 14 , 30
20

17
24

Insert 10 , 16
14
Insert 50,60,55,52,58,16 ,50
30

22
10
16 50 ?

16 40 60

55

52 58

50
The inorder traversal of any BST gives data in ascending order while the convert inorder
traversal of any BST gives data in descending order.

Example 2: 14, 7, 9, 20, 5, 12, 8, 11, 25, 35, 24, 21, 11, 3, 4, 13, 2, 38, 40, 1, 36

14

7
20

5 9 25

3 8 12 23 35

2 4
11 13 21 24 38

36 40

The first value 14 at the root


Insert 7 ( less than 14 ) then ( left branch ) reside as left son to the root ( 14 )
Insert 9 ( less than 14 ) then ( left branch ) reside as left son to the root ( 14 )
( 9 > 7 ) set as a right son to the node contain ( 7 )
Insert 20 ( greater than 14 ) then ( right branch ) reside as Right son to the root
( 14 ).
Insert 5 ( less than 14 ) then reside as left son to the root ( 14 ), ( 5< 7 ) set as a left
son to the node contain ( 7 ).
Insert 12 ( less than 14 ) then reside as left son to the root ( 14 ), ( 12 > 7 ) set as a
right son to the node contain ( 7 ), (12 > 9 ) then set as right son to the node contain
( 9) . and so on

Inorder Traversal: 1 2 3 4 5 7 8 9 11 12 13 14 20 21 23 24 25 35 36 38 40

Converse Inorder Traversal:40 38 36 35 25 24 23 21 20 14 13 12 11 9 8 7 5 4 3 2


1

Note that: all the value less than the root inserted in left sub tree and all
values greater than or equal to the root value insert at the right sub tree.

3- Using binary tree to Find duplicated data:


To find duplicated data, another field must be added to the node to compute the
redundancy of each element of the tree.
Example 3: Find the duplicated data if you given the following data:
20, 14 , 31 , 14 , 9 , 7 , 25 , 35 , 10 , 3 , 14 , 7 , 25 , 35 , 1 , 14 , 26 ,38 ,11,36,40 ,20

20

14
31

9 14 35
25

10 25
7 14 35
20

26
3 7
11 14 38

36 40

No. of nodes = ?
20 2

31 1
14 4

9 2 35 2
25 2
2222
38 1
7 2 10 2 26 1

3 1 11 1 36 1

40 1

1 1

No. of nodes = ?

The inorder traversal to a ( BST )


No. Occ. No. Occ.
1 1 35 2
3 1 36 1
7 2 38 1
9 2 40 1
10 2
11 1
14 4
20 2
25 2
26 1
31 2
Note that : A Binary search tree used to search for any given value
The search algorithm same as insertion
Questions :
1- Which method is the best to store duplicate values allocate a node to each value
or use occurrence field ?
2- In which case prefer to use occurrence field in stead of store values ?
3- What about the number of nodes in each methods ?

Example 4:
30
Write all the traversals for the Binary Search Tree below :

20 50

18 25 40 55

Inorder: 18-20-22-25-30-33-37-40-50-55-70 22 33 70
Preorder: 30-20-18-25-22-50-40-33-37-55-70
Postorder: 18-22-25-20-37-33-40-70-55-50-30 37

Converse Inorder: 70-55-50-40-37-33-30-25-22-20-18


Converse Preorder: 30-50-55-70-40-33-37-20-25-22-18
Converse Postorder: 70-55-37-33-40-50-22-25-20-18-30
Level by Level (top-down): 30-20-50-18-25-40-55-22-33-70-37
Level by Level (bottom-up): 37-22-33-70-18-25-40-55-20-50-30
Converse Level by Level (top-down): 30-50-20-55-40-25-18-70-33-22-37
Algorithms

1- Algorithm Create Tree


[This algorithm is to create a tree with single node (Root) contains x
value]
If (root =NULL) {
p←avail ( obtain new node )
avail ← Link (avail)
Info(p)← x ( set info and link fields )
Lptr(p)←NULL
Rptr(p)←NULL
Root←p } ( save the root node )

2- Algorithm Create node


[This algorithm is to create node called (po) contains information called
(x), so its input is value of (x) and return node called (po)]
{ po←avail
avail ← Link (avail)
Info(po)← x
Lptr(po)←NULL
Rptr(po)←NULL }
3- Algorithm Rinsert
[Given a binary search tree pointed by root, this is a recursive algorithm
to insert a new node to the tree contains a new information called (x)]
1- [check left subtree]

If (x < info (root)) then {


If (Lptr(root)= NULL ) //insert a new node as a left subtree
{
Newnode← create node
Lptr(root)←new node
}
Else
Call Rinsert (Lptr(root),x)
}
2- [check right subtree]

If x ≥ info (root) then {


If (Rptr(root)= NULL) //insert a new node as a right subtree
{
Newnode← createnode
Rptr(root)←newnode
}
Else
Call Rinsert (Rptr(root),x) }
4- algorithm Rpreorder
[This algorithm is recursive algorithm for printing the contents of each
node in any binary tree traversed in preorder]
If (root = NULL) // check empty tree
print message “empty tree”
Else {
print (info(root))
If (Lpte(root)≠NULL) // print out left subtree
Rpreorder (Lptr(root))
If (Rptr(root)≠NULL) // print out right subtree
Rpreorder (Rptr(root))
}

5- algorithm Rinorder
[This algorithm is recursive algorithm for printing the contents of each
node in any binary tree traversed in inorder]
If (root = NULL) // check empty tree
print message “empty tree”
Else {
If (Lpte(root)≠NULL) // print out left subtree
Rinorder (Lptr(root))
print (info(root))
If (Rptr(root)≠NULL) // print out right subtree
Rinorder (Rptr(root))
}
6- algorithm Rpostorder
[This algorithm is recursive algorithm for printing the contents of each
node in any binary tree traversed in postorder]
If (root = NULL) // check empty tree
print message “empty tree”
Else {
If (Lpte(root)≠NULL) // print out left subtree
Rpostorder (Lptr(root))
If (Rptr(root)≠NULL) // print out right subtree
Rpostorder (Rptr(root))
print (info(root))
}
5- Algorithm delete [there are 3 cases in deletion]
a- if the deleted node has no sons (it is a leaf)
- if deleted node denoted by pointer variable (T) then it’s parent denoted by (P). If
(T) is left child:-

A A

D B D B

P H
H

T L L
M

X X

Lptr(P)←NULL
Link(T) ← avail
avail ← T
- if deleted node denoted by pointer variable (T) then it’s parent denoted by (P). If
(T) is right child:-

A A

D B D B

H H

P M L
M L

T
X
Rptr (P)←NULL
Link(T) ← avail , avail ← T
if the deleted node have left or right subtree and denoted by (T) then It’s son can
be moved to take it’s place.
- If (T) has left subtree and (P) is T’s parent:-

P
A
A
T
Z D
B D

X F H
F H
Z ←

W Y L M
L M
X
K
K
W Y K

Lptr(P)←Lptr(T)
Link(T) ← avail
avail ← T
- If (T) has right subtree and (P) is T’s parent:-

P A A

B D B D

Z G K X G K
T

L M W Y L M
X

W Y
Rptr (P)←Lptr(T)
Link(T) ← avail
avail ← T
c- If the deleted node has two subtrees
If deleted node has left subtree and right subtree then it’s successor in inorder
traversal must take it’s place. Let (T) means deleted node then (PT) will be parent
PT successor of (T) in inorder traversal then (PS) is parent of
of (T) and let (S) means
(S).
A
A
T

B Q
B D

Z F H
Z F H

L M
L M PS X
X K

K I K
I W Y
W Y K
S

E
Q E

- If (S) is a leaf node.


Lptr(PS)←NULL
Lptr(S)←Lptr(T)
Rptr(S)←Rptr(T)
Rptr(PT)←S
PT

A
A
T
B I
B D

Z F H
Z F H

L M
L M X
X K
K

PS W Y E K
W Y I K K K
K
S O G
E
K

O G

- If (S) is a node that has one child or one subtree.


Lptr(PS)←Rptr(S)
Lptr(S)← Lptr(T)
Rptr(S)← Rptr(T)
Rptr(PT)←S
T
PS G
A

B D
B D

Z K
Z G K

L M
X
L M K
X
K
W Y
W Y S
- If (T) is a node that has two children or two subtrees (right and left) and (T is a root
node).
Lptr(PS)←NULL
Lptr(S)←Lptr(T)
Rptr(S)←Rptr(T)
Root←S

 Advantages of Tree:
Search operation of any node in a Binary Search Tree takes half time of search in
linear data structures like queue because it searches either in right subtree or in left
subtree.
 Disadvantages of Tree:
1- Its programming is difficult operation in the languages that not support the
pointers.
2- It takes more space to store the pointers.
3- There is a difficulty to reach to the parents.

The Sequential Representation of Binary Tree


The complete binary Search Tree can be numbered from 1 to n so that the number
assigned to the left son is twice the number assigned to it’s parent and the number
assigned to the right son is one more than twice the number assigned to it’s parent. (i.e)
the node in position (p) is the parent of the nodes in position (2p) and (2p
+1). If the left child at position (p) then it’s right brother in position (p+1) and if the right
child at position (p) then it’s left brother in position (p-1). If any node in position (k) then
it’s parent in position trunc (k/2). Then we will store the tree in the array level by level
from left to right.
Example:

B D

E H L

F K M

A B D H L F K M
E

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

 In sequential allocation, the returning to the root is easy. While in linked allocation,
it is hard to return to the root, also there is increasing in reserving space since there
is a need to reserve the left and right pointers.

Sorting of Tree
Two sorting methods are based on a tree representation of data:
1- The straight forward Binary Search Tree (BST).
2- The Heap Sort which is involving binary tree in much more complex structure
called “Heap”.

Heap Structure: is a complete tree with some of right most leaves removed. There are
two types of heap sort: 1- Max heap and Min heap. The Max heap represents a table of
records that satisfies the following properties:-
j/2
1- ( max heap) kj ≤ ki for 2≤j≤n and i= └ ┘

2- The record with largest key is at the root of the tree called the top of the heap.
3- Any path from the root to a leaf is sorted list in descending order.
While Min heap represents a table of records that satisfies the opposite properties of
max heap.

500

300 200

100 130 120 160

Heap Sort algorithm:


1- Build heap structure: arrange the data using tree then put the data of resulted
tree in an array.
2- Reheaping: is the process of printing the data of the heap by deleting root until
the tree became empty tree.
Note that:
 The heap structure must be represented in a sequential allocation.
 If we build a Min heap then the data will be sorted in ascending order.
Example:
Use the following data to construct the heap structure then sort the data in descending
order: 42, 23, 74, 11, 58, 62, 94, 99, 87, 36.

1- Build heap structure. 74


74

42 42 74 23 42
42
42

23 23 74 23 42
11

insert “42” insert “23” insert “74” insert “11”


74 74 74
58 62

23 42 58 42 58 42

11 58 23 11 23 11 23 62 42

insert “58” insert “62”

94

74 74 94
74
94
62 58 62 74
58 58

62
11 23 42 11 23 42 94 23 62
11 42

insert “94”

99
94 94 99
99

58 74 94 74
58
99
11 23 42 62 58 23 42 62

11

99 11

insert “99”
99
99

94 74
94 74
87

58 23 42 62
87 23 42 62

11 87
58 11 58

insert “87”

99
99

94 74
94 74
36

87 36 42 62
87 23 42 62

11 58 23
11 58 36
23

insert “36”

Q) How a heap represent in memory ? Why ?

Answer : The heap represent in memory as array (Static) because the relation
between a parent and its child are twice and half.(easy to reach to any location directly)

99 94 74 87 36 42 62 11 58 23
2- Reheaping
94
23
87 23
99 94
87 58
23 23
94 74 87 74
58 23
23 87 36 42 62 36 42 62
58

11 58 23 11 23
23

delete “99” delete “94”

74 11
87 62
62 11
74
11
74 42
58
58 62 11

23 36 42 62
11
23 36 42 11

11
11

delete “87” delete “74”


42
58 11
11
62
58
36
11 58 42
42 11
36

23 36 11
23 11
11

delete ”62” delete “58”

36
23
42
23 11
11 ←
36 23

36 11

23 23 11
11

23 11

delete “42” delete “36” delete “23”

delete “11”

11

You might also like