Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

data structures notes

Uploaded by

dhana lalitha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

data structures notes

Uploaded by

dhana lalitha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

NTRODUCTION TO DATA STRUCTURE

A data structure is a way of storing the data in computer’s memory


so that it can be used efficiently. Formally, a data structure is a
logical or mathematical model of organization of data. The choice of
data structure begins with the choice of an Abstract Data Type
(ADT) such as array.

CLASSIFICATION OF DATA STRUCTURES


It can be classified into three different categories:
1. Linear and Non-linear data structures
2. Static and Dynamic Data structures
3. Homogeneous and Non-homogeneous data structuresNow we will
explain above categories one by one.1. Linear and Non-linear data
structures

Linear data structure are those in which elements in a linear manner


or in a linear sequence.
Example: Array, Linked List, Queue , stack etc.

Non-linear data structures are those in which elements do not have


any sequence.
Example: Trees , Graph etc.

2. Static and Dynamic Data structures


Static Data structure are those whose memory occupation is fixed.
The memory taken by these Data structures cannot be increased or
decreased at run time.
Example: Array.
The size of an array is declared at compile time and this size cannot
be changed during the run time.

Dynamic Data structures are those whose memory occupation is


not fixed. The memory taken by these Data structures can be
increased or decreased at run time.
Example: Link list.
The size of an Link list is declared at compile time and this size
cannot be changed during the run time.

Note: Data structures like stack , queue , tree and graph can be
static or dynamic depending on, whether these are implemented
using an array or a link list.
3. Homogeneous and Non-homogeneous data structures
Homogeneous data structure are those in which data of same type
can be stored.
Example: Array
Non-Homogeneous data structure are those in which data of
different type can be stored.
Example: Link list.
TREE
A tree t can be a defined as finite(means limit or not infinite) set of
nodes in which there is one specially designated node known as root
of the tree.
and the remaining nodes are partitioned into disjoint sets t1, t2….tn
such that each t1 is itself a tree and is known as sub-tree of its root.

A general tree
A line which is connects a parent node to its child node is called
an edge or branch.
All the nodes in a tree except the root node, has a parent node
associated with it.
A node in the tree can have zero or more children called
its successors.
A node having no child is called a leaf node or terminal node.
sometimes, it is also known as external nodes and non-terminal
nodes are called internal nodes.
Nodes having same parent node are called siblings or brothers.

 In the above figure, A is the root having three children B, C and


D.
 B,C and D are siblings as these have the same parent node A.
 similarly,K and L are also known as siblings having same parent
E.
 K,L,M and N are the leaf nodes having no child.

A path between any two nodes in a tree is a sequence of distinct


nodes in which successive nodes are connected by the edges.There
must be exactly one path between the root and any other node in
the tree. If there is more than one path between any two nodes then
that is not a tree but a graph.

The length of a path zero from every node to itself. In the above
figure, A->N->E->K which is length 3.
The height of any node in a tree is the length of the longest path
from that node to a terminal node i.e node B is at height of 2 and
node E is at height of 1.
The degree of node can be defined as number of child nodes it has.
Node A has degree 3 and G has degree 1.
The weight of the tree is the number of external nodes available in
that tree. In the above figure, weight is 7 i.e K,L,F,M,H,N and J are
external nodes.
Each node is assigned a level number which comes from the length
of the path from the root to that node. Root of a tree is said to be at
level 0.
The level also known as depth of the tree, of any node is the
number of edges that are to travelled from the root node to that
particular node.
What is binary numbers?
Binary means two numbers i.e 0 or 1.
What is binary addition and subtraction?
Addition and subtraction are arithmetic operators.
Binary addition means adding between two binary numbers.
Binary subtraction means subtracting between two binary numbers.
Overflow rule:
In general term, overflow means , a thing more than capacity and it
is wastage.
In computer organization,Overflow occurs when the resulting value
of an operation performed on valid representations of numbers is
out of the range of valid values. That is, the resulting value is
greater than max or less than min.
How overflow occurs depends:

 the operation being performed,


 the representation system used to represent the numbers,
 and the number of bits used

lets take an example,


If you are representing numbers using 4 bits as unsigned binary, the
minimum value is 0 (i.e., 00002, while the maximum value is 15
(11112).
Any operation that results in a number larger than 15 or smaller
than 0 has overflowed.
Suppose i want to add 15 + 15 (1111 + 1111) , we can represent 15
in 4 bits but after addition the result will be 30. That, is not possible
in 4 bits. so this is overflow.
Remember: −2n−1 ≤ Two’s Complement ≤ 2n−1 − 1
−8 ≤ x[4] ≤ +7
−128 ≤ x[8] ≤ +127
−32768 ≤ x[16] ≤ +32767
−2147483648 ≤ x[32] ≤ +2147483647
Two’s complement addition:
Add the values and discard any carry-out bit.
Examples: using 8-bit two’s complement numbers.

1. Add −8 to +3

(+3) 0000 0011

+(−8) 1111 1000

-----------------

(−5) 1111 1011 no overflow

Add −5 to −2

(−2) 1111 1110

+(−5) 1111 1011

-----------------

(−7) 1 1111 1001 overflow occur


Now, the shortcut method to know that overflow will occur or not?

firstly, see some example:

• 1011 + 10010 =

11101: • 11001011 + 1011010 =


• 1010110 + 110100 = 10001010:
1 1 1 1
1 1 1 1
1

1 1 0 0 1
0 1 0 1 0 1 1 0
0 0 0 0 1 0 1 1

+ 0 1 0 1 1
+ 0 0 1 1 0 1 0 0
+ 0 0 0 1 0 0 1 0

0 0 1 0 0
1 0 0 0 1 0 1 0
0 0 0 1 1 1 0 1

Overflow. The carry-out i


No overflow. Sum is correct.
No overflow. Sum is the sum is not correct
correct.

• 1010001 + 11001 = • 110001 + 11100100 = 100010101: • 10011011 + 1001010 =

1101010:
1 1 1 1 1

1 1

0 0 1 1 0 0 0 1 1 0 0 1 1
0 1 0 1 0 0 0 1

+ 1 1 1 0 0 1 0 0
+ 0 1 0 0 1
+ 0 0 0 1 1 0 0 1

0 0 0 1 0 1 0 1
1 1 1 0 0
0 1 1 0 1 0 1 0
Overflow. The carry-out is discarded, and
No overflow. Sum is corre
No overflow. Sum is the sum is not correct

correct.

GOLDEN RULE OF ADDITION


If 2 Two’s Complement numbers are added with each other, and
they both have the same sign (both positive or both negative), then
overflow occurs if and only if the result has the opposite sign.
Overflow never occurs when adding operands with different signs.
Overflow occurs if

 (+A) + (+B) = −C
 (−A) + (−B) = +C

Example: Using 4-bit Two’s Complement numbers (−8 ≤ x ≤ +7)

(−7) 1001

+(−6) 1010

------------

(−13) 1 0011 = 3 : Overflow occur (largest −ve number is −8)

BINARY SUBTRACTION

Subtrahend: what is being subtracted


Minuhend: what it is being subtracted from

Example: 400 - 200 = 200

200 is the subtrahend, 400 is the minuhend, 200 is the result


If 2 Two’s Complement numbers are subtracted, and their signs are
different, then overflow occurs if and only if the result has the same
sign as the subtrahend.
Overflow occurs if

 (+A) − (−B) = −C
 (−A) − (+B) = +C

Example: Using 4-bit Two’s Complement numbers (−8 ≤ x ≤ +7)


Subtract −6 from +7

(+7) 0111 0111

−(−6) 1010 -> Negate -> +0110

---------- -----

13 1101 = −8 + 5 = −3 : Overflow

Numbers can be added or subtracted by moving round the number


circle

 Clockwise for addition


 Anti-Clockwise for subtraction (addition of a negative number)

WHEEL OF ADDITION AND SUBTRACTION OF 4 BITS NUMBER:


fig a wheel of addition and subtraction
In the above fig a, you can see when we will add two numbers 1 + 2
then it will go to the right side and no overflow occurs.
if the numbers are large or you can say that out of range then
overflow will occur i.e 7 + 7 =14
In this case, range is out of bound it will go to the negative value .
NUMBER IN BINARY
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
10 1010
11 1011
12 1100
13 1101
14 1110
15 1111
In the above table, after 7(0111) then next value is the reverse of it
i.e (1000)
6(0110) is the reverse of 9(1001).
this is the logic which is not mentioned in any book.
BINARY TREE
An important category of general tree is Binary Tree. A binary tree
can be defined as a finite collection of nodes where each node is
having the property that it can have 0,1 or 2 children.
A binary tree may be empty known as Null tree or it contains a
special node called the root of the tree and remaining nodes in the
tree form the left and right binary sub-trees.

fig. (a) A
binary tree

Here, the node A is the root of the binary tree which is the top of
the tree.

B and C are the left and right sub-trees of the root A, respectively.

The left sub-tree namely B, having single node D.

The right sub-tree namely C having two nodes E and F.

Similar Binary tree

Two trees are called similar if both are having same structure but
the elements in both the tree can be different as shown in fig. b
fig. b similar binary tree

Equivalent Binary tree


Two trees are called equivalent if both are having same structure
and having the same content in their respective nodes.

Complete binary Tree

A binary tree is called complete if it contains the maximum number


of nodes it can have as shown in fig. c

fig. c complete binary tree

On the other hand, a binary tree is said to be almost complete


binary tree if all its levels are full except the last one as shown in
fig. d

fig. d almost complete binary tree


In fig. c , at level k, there can be maximum 2^k nodes. therefore,
level 0 can have 2^0=1 node.
level 1 can have 2^1=2 nodes.
level 2 can have 2^2=4 nodes and so on.
Strictly Binary tree
A binary is called Strictly binary tree if all the non leaf nodes of the
tree contains exactly two children.
It means , every non-leaf nodes contains left and right sub-tree.
It is also known as 2- tree or full Binary tree or extended binary
tree.
It contains exactly 2n-1 nodes.

fig.e strictly binary tree

BINARY TREE PROPERTIES:

1. A Binary tree with n nodes has exactly n-1 edges.

fig.1 binary tree


In the above fig. 1, number of nodes(n) are 15
number of edges= n-1=15-1=14
2. Every node except the root node has exactly one parent.
In the above fig. 1, A is the root node having no parent but rest
nodes having only one parent i.e 3 has parent 1, 5 has parent 2.
3.There is exactly one path connecting any two nodes in the tree.
In the above fig. 1, you can see there is only one path between 0 to
1 or 1 to 3.
4. The maximum number of nodes at level ‘l’ of a binary tree is 2l-1
In the above fig. 1, level 1 has only one node 2^(1-1)=2^0=1
level 2 has 2 nodes 2^(2-1)=2^1=2 and so on.
5. Maximum number of nodes in a binary tree of height ‘h’ is 2h – 1.
In the above fig. 1, height of binary tree is 4(starting from 1 ,2,3,4)
Apply formula, Total number of nodes in a binary tree is 2^(4)-1=15
6. The number of leaf nodes in a complete binary tree is (n+1)/2
In the above fig. 1, number of nodes is 15
number of leaf nodes= (15+1)/2= 8 i.e node 7 to 14.
7. In a complete binary tree, number of external nodes= number of
internal + 1
In the above fig. 1, number of internal nodes are 7 i.e 0 to 6
number of external nodes are 8 i.e 7 to 14
Concept of Data Structure

A data type can be defined as set of values and set of operations


which are permissible on those values.
For example, an integer data type in C language can have range ( -
32768 to 32767) and set of operations +, – , * , , % etc.

Data type can be classified into two categories:

1. Primitive data type are also known as predefined or basic data


types. These data types may be different for different languages.
For example, in C language, the primitive data types for storing the
integer values are int , long , short and for storing the real values
are float , double and long double.

2. Abstract data types


It is necessary to understand the concept of Abstract and data type
separately. The term abstract stands for considering apart from the
detailed specification.
Data type as mentioned earlier is the set of values and set of
operations that are permissible on those values.

Abstract data types uses the following principles:


Encapsulation: Providing the data and operations on the data in as
single unit.

Abstraction: Hiding the detail of implementation – a class in c++ or i


java exhibits what it does through its methods but the detail is
hidden from the user.

Data Structure Vs File organization

The file organization is the study of storing the data records into the
files. There are various file organization techniques like sequential
organization , random file organization and indexed file
organization. These file organization techniques differ in record
sequencing and retrieval methods used.

A data structure and a file organization are same but differ in the
following aspects:

 In implementation
 In access Methods
 Data structure are thought of as in main memory like RAM and
file organization as in auxiliary storage like disks.

INTERRUPT

An interrupt is a control signal sent to the microprocessor to draw


its attention.
It is a type of signal to processor in which processor,on receiving
the interrupt request,stops its current operation and starts
executing the subroutine associated with the interrupt signal.

Interrupt signal is – active low (0) or active high (1) signal used to
send to a device to say “stop doing what you’re doing and listen to
me. I’ve got something for you”.

In general term, interrupt is a kind of BUZZER to stop the continous


progess orbreak the continuity.

THERE ARE THREE TYPES OF INTERRUPTS AS FOLLOWS:


1) Internal Interrupt
2) Software Interrupt
3) External Interrupt

1. INTERNAL INTERRUPT

As clear to its name, internal interrupts are those which are


occurred due to Some Problem in the Execution.It seems to be used
mostly to refer to what are also known as exceptions: interrupts
that occur in response to a processing error, such as referencing an
invalid address in memory, division by zero, or similar error
condition.

2. SOFTWARE INTERRUPT

A software interrupt is a type of interrupt generated by executing


an instruction is called software interrupt. A software interrupt is
invoked by software, unlike a hardware interrupt, and is considered
one of the ways to communicate with the kernel or to invoke system
calls, especially during error or exception handling.

Software interrupts are generally used to make system calls.

3. EXTERNAL INTERRUPT

External interruptions are generated by peripheral devices, such as


keyboards, printers, communication cards, etc.The External
Interrupt occurs when any Input and Output Device request for any
Operation and the CPU will Execute that instructions first.
These interruptions are not sent directly to the CPU but they are
sent to an integrated circuit whose function is to exclusively handle
this type of interruptions.
Maskable AND Non-Maskable Interrupt
An interrupt that can be disabled by writing some instruction is
known as Maskable Interrupt.
A non-maskable interrupt (NMI) is a hardware interrupt that
standard interrupt-masking techniques in the system cannot ignore.
There are two types of interrupts used in 8085 Microprocessor:

1. Hardware Interrupts
2. Software Interrupts
A software interrupts is a particular instructions that can be
inserted into the desired location in the program. There are eight
Software interrupts in 8085 Microprocessor.

1. RST0
2. RST1
3. RST2
4. RST3
5. RST4
6. RST5
7. RST6
8. RST7

We can calculate the vector address of these interrupts using the


formula given below:

Vector Address = Interrupt Number * 8

Interrupt Vector Address(in hexadecimal)

1. RST0 ————– 0000 ——————————— 0*8=0


2. RST1 ————– 0008 ——————————— 1*8=8
3. RST2 ————– 0010 ——————————— 2*8=16
4. RST3 ————– 0018 ——————————— 3*8=24
5. RST4 ————– 0020 ——————————— 4*8=32
6. RST5 ————– 0028 ——————————— 5*8=40
7. RST6 ————– 0030 ——————————— 6*8=48
8. RST7 ————– 0038 ——————————— 7*8=56

Shortcut method to learn with trick:

step 1 : first interrupt will be same i.e RST0 =0000


step 2: Now Add 8 to result of first interrupt i.e 0+8= 0008
step 3: Now add 2 to result of second interrupt i.e 8+2=0010
step 4 : again add 8 into result of previous interrupt and the again
add 2 into result of previous interrupt so on.
HARDWARE INTERRUPT

Interrupt Vector Address(in hexadecimal)

1. TRAP—————-0024
2. RST7.5————–003C
3. RST6.5————–0034
4. RST5.5————–002C
5. INTR

TRAP > RST7.5 > RST6.5 > RST5.5 > INTR

TRAP has highest priority and RST7.5 has second highest priority and so on.

TRAP

It is non-maskable edge and level triggered interrupt. TRAP has the


highest priority and vector interrupt. Edge and level triggered
means that the TRAP must go high and remain high until it is
acknowledged.

 There are two ways to clear TRAP interrupt.

1.By resetting microprocessor (External signal)


2.By giving a high TRAP ACKNOWLEDGE (Internal signal)

RST7.5

It has the second highest priority. It is maskable and edge level


triggered interrupt.

It can also be reset or masked by reseting microprocessor.


It can also be resetted by DI instruction.

RST6.5 and RST5.5


BOTH interrupts are level triggered and maskable interrupts.

When RST6.5 pin is at logic 1, INTE flip-flop is set. RST 6.5 has third
highest priority and RST 5.5 has fourth highest priority.

IIt is disabled by,


1.DI, SIM instruction

2.System or processor reset.

3.After reorganization of interrupt.

INTR

It is level triggered and maskable interrupt.


It has lowest priority.
The following sequence of events occurs when INTR signal goes
high:

1. The 8085 checks the status of INTR signal during execution of


each instruction.
2. If INTR signal is high, then 8085 complete its current instruction
and sends active low interrupt acknowledge signal, if the
interrupt is enabled.
3. On receiving the instruction, the 8085 save the address of next
instruction on stack and execute received instruction.

You might also like