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

Data Structure

Data structures notes

Uploaded by

Rasna P
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Data Structure

Data structures notes

Uploaded by

Rasna P
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Define data structure?

requirements grow as the input size Non -Primitive data types:


grows
Logical or mathematical model of These Data structures do not allow any
particular organization of data specific instructions to be performed on
the Data items directly.
8. What are the different operations that
can be performed on different data The different non primitive data types
Briefly describe the notation of the structure? are: Arrays, Structures, Unions Non-
space time tradeoff of algorithm. primitive data types are again divided
Ans: Operation means processing the into 2
Time complexity is a function describing data in the data structure.
the amount of time an algorithm takes 1. Linear
in terms of the amount of input to the The following are some important
algorithm. operations. 2. Non – linear

Space complexity is a function a. Traversing Linear Data structures:


describing the amount of memory
(space) an algorithm takes in terms of b. Searching This Data Structures involve arranging
the amount of input to the algorithm. the elements in linear fashion.
Big O notation is the language we use c. Inserting
for talking about how long an algorithm Eg: Stacks, Queue, Lists
d. Deleting
takes to run. Order of magnitude is often
Non- Linear Data structures:
called Big-O notation (for “order”) and e. Sorting
written as O(f(n)). This Data structures involve representing
f. Merging the elements in Hierarchical order.
Common Functions for Big-O
Traversing Eg: Trees, Graphs
F(n) Name
To visit or process each data exactly
1 Constant once in the data structure
logn Logarithmic Searching
Define string? Explain different string
n Linear To search for a particular value in the operations.
data structure for the given key value.
nlog n Log Linear
Ans: Strings are defined as an array of
Inserting characters. The difference between a
n2 Quadratic
To add a new value to the data structure character array and a string is the string
n3 Cubic
is terminated with a special character
Deleting ‘\0’.
2 n Exponential
To remove a value from the data Operations:
Eg:
structure
Linear search : O(n) 1) Length: Find length of the string.
Sorting [Number of characters in a string is
Binary search : O(log n) called its length.]
To arrange the values in the data
Bubble sort : O(n2) structure in a particular order. Eg: s=”computer’

Merge sort : O(n log n) Merging Length(s) = 8


To join two same type of data structure 2) Substring: Accessing a
values substring from a given string.
Write different applications of data
structure. 3) SUBSTRING(string,
initial,length)
1. Dynamic memory allocation What are the different categories of data
structure? Explain. Eg: S=”TO BE OR NOT TO BE” , 3,4) =BE O
2. Maintaining directory of names
Ans: 3) Concatenation: let S1 and S2 be
3. Performing arithmetic operations on strings s1//s2 is the string consisting of
long integers Primitive
the characters of s1 followed by the
Non-primitive characters of s2.
4. History of visited websites
Primitive data types: Eg: S1= ‘welcome”
5. job scheduling
These are the data structures which are S2=”home”
directly supported by the
S1//s2=” welcomehome”
What is the importance of Big-O machine.i.e,Any operation can be
notation? performed in these data items. The
different primitive data types are:
It is used to classify algorithms according Integer, Float, Double
to how their run time or space
4) Indexing (pattern matching): find the
position where a string pattern P
Firstappears in a given

string T.

Eg: T=”welcome to ABC college”


INDEX(T,”to”) =8

Explain Pattern matching algorithm

Ans: Pattern matching algorithm Pattern


Searching algorithms are used to find a
pattern or substring from another bigger
string. There are different algorithms.
The main goal to design these types of
algorithms to reduce the time

complexity. The traditional approach


may take lots of time to complete the
pattern searching task for a

longer text.

Eg: Main String:


“ABAAABCDBBABCDDEBCABC”, Pattern
“ABC”

Output:Pattern found at position: 4

Pattern found at position:

10

Pattern found at position: 18

Algorithm:

P and T are strings with length R and S.

1. set k=1 and MAX= S – R +1

2. 2. Repeat steps 3 to 5 while


K<=MAX

3. Repeat for L=1 to R

If P[L]≠T[K+L-1] then goto step 5

4. Set INDEX= K and exit

5. Set K=K+1

6. Set INDEX=0

7.Exit
• Linear: A linear describes data characteristics
whether the data items are arranged in
sequential form like an array.
Data structure 2D ARRAYThe two-dimensional array • Non-Linear: A Non-Linear data structure
Data structure is a representationof can be defined as an array of arrays. The describes the characteristics of data items that
logical relationship existing between 2D array is organized as matrices which are not in sequential form like a tree, graph.
individual elements of data. In other can be represented as the collection of • Static: It is a static data structure that
words, a data structure defines a way of describes the size and structures of a collection
rows and columns. However, 2D arrays of data items associated with a memory
organizing all data items that are created to implement a relational location at compile time that are fixed.
considersnot only the elements stored Example – Array.
database lookalike data structure. It
but also their relationship to each other. • Homogenous: It is a characteristic of data
The term data structure is used to provides ease of holding the bulk of data structures representing whether the data type
describe the way data is stored. at once which can be passed to any of all elements is the same. Example- Array.
number of functions wherever • Non-Homogenous: It is a characteristic of
Primitive Data Structures:- are the basic data structures representing whether the data
data structures that directly operate required.Syntax: data_type type elements may or may not be the same
upon themachine instructions. They array_name[rows][columns]; • Dynamic: It is a dynamic data structure that
have different representations on defines the shrinking and expanding of data
different computers. Integers, floating ARRAY VS LINKED LIST items at the run time or the program’s
Array:-•Size of an array is fixed execution. It is also related to the utilization of
point numbers, character constants, memory location that can be changed at the
string constants and pointers come •Memory is allocated from stack•It is program’s run time. Example: Linked list
under this category. necessary to specify the number of • It has some rules that define how the data
Non-primitive data structures:-are more elements during declaration (i.e., items are related to each other
complicated data structures and are duringcompile time).• It occupies less • It defines some rules to display the
memory than a linked list for the same relationship between data items and how they
derived fromprimitive data structures. interact with each other
They emphasize on grouping same or number of elements. •Inserting new • It has some operations used to perform on
different data items withrelationship elements at the front ispotentially data items like insertion, searching, deletion,
between each data item. Arrays, lists expensive because existing elements etc.
and files come under this category need to be shifted over to make • It helps in reducing the usage of memory
room•Deleting an element from an resources
•Time Complexity: The execution time of a
array is not possible. program in a data structure should be
Recursion: Linked List:-•Size of a list is not minimum as possible
A function is recursive if a statement in fixed•Memory is allocated from heap•It •Space Complexity: The memory usage
the body of the function calls itself. is not necessary to specify the Number through all data items in a data structure
of elements during declaration(i.e., should be less possible.
Recursion is The process of defining
something in terms of itself. For a memory is allocated during run Time).•It
computer language to be Recursive, a occupies more memory.•Deleting an LINEAR SEARCH?
function must be able to call itself. element is possible. A linear search, also known as a
LINKED LIST SINGLY LINKED LIST:-Linked List can be sequential search, is a method of finding
Linked lists and arrays are similar since defined as collection of objects an element within a list. It checks each
they both store collections of data. Array called nodes that are randomly stored in element of the list sequentially until a
is the Most common data structure used the memory.A node contains two fields match is found or the whole list has
to store collections of elements. Arrays i.e. data stored at that particular address been searched.
are Convenient to declare and provide and the pointer which contains the Algorithm:-
the easy syntax to access any element by address of the next node in the •begin with the leftmost element of
its index Number. Once the array is set memory.The last node of the list arr[] and one by one compare x with
up, access to any element is convenient contains pointer to the each element.
and fast TWO WAY LINKED LIST/DOUBLY LINKED •x matches with an element then return
Advantages of linked lists: LIST the index.
•Linked lists are dynamic data A Doubly Linked List (DLL) contains an •if x does not match with any of the
structures. i.e., they can grow or shrink extra pointer, typically called previous
elements then return -1.
during The execution of a program. pointer, together with next pointer and PROGRAM:-
•Linked lists have efficient memory data which are there in singly linked •#include<stdio.h>
utilization. Here, memory is not list.A doubly linked list or a two-way •Int main(){
pre-Allocated. Memory is allocated linked list is a more complex type of •Int a[20],I,x,n;
whenever it is required and it is linked list which contains a pointer to •Printf(“How many elements?”);
de-allocated when it is no longer the next as well as the previous node in •Scanf(“%d”,&n);
needed. •Insertion and Deletions are sequence, Therefore, it contains three •Printf(“Enter array elements:n”);
easier and efficient. parts are data, a pointer to the next •For(i=0;i<n;++i)• Scanf(“%d”,&a[i]);
•Linked lists provide flexibility•In node, and a pointer to the previous •Printf(“nEnter element to search:”);
inserting a data item at a specified node. This would enable us to traverse •Scanf(“%d”,&x);
position and deletion of the data item the list in the backward direction as well •For(i=0;i<n;++i)
From the given position. CIRCULAR LINKED LIST •If(a[i]==x)
•Many complex applications can be Circular linked list is a linked list where •Break;
easily carried out with linked lists. all nodes are connected to form a circle. •If(i<n)
There is no NULL at the end. A circular •Printf(“Element found at index %d”,i);
ARRAY:- linked list can be a singly circular linked •Else
An array is a collection of similar data list or doubly circular linked •Printf(“Element not found”);
elements stored at contiguous memory list.AdvantagesofCircularLinkedLists: •Return 0;}
locations. It is the simplest data 1) Any node can be a starting point. We
structure where each data element can can traverse the whole list by starting
be accessed directly by only using its from any point. 2)useful to
index number. … Rather, we can define implementation of queue
an array which will store the data
elements at contiguous memory
locations. Application of data structures
Data structures are integral part of
programming languages and when you
CHARACTERISTICS OF DS write a program and execute it, you will
be using a data structure in some or the Adding New Node BST:- •If an operand is encountered, add it to
other context. Whether you have used •Create a new BST node and assign Y.
data structures implicitly or explicitly values to it. •If a left parenthesis is encountered,
doesn’t matter. If you have not used any •Insert(node, key)If root == NULL, push it onto STACK.
data structure explicitly in your program, •Return the new node to the calling •if an operator is encountered, then(a)
operating system will use one while function.If root=>data < key Repeatedly pop from STACK and add to Y
executing your program •Call the insert function with each operator (on the top of STACK)
•Stacks are used in applications root=>right and assign the return value which has the same precedence as or
like:UNDO/REDO options in your text in root=>right. higher precedence than operator.(b)
editorStoring function calls in recursion, • Root->right = insert(root=>right,key) Add operator to STACK./*End of If
result that each function returns in • If root=>data > key structure */
recursion is stored in stack and when •Call the insert function with root->left •if a right parenthesis is encountered,
function returns the result if popped and assign the return value in root=>left. then :
from stack ,Storing browser history, •Root=>left = insert(root=>left,key) (a) Repeatedly pop from STACK and add
when you click back and forward •Finally, return the original root pointer to Y each operator (on the top of STACK)
buttons you visit the previous or next to the calling function. until a left parenthesis is encountered.
page that you visited respectively (b) Remove the left parenthesis. [Do not
• Queues:-are used in Scheduling add the left parenthesis to Y]./* end of If
processes like LRU (Least Recently APPLICATIONS OF TREES structure *//* end of Step 2 loop */
Used)In asynchronous programming • Storing naturally hierarchical data: •exit.
For storing your keystrokes as you type Trees are used to store the data in the
on your keyboard hierarchical structure. For example, the MEMORY ALLOCATION IN TWO
• Graphs are used in Network/ request file system. The file system stored on the DIMENSIONAL ARRAY
routing In algorithms like Djikstra’s disc drive, the file and folder are in the 1)using a single pointer:- A simple way
algorithm (whose modified version is form of the naturally hierarchical data is to allocate memory block of size r*c
used in Google ,Social networking sites and stored in the form of trees. and access elements using simple
• Trees:-A tree is also one of the data •Organize data: It is used to organize pointer arithmetic.
structures that represent hierarchical data for efficient insertion, deletion and 2)using an array of pointers:- We can
data. Suppose we want to show the searching. For example, a binary tree has create an array of pointers of size r. Note
employees and their positions in the a logN time for searching an element. that from C99, C language allows
hierarchical form. •Trie: It is a special kind of tree that is variable sized arrays. After creating an
•General tree is a tree in which each used to store the dictionary. It is a fast array of pointers, we can dynamically
node can have many children or nodes and efficient way for dynamic spell allocate memory for every row.
•The subtree of a general tree do not checking. 3)using pointer to a pointer:- We can
hold the ordered property •Heap: It is also a tree data structure create an array of pointers also
•In data structure, a general tree can not implemented using arrays. It is used to dynamically using a double pointer.
be empty implement priority queues. Once we have an array pointers
•B-Tree and B+Tree: B-Tree and B+Tree allocated dynamically, we can
Expression Trees are the tree data structures used to dynamically allocate memory and for
An expression tree is a representation of implement indexing in databases.
every row like method 2. 4) Using
expressions arranged in a tree-like data •Routing table: The tree data structure
is also used to store the data in routing double pointer and one malloc call;
structure. In other words, it is a tree
with leaves as operands of the tables in the routers.
GRAPH
expression and nodes contain the A graph can be defined as group of
operators. Similar to other data vertices and edges that are used to
Θ Notation: The theta notation bounds
structures, data interaction is also connect these vertices. A graph can be
a function from above and below, so it
possible in an expression tree. defines exact asymptotic behavior. A seen as a cyclic tree, where the vertices
Expression trees are mainly used for simple way to get Theta notation of an (Nodes) maintain any complex
analyzing, evaluating and modifying expression is to drop low order terms relationship among them instead of
expressions, especially complex and ignore leading constants having parent child relationship
expressions. • Big O Notation: The Big O notation WEiGHTED GRAPH
defines an upper bound of an algorithm, A WG is a graph with the property that
BINARY SEARCH TREE(BST) it bounds a function only from above. its edges have a number associated with
Binary Search Tree is a node-based For example, consider the case of it.the number is called weight of the
binary tree data structure which has the Insertion Sort. It takes linear time in best edge.The WG can be undirected or
following properties: case and quadratic time in worst case. directed
• The left subtree of a node contains We can safely say that the time DIRECTED GRAPH
only nodes with keys lesser than the complexity of Insertion sort is O(n^2). A DG is a graph with the property that it
node’s key Note that O(n^2) also covers linear time. edges have directions.They are also
• The right subtree of a node contains • Ω Notation: Just as Big O notation called digraph
only nodes with keys greater than the provides an asymptotic upper bound on
node’s key. a function, Ω notation provides an
• The left and right subtree each must asymptotic lower bound. Ω Notation can
also be a binary search tree. be useful when we have lower bound on
time complexity of an algorithm

The binary search tree is considered as


efficient data structure in compare to
arrays and linked lists. In searching DATA STRUCTURE CATEGORY:-
CONVERT INFIX TO POSTFIX Data structure can be divided into
process, it removes half sub-tree at
•Push “(“ onto stack, and add “)” to the following categories• Primitive data
every step. Searching for an element in a
end of X. structure• Non-primitive data structure
binary search tree takes o(log2n) time.
•Scan X from left to right and repeat Primitive data structure
In worst case, the time it takes to search
Steps 3 to 6 for each element of X until
an element is 0(n).
the STACK is empty :
Primitive data structure are the basic that the first and last elements are Ans:
structure and they are directly operated exchanged, the second and second last
upon by the machine instructions. element are exchanged, and so on for all An array is a list of finite number of n
The important primitive data structure other elements. homogeneous data elements.(i.e. data
are:Integer,Float,,Character,Pointer Stack using Linked List elements of the same type) such that
Non-primitive data structure In linked list implementation of stack,
Non-primitive data structure are derived the nodes are maintained The elements of an array are referenced
from the primitive data structures.They non-contiguously in the memory. Each by an index.
can allow structuring of homogeneous node contains a pointer to its immediate
or heterogenous data items.The successor node in the stack. Stack is said Elements of array stored at contiguous
important non-primitive data structure to be overflown if the space left in the memory locations.
are:Arrays,Lists,Files
memory heap is not enough to create a
PATTERN MATCHING ALGORITHM:- This makes it easier to calculate the
Pattern matching finds whether or not a node
position of each element by simply
given string pattern appears in a string
Push an element to stack using Linked adding an offset to a base value.
text. Commonly used pattern matching
algorithms are Naïve Algorithm for List
Step 1 - Create a newNode with given Traversal
pattern matching and pattern matching
algorithm using finite automata. value.
Insertion
Step 2 - Check whether stack
1. SET K=1 and MAX=S-R+1.
is Empty (top == NULL) Deletion
2. Repeat Step 3 to 5 while K<=MAX: Step 3 - If it is Empty, then Traversal
set newNode→ next = NULL.
3. Repeat for L=1 to R: If TEXT[K+L-1] ≠
It prints all the array elements one after
PAT[L], then: Go to Step 5. Step 4 - If it is Not Empty, then another.
set newNode→ next = top.
4. SET INDEX=K, and EXIT.
Algorithm
Step 5 - Finally, set top = newNode
5. K=K+1.6. SET INDEX=0.
LA is a linear array with lower bound LB
7. Exit and upper bound UB.
DELETE DUPLICATE ELEMENTS FROM
INSERTION INTO A QUEUE step 1: set k=LB
ARRAY
Step 1: IF REAR = MAX-1 Write • #include<stdio.h>•
OVERFLOW Goto step 4 [END OF IF] Step step 2: Repeat steps 3 and 4 while k<=UB
Intremove_duplicate_elements(intarr[],
2: IF FRONT=-1 and REAR=-1 SET FRONT step 3: Apply visit to LA[k]
int n){• If (n==0 || n==1)• Return n;• Int
= REAR = 0 ELSE SET REAR = REAR+1 temp[n];• Int j= 0;• Int I;• For (i=0; i<n-1;
[END OF IF] step 4: set k=k+1
i++)• If (arr[i] != arr[i+1])• Temp[j++] =
Step 3: SET QUEUE[REAR] = NUM
arr[i];• Temp[j++] = arr[n-1];• For (i=0; step 5: exit
Step 4: Exit
i<j; i++)• Arr[i] = temp[i];• Return j;}• int
main(){ Insertion

• int n; It adds an element at given index.


APPLICATION OF STACKS
• Evaluation of Arithmetic Expressions • scanf(“%d”,&n); Algorithm
A stack is a very effective data structure
for evaluating arithmetic expressions in • intarr[n]; INSERT(LA,N,K,ITEM)
programming languages. An arithmetic
expression consists of operands and • inti; LA is a linear array with N elements and
operators K is a positive integer K<=N. This
•In addition to operands and operators, • for(i = 0; i< n; i++){ algorithm inserts an element ITEM into
the arithmetic expression may also
include parenthesis like “left • scanf(“%d”,&arr[i]);} the Kth position in LA.
parenthesis”and“rightparenthesis.Stacks
can be used to check parenthesis • n = remove_duplicate_elements(arr, Step 1: set J=N
matching in an expression. n);
•Stacks can be used for Conversion from step 2: Repeat steps 3 and 4 while J>=K
one form of expression to another. • for (i=0; i<n; i++)
•Stacks can be used for Memory step 3: set LA[J+1]=LA[J]
Management. • printf(“%d “,arr[i]);
•Stack data structures are used in step 4: set J=J-1
backtracking problems • return 0;j
• Backtracking:-Backtracking is another step 5: set LA[K]=ITEM
application of Stack. It is a recursive Define circular linked list.
algorithm that is used for solving the step 6: set N=N+1
optimization problem. Circular linked list is a linked list where
all nodes are connected to form a circle. step 7: exit
There is no NULL at the end.
•Delimiter Checking:-The common Deletion
application of Stack is delimiter
checking, i.e., parsing that involves Operation of removing one of the
analyzing a source program syntactically. elements from LA.
It is also called parenthesis checking. What is linear array? What are the
• Reverse a Data:-To reverse a given set different operations on array. Algorithm
of data, we need to reorder the data so
DELETE(LA,N,K,ITEM) What are the different types of 1: if TOP = 0 then print: UNDERFLOW
notations? and return
LA is a linear array with N elements and
K is a positive integer ,K<=N. This Infix : 2: set ITEM = STACK[TOP]
algorithm deletes the Kth element
An expression is called the Infix 3: set TOP = TOP - 1
from LA. expression if the operator appears in
between the operands in the expression. 4: return
step 1: set ITEM=LA[K]
Example : A+B
step 2: Repeat for J=K to N-1
(A+B) * (C-D)
step 3: set LA[J]=LA[J+1]
Prefix :
step 4: set N=N-1
An expression is called the prefix
step 5: exit expression if the operator appears in the
expression before the operands. Write an algorithm to perform insertion
operation in a queue?
Prefix notation is also known as Polish
Notation. Ans: Insertion :Algorithm

Example : QINSERT( Queue, N, front, rear, item)

infix prefix Step 1 : Check if the queue is full.

A+B +AB Step 2 : If the queue is full, produce


overflow error and exit.
(A+B) * (C-D) *+AB-CD
[ rear = N ]
Postfix :
Step 3 : If the queue is not full,
An expression is called the postfix
expression if the operator appears in the (a) If rear=0 then set rear=front=1
expression after the operands. otherwise

Postfix notation is also known as Reverse (b) increment rear pointer to point the
Polish Notation or Suffix. next empty space.

Example : [rear = rear + 1]

Infix postfix Step 4 : Add data element to the queue


location, where the rear is pointing.
What is the main difference between A+B AB+
[ Queue[rear] = item ]
array and linked list?
(A+B)*(C-D) AB+CD-*
Step 5 : return success.
:An array is a list of finite number of n
homogeneous data elements.(i.e. data Deletion : Algorithm
elements of the same type) such that ?write down stack operations algorithms.
QDELETE( Queue, N, front, rear, item)
The elements of an array are referenced Two basic operations:
by an index. Elements of array stored at Step 1 : Check if the queue is EMPTY.
contiguous memory locations. a) Push : Insert an element into a stack.
Step 2 : If the queue is EMPTY, produce
This makes it easier to calculate the b) Pop : Delete an element from a stack. underflow error and exit.
position of each element by simply
adding an offset to a base value.Linked  Algorithm:PUSH [ front=0 ]
List is an ordered collection of elements
of same type, which are connected to PUSH(STACK,TOP,MAXSTK,ITEM) Step 3 : If the queue is not empty, then
each other using pointers. set item = Queue[front]
1: if TOP = MAXSTK then print:
Linked List supports Sequential Access, OVERFLOW and return. and perform (a) or(b)
which means to access any
2: set TOP = TOP + 1 (a) If front=N, then set rear=front=0
element/node in a linked list, we have to
otherwise
sequentially traverse the complete linked 3: set STACK[TOP] = ITEM
(b) increment front pointer to point the
list, upto that element.
4: return next empty space.
Linked list can be Linear(Singly), Doubly
 Algorithm:POP [ front = front + 1]
or Circular linked list.
POP(STACK,TOP,ITEM) Step 5 : return success.
CIRCULAR QUEUE 5. Syntax Tree: Used in Compilers. #include<stdlib.h>

Circular Queue is a linear data structure 6. K-D Tree: A space partitioning tree /* A binary tree node has data, pointer to
left child
in which the operations are performed used to organize points in K dimensional
based on FIFO (First In First Out) space. and a pointer to right child */
principle and the last position is
connected back to the first position to 7. Trie : Used to implement dictionaries struct node
make a circle.It is also called 'Ring with prefix lookup.
Buffer' {
8. Suffix Tree : For quick pattern
searching in a fixed text. int data;

struct node* left;


What is priority queue? 9. Spanning Trees and shortest path
trees are used in routers and bridges struct node* right;
Ans: It is a collection of elements such respectively in computer networks
that each element has been assigned a };
priority and such that the 10.As a workflow for compositing digital
images for visual effects. /* Helper function that allocates a new
node with the
order in which elements are deleted and
processed comes from the following given data and NULL left and right
rules: An element of higher priority is pointers. */
How to represent a tree using array?
processed before any element of lower
Write a program to delete a number
priority. Two elements with the same struct node* newNode(int data)
from binary tree?
priority are processed according to the
{
order in which they were added to ∙ Because an array's length is fixed at
compile time, if we use an array to
the queue. implement a tree we have to set a limit on
the number of nodes we will permit in the
tree. Our strategy is to fix the
What is an undirected graph?

maximum height of the tree (H), and make An undirected graph is a set of
Define forest. . the array big enough to hold any binary nodes and a set of links between
tree of this height the nodes. Each node is called
Forest is a collection of disjoint trees.
In other words, we can also say that (or less). We'll need an array of size a vertex, each link is called an edge,
forest is a collection of an acyclic (2**H)-1. Here is the biggest binary tree of
and each edge connects two
graph which is not connected. Here depth 3: root of the
vertices. The order of the two
is a pictorial representation of a connected vertices is unimportant.
tree (A): array position 1
An undirected graph is a finite set of
forest. ∙ root's left child (B): array position 2 vertices together with a finite
∙ root's right child (C): array position 3 set of edges.
∙ ...

∙ left child of node in array position K: array


position 2K

∙ right child of node in array position K:


array position 2K+1

∙ So D is in position 2*2 (4), E is in position


2*2+1 (5), F is in position 2*3 (6), G is in
position 2*3+1

(7). root of the tree (A): array position 1


What are the applications of tree?
∙ root's left child (B): array position 2
1. Store hierarchical data, like folder
∙ root's right child (C): array position 3
structure, organization structure,
XML/HTML data. ∙ ...

2. Binary Search Tree :is a tree that ∙ left child of node in array position K: array
allows fast search, insert, delete on a position 2K
sorted data. It also
∙ right child of node in array position K:
allows finding closest item array position 2K+1

So D is in position 2*2 (4), E is in position


3. Heap: is a tree data structure which is 2*2+1 (5), F is in position 2*3 (6), G is in
implemented using arrays and used to position 2*3+1
implement priority
(7).
queues.
// C program to Delete a Tree
4. B-Tree and B+ Tree : They are used to
#include<stdio.h>
implement indexing in databases.

You might also like