Professional Documents
Culture Documents
memory locations.
The important terms of array are:
Element − Each item stored in an array is called an
element.
Index − Each location of an element in an array has a
numerical index, which is used to identify the element.
Types of an array-
One dimensional Array- A one dimensional array is also called
a single dimensional array where the elements will be accessed
in sequential order. This type of array will be accessed by the
subscript of either a column or row index.
Two-dimensional Array- An array which has two subscript is
known as 2-D array it is also known as matrix.
Implementation:
There are two ways to implement a stack:
Using array
Using linked list
OVERFLOW-
When the stack is full and you still try to push an element in, the
condition is called stack overflow.
UNDERFLOW-
When the stack is empty and an element is popped of the stack,
the condition is called stack underflow.
PUSH operation
If we try to insert the element in a stack, and the stack is full, then
the overflow condition occurs.
When the new element is pushed in a stack, first, the value of the
top gets incremented, i.e., top=top+1, and the element will be
placed at the new position of the top.
POP operation
Before deleting the element from the stack, we check whether the
stack is empty.
Postfix Expression:
Circular Queue:
Merge Sort:
Basic Operations
Following are the basic operations of a tree −
Search − Searches an element in a tree.
Insert − Inserts an element in a tree.
Pre-order Traversal − Traverses a tree in a pre-order
manner.
In-order Traversal − Traverses a tree in an in-order
manner.
Post-order Traversal − Traverses a tree in a post-order
manner.
Inorder Traversal:
Algorithm Inorder(tree)
1. Traverse the left subtree, i.e., call Inorder(left-subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right-subtree)
If a binary tree is traversed in-order, the output will produce
sorted key values in an ascending order.
Uses of Inorder
In the case of binary search trees (BST), Inorder traversal gives
nodes in non-decreasing order. To get nodes of BST in non-
increasing order, a variation of Inorder traversal where Inorder
traversal s reversed can be used.
Complete binary tree: All the levels in the trees are full of
last level’s possible exceptions. Similarly, all the nodes are
full, directing the far left.
Full binary tree: All the nodes have 2 child nodes except
the leaf.
Balanced or Perfect binary tree: In the tree, all the nodes
have two children. Besides, there is the same level of each
sub node.
Search Operation
Always initiate analyzing tree at the root node and then move
further to either the right or left subtree of the root node
depending upon the element to be located is either less or greater
than the root.
Insert Operation
First, the root node is inserted, then the next value is compared
with the root node. If the value is greater than root, it is added to
the right subtree, and if it is lesser than the root, it is added to the
left subtree.
Delete Operations
There are multiple cases handled for deletion in the BST.
Case 2 – Node with one child: once you delete the node,
simply connect its child node with the parent node of the
deleted value.
Graph:
char str_name[size];
Arrays of Strings-
An array of strings is just a two-dimensional array of characters.
Balance Factor:
difference between the height of the left subtree and that of the
AVL tree. The value of the balance factor can possess three
1. Search Operation
2. Insertion Operation
3. Deletion Operation
Insertion Operation
Insertion is a three-step process −
Deletion Operation
Deletion is a two-step process −
Navigation Operation
Navigation is a recursive step process and is basis of many
operations like search, delete etc. −
of the first element i.e. head -> data. Then we will traverse the
linked list element by element. And then compare the value of the
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void printLargestSmallestLinkedList(struct Node* head) {
int maxElement = INT_MIN;
int minElement = INT_MAX;
while (head != NULL) {
if (minElement > head->data)
minElement = head->data;
if (maxElement < head->data)
maxElement = head->data;
head = head->next;
}
cout<<"Smallest element in the linked list is :
"<<minElement<<endl;
cout<<"Largest element in the linked list is :
"<<maxElement<<endl;
}
void push(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node));
newNode->data = data;
newNode->next = (*head);
(*head) = newNode;
}
int main() {
struct Node* head = NULL;
push(&head, 5);
push(&head, 2);
push(&head, 7);
push(&head, 3);
push(&head, 9);
push(&head, 1);
push(&head, 4);
printLargestSmallestLinkedList(head);
return 0;
}