Download - Computer Notes - Data Structures - 11
-
8/3/2019 Computer Notes - Data Structures - 11
1/27
Class No.11
Data Structures
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
2/27
Code for Simulation
// print the final avaerage wait time.
double avgWait = (totalTime*1.0)/count;
cout
-
8/3/2019 Computer Notes - Data Structures - 11
3/27
Priority Queue
#include "Event.cpp"
#define PQMAX 30
class PriorityQueue {
public:
PriorityQueue() {size = 0; rear = -1;
};
~PriorityQueue() {};
int full(void)
{return ( size == PQMAX ) ? 1 : 0;
};
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
4/27
Priority Queue
Event* remove()
{
if( size > 0 ) {
Event* e = nodes[0];
for(int j=0; j < size-2; j++ )
nodes[j] = nodes[j+1];
size = size-1; rear=rear-1;
if( size == 0 ) rear = -1;
return e;
}return (Event*)NULL;
cout
-
8/3/2019 Computer Notes - Data Structures - 11
5/27
Priority Queue
int insert(Event* e)
{
if( !full() ) {
rear = rear+1;
nodes[rear] = e;
size = size + 1;sortElements(); // in ascending order
return 1;
}
cout
-
8/3/2019 Computer Notes - Data Structures - 11
6/27
Tree Data Structures
There are a number of applications wherelinear data structures are not appropriate.
Consider a genealogy tree of a family.
Mohammad Aslam Khan
Sohail Aslam Javed Aslam Yasmeen Aslam
SaadHaaris Qasim Asim Fahd Ahmad Sara Omer
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
7/27
Tree Data Structure
A linear linked list will not be able tocapture the tree-like relationship withease.
Shortly, we will see that for applicationsthat require searching, linear datastructures are not suitable.
We will focus our attention on binary trees.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
8/27
Binary Tree
A binary treeis a finite set of elements that iseither empty or is partitioned into threedisjointsubsets.
The first subset contains a single element calledthe rootof the tree.
The other two subsets are themselves binarytrees called the leftand right subtrees.
Each element of a binary tree is called a nodeofthe tree.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
9/27
Binary Tree
Binary tree with 9 nodes.
A
B
D
H
C
E F
G I
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
10/27
Binary Tree
A
B
D
H
C
E F
G I
Left subtree
root
Right subtree
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
11/27
Binary Tree
Recursive definitionA
B
D
H
C
E F
G ILeft subtree
root
Right subtree
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
12/27
Binary Tree
Recursive definitionA
B
D
H
C
E F
G I
Left subtree
root
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
13/27
Binary Tree
Recursive definitionA
B
D
H
C
E F
G I
root
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
14/27
Binary Tree
Recursive definitionA
B
D
H
C
E F
G I
root
Right subtree
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
15/27
Binary Tree
Recursive definitionA
B
D
H
C
E F
G I
root
Right subtreeLeft subtree
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
16/27
Not a Tree
Structures that are not trees.
A
B
D
H
C
E F
G I
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
17/27
Not a Tree
Structures that are not trees.
A
B
D
H
C
E F
G I
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
18/27
Not a Tree
Structures that are not trees.
A
B
D
H
C
E F
G I
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
19/27
Binary Tree: Terminology
A
B
D
H
C
E F
G I
parent
Left descendant Right descendant
Leaf nodes Leaf nodes
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
20/27
Binary Tree
If every non-leaf node in a binary tree has non-empty left and right subtrees, the tree is termeda strictly binary tree.
A
B
D
H
C
E F
G I
J
K
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
21/27
Level of a Binary Tree Node
The levelof a node in a binary tree isdefined as follows:
Root has level 0,
Level of any other node is one more than thelevel its parent (father).
The depthof a binary tree is the maximum
level of any leaf in the tree.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
22/27
Level of a Binary Tree Node
A
B
D
H
C
E F
G I
1
0
1
2 2 2
3 3 3
Level 0
Level 1
Level 2
Level 3
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
23/27
Complete Binary Tree
A complete binary treeof depth dis the strictlybinary all of whose leaves are at level d.
A
B
N
C
G
O
1
0
1
2
3 3L
F
M
2
3 3H
D
I
2
3 J
E
K
2
3
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
24/27
Complete Binary Tree
A
B
Level 0: 20 nodes
H
D
I
E
J K
C
L
F
M
G
N O
Level 1: 2
1
nodes
Level 2: 22 nodes
Level 3: 23 nodes
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
25/27
Complete Binary Tree
At level k, there are 2knodes.
Total number of nodes in the tree of depthd:
20+21+22+ . + 2d= 2j=2d+1 1
In a complete binary tree, there are 2d leafnodes and (2d- 1) non-leaf (inner) nodes.
j=0
d
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
26/27
Complete Binary Tree
If the tree is built out of n nodes then
n =2d+1 1or log2(n+1) = d+1or d = log2(n+1) 1
I.e., the depth of the complete binary tree builtusing n nodes will be log2(n+1) 1.
For example, for n=100,000, log2(100001) isless than 20; the tree would be 20 levels deep.
The significance of this shallowness will becomeevident later.
http://ecomputernotes.com
http://ecomputernotes.com/http://ecomputernotes.com/ -
8/3/2019 Computer Notes - Data Structures - 11
27/27
Operations on Binary Tree
There are a number of operations that canbe defined for a binary tree.
If pis pointing to a node in an existing treethen
left(p) returns pointer to the left subtree
right(p) returns pointer to right subtree
parent(p) returns the father of p
brother(p) returns brother of p.
info(p) returns content of the node.
http://ecomputernotes com
http://ecomputernotes.com/http://ecomputernotes.com/