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

applications of datastructure

datastructure using python

Uploaded by

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

applications of datastructure

datastructure using python

Uploaded by

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

Applications

Of
Data_structure
• Stack:-
Check whether the string is palindrome [O (n)]:
Algorithms

1- Make to indexes one at the beginning ‘i’ and the other at the
end ‘j’
2- Compare ‘i’ and j values if they are the same:
True: increment ‘i’, decrease ‘j’
False: the string is not palindrome
3- If ‘i’ and ‘j’ met at the middle(X) the n the string is
palindrome.

Code
Output

Checking balancing of symbols [O (n)]:


Algorithms

1- Create a stack
2- Iterate for symbols in the given string a. Ignore any
symbol expect the parentheses
b. Push the symbol to the stack if it is opening (, {, [
c. If it is closing, check if the stack empty reports an error,
if not pop the stack.
d. If the popped symbol does not correspond to an opening
symbol, report, and error.
3- At the end, if the stack is empty then the symbols are
balanced.
Code

Output
• Tree:-
This tree represents the expression ((((3
+1)*3)/((9 −5)+2))−((3×(7−4)) +6)).
Algorithms

1- Built tree nodes.


2- use in order traverse by recursion.
3- insert the data .
4- make edges
5- call the function
The tree
Code
Output
Arrange elements of the tree ascending by
preorder traverse:
Algorithms

1- Create the binary search tree.


2- Create the preorder traverse by recursion .
3- Insert the data.
4- Call the function.
Code
Output

• Queue:-
When you go to super market you take a
number to wait this example print the first
element and dequeue it:
Algorithms

1-Creat the queue by fixed array.


2- Create enqueue and dequeue functions.
3-insert the data
4-determine the size.
5- print the front element and dequeue it after print.
Code

Output
• Linked list:-
Implement stack using linked-list:
Algorithms

- First, we will make a node of singly linked list


- Push operation implementation same as insert at
beginning:
• Create a node
• Point the Node to the stack head
• Make the node is the top element

- Pop operation is implemented by deleting the node from


beginning: • Check if the stack is empty(error)
• Assign top element to variable(temp)
• Set the head value to temp.next

Code
Output
• Graph:-
Represent a graph by adjacency matrix
V = {‘el maadi’, ’helwan’, ’dar el Salam’, ’el
basaten ‘}
E = {{‘el maadi’, ’helwan’},{’helwan’, ’dar el
salam’},{’dar el salam’, ’el basaten ‘},{’el
basaten ‘,’helwan’},{ el maadi’, ’dar el salam’}}
Algorithm

1- Add nodes.
2- Add edges.
3- Print adj matrix.
Code
Output

Count the number of connected components


of Graph G which is
Represented in the adjacent matrix:
Algorithm

1. Initialize a variable count to 0 and a Boolean array


visited of size V,
Where V is the number of vertices in the graph.
2. for each vertex v in the graph:
3. If v has not been visited, increment count by 1 and
perform a breadth-
First search starting at v to mark all the vertices reachable
from v as
Visited.
4. Return count as the number of connected component
Code

Output

You might also like