Professional Documents
Culture Documents
Array-Based Implementation:
An array can be used to store some binary trees. Number
the nodes level by level, from left to right,
0
O
1 2
M T
3 4 5 6
C E • P U
•
•
Store node # 0 in array location 0, node #1 in array location 1, etc.
i 0 1 2 3 4 5 6 . ..
t [i ] O M T C E P U . ..
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
E
But, unless each level of the tree is full
so there are no "dangling limbs," there C M
63 array
before but requires ___
P
positions for storage:
Max # nodes on level i: O
2i i 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
In array representation, children of i t[ i] E C M U T
are at:
2i + 1, 2i + 2 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
P
Parent of i is at:
(i - 1) / 2 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 …
i is the index of the a Parent node O …
left right
root
Left child Right child
and maintain a pointer to the root. 75
75
60 80
60 80
58 65 92 58 65 92
Preorder:
root – left – right
Postorder:
left – right – root
Inorder:
left – root – right
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Implementing Tree Traversals
void Preorder(Node * ptr)
{
if(ptr!=NULL)
{
cout << ptr->data << “ “;
Preorder(ptr->left);
Preorder(ptr->right);
}
}
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Implementing Tree Traversals
void Postorder(Node * ptr)
{
if(ptr!=NULL)
{
Postorder(ptr->left);
Postorder(ptr->right);
cout << ptr->data << “ “;
}
}
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Implementing Tree Traversals
void Inorder(Node* ptr)
{
if(ptr!=NULL)
{
Inorder(ptr->left);
cout << ptr->data << “ “;
Inorder(ptr->right);
}
}
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Traversal exercises: finding output
14
15 void Traversal(Node* temp)
4 {
3 18 if(temp!=NULL)
9 14 {
20 cout<<temp->data;
7 9 16 Traversal(temp->left);
5 Traversal(temp->right);
17 cout<<temp->data;
4 5 }
}
14 4 3 3 9 7 5 4 4 5 5 5 7 9 9 9 4 15 14 14 18 16 17 17 16 20 20 18 15 14
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Traversal exercises: finding output
void Traversal(Node* temp, int count) {
if(temp!=NULL) {
14
if (count%2 == 0)
15
4 cout<<temp->data;
3 18
9 14 count++;
20 Traversal(temp->left, count);
7 9 16
Traversal(temp->right, count);
5
17 }
4 5 }
A Binary Search Tree (BST) is a binary tree in which the value in each
node is greater than all values in its left sub-tree and less than all values
in its right sub-tree.
75
60 80
58 65 92
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
BST As ADT
Collection of Data Elements:
A binary tree in which for each node x:
value in left child of x < value in x < value in right child of x
Basic Operations:
1. Construct an empty BST
2. Determine if the BST is empty
3. Search the BST for a given item
4. Insert an item in the BST maintaining the BST property
5. Delete an item from the BST maintaining the BST property
6. Traverse the BST
15
4 15
4 15
4 15
4 15
9 18
4 15
3 9 18
4 15
3 9 18
4 15
3 9 18
16
7
4 15
3 9 18
16
7
4
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Example Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
4 15
3 9 18
16 20
7
4
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Example Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
4 15
3 9 18
16 20
7
17
5
4
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Example Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
4 15
3 9 18
16 20
7 9
17
5
4
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Example Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
4 15
3 9 14 18
16 20
7 9
17
5
4
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Example Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
4 15
3 9 14 18
16 20
7 9
17
5
4 5
“Binary Search Tree” of the given data set
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Implementing a BST
class Node
{
public:
int data;
Node *left, *right;
};
Node(int item)
{
data = item;
left = right = NULL;
}
The above process will continue till the item is found or you
reached end of the tree.
This has to be done such that the property of the search tree is
maintained.
Deletion
To delete a node x from a BST, we have three cases:
1. x is leaf
2. x has one child
3. x has two children
75
60 80
x 58 65 92
Free x 75
60 80
x 58 65 92
60 80
58 65 x 92
62
60 80
58 65 x 92
62
5 x
2 21
-4 3
19 25
2 21
-4 3
19 25
21
19 25
21
2
-4 3 19 x 25
The following approach can be utilized to remove a node, which has two children:
1. find a minimum value in the right subtree;
2. replace value of the node to be removed with found minimum. Now, right subtree contains a
duplicate!
3. apply remove to the right subtree to remove a duplicate.
F J x
A
H O
I M P
xSucc K N
L
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Deletion in BST
G
F K x
A
H O
I M P
xSucc K N
L
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Deletion in BST
G
F K x
A
H O
I M P
xSucc K N
L
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Deletion in BST
G
F K
A
H O
I M P
L N
75 75
parent 60 80 parent 60 80
58 65 temp 92 temp 58 65 92
62 52
}
BS (IT) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
SCENARIO: if(temp->right != NULL)
75 75
parent 60 80 parent 60 80
58 65 temp 92 temp 58 65 92
66 59
+ c +
a b d e
+ c + + *
a b d e a b c +
d e
Finally, the last symbol is read, two trees are merged, and a pointer
to the final tree is left on the stack.
+ * *
a b c + + *
d e a b c +
d e