lec8data
lec8data
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
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
}
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)
}
Example
20
20 20 20
1
17 24
24
17 24
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
Rpreorder (Lptr(root))
if (Rptr(root)≠NULL) // print out right subtree
Rpreorder (Rptr(root))
}
A A
D B D B
P H H
T M L L
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
L P M L
M
T
Rptr (P)←NULL X
Link(T) ← avail
avail ← T
T Z D
B D
F H
F H X
Z ←
W Y L M
X L M
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
M
X L M W Y L K
K
W Y
Rptr (P)←Lptr(T)
Link(T) ← avail
avail ← T
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
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.
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.
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
58 74 74 74
62
23 42 42
58 58 42
11 58 23 23 23 62
11 11 42
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”
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
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
42
58 11
11
62 58
36
11 58 42 42 11
36
23 36 11
23 11
11
36 23 42
23 11 23 11
36 ←
36 11
23 23 11 11
← 11
23
←
11
delete “11”