All Lectures Data Structure 2020 Makia Hamad Compressed السنة الجاية
All Lectures Data Structure 2020 Makia Hamad Compressed السنة الجاية
1. Tree
2. Graph
3. Network
1. Tree
Root node A
Subtree
B C D
E F G H I
J K
Leaves nodes Leaves 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
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:
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: 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 )
Operator
Operand Operand
1 2
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.
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
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
+ 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
d n x
a
3)
b
e
4)
a
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.
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
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.
2- ( A * B + K ) / 4 + ( N * L *G )
4- A B / E H C / - * N R ^ +
5- - + + A* B C / D E
6- ( A / B + K ) ^ 4 ^ ( N - L *G )
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.
2- ( A * B + K ) / 4 + ( N * L *G )
4- A B / E H C / - * N R ^ +
5- - + + A* B C / D E
6- ( A / B + K ) ^ 4 ^ ( N - L *G )
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
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
↑
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
d n x
B)
N=5
a
N
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
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 ? )
Sub trees
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
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.
2- ( A * B + K ) / 4 + ( N * L *G )
4- A B / E H C / - * N R ^ +
5- - + + A* B C / D E
6- ( A / B + K ) ^ 4 ^ ( N - L *G )
من الممكن رسم األشجار بدون دوائر للسرعه وخصوصا باالمتحانات وكما مبين ادناه: مالحظه
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
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
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
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
+ ^
/ K 4 -
A B N *
G L
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
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
Z R / B A * H / + M 2 ^ -
+ ^
/ / M 2
Z R * H
B A
A Z ^ B C * H / - L N / +
- /
^ / L N
A Z * H
B C
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
Inorder Traversal: 1 2 3 4 5 7 8 9 11 12 13 14 20 21 23 24 25 35 36 38 40
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.
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 = ?
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
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
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
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.
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
42 42 74 23 42
42
42
23 23 74 23 42
11
23 42 58 42 58 42
11 58 23 11 23 11 23 62 42
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”
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
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
23 36 11
23 11
11
36
23
42
23 11
11 ←
36 23
36 11
23 23 11
11
←
23 11
←
delete “11”
11