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

lec8data

Uploaded by

ejr1590
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)
6 views

lec8data

Uploaded by

ejr1590
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/ 16

Data Structures – Eighth Lecture

2- Using binary tree to obtain sorted data by building binary search tree
(BST):
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: given the following BST: 14
1

7 20

5 9 25

3 8 12 23 35

2 4 11 13 21 24 38

1
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
Converse Inorder Traversal: 40 38 36 35 25 24 23 21 20 14 13 12 11 9 8 7 5 4 3 2 1

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.
Data Structures 2024-2025

Example:
Find the duplicated data if you given the following data:
20,14,31,14,9,7,25,35,10,3,14,7,25,35,10,3,14,7,22,31,9,14

20 1

14 5 31 2

9 2 25 2 35 2

7 3 10 2 22 1

3 2

Algorithms
1- Algorithm CreateTtree
[This algorithm is to create a tree with single node (Root) contains x value]
If (root =NULL)
{
p←avail
avail ← Link (avail)
Info(p)← x
Lptr(p)←NULL
Rptr(p)←NULL
Root←p
}

Prepared by Dr. Dunia H. Hameed Page 61


Data Structures 2024-2025

2- Algorithm Createnode
[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← createnode
Lptr(root)←newnode
}
Else
Call Rinsert (Lptr(root),x)
}

Prepared by Dr. Dunia H. Hameed Page 62


Data Structures 2024-2025

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)
}

Example
20
20 20 20
1
17 24
24
17 24

14

Root=20 insert (24) insert (17) insert (14)

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

Prepared by Dr. Dunia H. Hameed Page 63


Data Structures 2024-2025

Rpreorder (Lptr(root))
if (Rptr(root)≠NULL) // print out right subtree
Rpreorder (Rptr(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 M L L

X X

Lptr(P)←NULL
Link(T) ← avail
avail ← T

Prepared by Dr. Dunia H. Hameed Page 64


Data Structures 2024-2025

- 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

L P M L
M

T
Rptr (P)←NULL X

Link(T) ← avail
avail ← T

b- If the deleted node have only one subtree


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

F H
F H X
Z ←
W Y L M
X L M

K
W Y K

Prepared by Dr. Dunia H. Hameed Page 65


Data Structures 2024-2025

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
M
X L M W Y L K
K

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 of (T) and let
(S) means successor of (T) in inorder traversal then (PS) is parent of (S).

Prepared by Dr. Dunia H. Hameed Page 66


Data Structures 2024-2025

PT

A A
T
B Q
B D

Z F H
Z F H

L L M
M PS X
X

I K W Y I K
W Y K
S
Q E
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 X L M
X K
K

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

Prepared by Dr. Dunia H. Hameed Page 67


Data Structures 2024-2025

- 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
G
PS
A
B D
B D
Z K
K
Z G

X L M
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.

Prepared by Dr. Dunia H. Hameed Page 68


Data Structures 2024-2025

 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

Prepared by Dr. Dunia H. Hameed Page 69


Data Structures 2024-2025

 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

Prepared by Dr. Dunia H. Hameed Page 70


Data Structures 2024-2025

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 74
42 23 42
42
42
23 23 74 23 42
11
insert “42” insert “23” insert “74” insert “11”

58 74 74 74
62
23 42 42
58 58 42

11 58 23 23 23 62
11 11 42

insert “58” insert “62”

Prepared by Dr. Dunia H. Hameed Page 71


Data Structures 2024-2025

94
74 74 74 94

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

insert “94”

99
94 94 99
99
74 74
58 94
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 42
87 23 62

11 87
58 11 58

insert “87”

Prepared by Dr. Dunia H. Hameed Page 72


Data Structures 2024-2025

99 99

74 94 74
94
36
87 36 42 62
87 23 42 62

11 11 58 23
58 36
23

insert “36”
2- Reheaping 94
23
87 23
99 94
87 58
23 23
94 74 87 74
58 23
23 87 36 42 62 36 62
58 42

11 58 23 1 23
23 1

delete “99” delete “94”

74 11
87 62
62 11 74
11 42
58 7
4 58 62 11

23 36 4 6 11
2 2 23 3 4 11
6 2
11 11

delete “87” delete “74”

Prepared by Dr. Dunia H. Hameed Page 73


Data Structures 2024-2025

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 23 11
36 ←
36 11
23 23 11 11
← 11
23

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

11

delete “11”

Prepared by Dr. Dunia H. Hameed Page 74


Data Structures 2024-2025

Differences Between Binary Search Tree and Single Linked List


1- The Binary Search Tree does not permit the random access. It provides quicker and
more constant access to individual nodes than does a linked list.
2- The Binary Search Tree with it’s extra pointer in each node will take up more
memory space than a single linked list.
3- The algorithms for manipulating the tree are more complicated. For example, the
inorder traversal of a binary tree is much more expensive than the sequential
traversal of the elements of single linked list.

Prepared by Dr. Dunia H. Hameed Page 75

You might also like