This project is under development for educational purposes, I was quite a newbe to C/C++ at the time I wrote this code, so be ready face some potential bugs. These Structures have various algorithmic and database related appliances.
- Stack
- Queue
- Heap (Min & Max)
- PriorityQueue (Min & Max)
- Tree (Binary Search Tree)
- TreeMap (BST Dictionary)
- ArrayList, ArrayList-Ext (Similar to Java's ArrayList, uses amortized dynamic memory allocation based on size)
- Nodes and Containers used in different data structures
A Stack
is a basic LIFO based data structured, useful in many appliances such as Topological Sort, Undo/Redo operations, Virtual machines (e.g JVM).
Stack
is found in adt.h
and respective member functions in adt.cpp
Stack<Type> *myStack=new Stack<Type>();
Stack<Type>{
Type* Top();
void Pop();
void Push(Type *Data);
int Size();
}
myStack->Push(new int(3));
- Inserts Data into the Stack.
int *Temp=Stack->Top()
- Returns the Last Inserted Data without removing it from the Stack.
myStack->Pop();
- Removes the Last Inserted Data from the Stack.
int Temp=myStack->Size();
- Returns the total number of elements inside the Stack.
A Queue
is a basic FIFO based data structured, useful in many appliances such as Web Server response management, or a Process Queue for the CPU.
Queue
is found in adt.h
and member functions in adr.cpp
Queue<Type> *myQueue=new Queue<Type>();
Queue<Type>{
Type* Top();
void Pop();
void Push(Type *Data);
int Size();
}
myQueue->Push(new int(3))
- Inserts Data into the Queue.
int *Temp=myQueue->Top();
- Returns the First Inserted Data without removing it from the Stack.
myQueue->Pop();
- Removes the First Inserted Data from the Stack.
int Temp=myQueue->Size()
- Returns the total number of elements inside the Stack.
The ArrayList
is a dynamic memory (de)allocating Array based structure, useful in many appliances such as Heaps and any other Database which needs Constant Insertion and Retrieval Complexity.
ArrayList
is found in arrays.h
and member functions in arrays.cpp
The Array Expands when it's filled up. It grows by a 1.5 factor, which close to the Golden Ration (φ = 1.61803398.. ) as it's proven to be very efficient and the Java API ArrayList also uses the same factor.
ArrayList_Ext
is a derived ArrayList
class which provides extended functions. Collapses when its used capacity is 45% of the available capacity. I have chosen this factor as after private research I concluded it's efficient enough. (Minimises repeatedly shrinking and growing of array so it keeps the amortised costs as low as possible).
For more info, there is a Wikipedia Article.
ArrayList<Type> *myArrayList=new ArrayList<Type>();
ArrayList_Ext<Type> *myArrayList=new ArrayList_Ext<Type>();
ArrayList<Type>{
void Insert(Type *Data);
Type* Get(int index);
void Set(int index,Type *Data);
void Reset();
int Size();
/* Member Functions below are only in ArrayList_Ext<Type> */
void RemoveLast();
int Find(Type *Data);
void ShrinkToFit();
}
myArrayList->Insert(new int(3));
- Inserts Data into the ArrayList.
int *Temp=myArrayList->Get(1);
- Returns the Data inside position of index. If index is not valid it throws an OutofBoundsException.
myArrayList->Set(1,new int(2));
- Changes the Data of position index with the new Data provided.
myArrayList->Reset();
- Deletes All Data inside the ArrayList and Initialises it in its original capacity (2).
int Temp=myStack->Size();
- Returns the total number of occupied positions inside the ArrayList.
myArrayList_Ext->RemoveLast();
- Deletes the last occupied position's Data. Useful for Heaps and Priority Queues.
int Temp=myArrayList_Ext->Find(new char'R');
- Performs Linear Search and returns index of Object's position.
myArrayList_Ext->ShrinkToFit();
- Shrinks Array to last occupied space.
A Heap is an essential Data Structure, used to guarantee O(log n) Retrieval and Insertion Time Complexity of the Biggest/Smallest Element in the Structure. Very usable for various Sorting Algorithms which rely on Operation with biggest/smallest element (Selection Sort).
Minheap
and MaxHeap
can be found in adt.h
and their member functions in adt.cpp
MinHeap<Type> *myMinHeap=new MinHeap<Type>();
MaxHeap<Type> *myMaxHeap=new MaxHeap<Type>();
MinHeap<Comparable>{
void Insert(Comparable *Data);
Comparable* Extract();
Comparable* Peek();
}
myMinHeap->Insert(new int(3));
- Inserts Data into the MinHeap.
Comparable *Temp=myMinHeap->Extract();
- Returns the lowest/biggest Data and Removes it from Heap.
Comparable *Temp=myMinHeap->Peek();
- Returns the lowest/highest Data without removing it from Heap.
A Priority Queue is quite as essential as the Heap, and performs almost likewise. It has equal properties to a Heap, yet its applications differ. The main difference is that a Priority Queue acts like a Map. While inserting Data, you also assign a priority value to the Data. The Data with smallest/biggest priority gets Extracted first.
Famous applications for Priority Queues include Dijkstras Shortest Path Algorithm, Prim-Jarnik Minimum Spanning Tree Algorithm etc.
MinPriorityQueue
and MaxPriorityQueue
are included in adt.h
and their respective member functions in adt.cpp
MinPriorityQueue<Comparable,Type> *myMinPQ=new MinPriorityQueue<Comparable,Type>();
MaxPriorityQueue<Comparable,Type> *myMaxPQ=new MaxPriorityQueue<Comparable,Type>();
MinPriorityQueue<Comparable, Type>{
void Insert(Comparable* Priority, Type* Data);
Type* Extract();
Type* Peek();
}
myMinPQ->Insert(new int(100),new char('R'));
- Inserts 'R' into the PQ with Priority 100.
char* Temp=myMinPQ->Extract();
- Returns the char with lowest/highest priority and removes it from PQ.
char* Temp=myMinPQ->Peek();
- Returns the char with lowest/highest priority without removing it from PQ.
A binary search tree is a rooted binary tree, whose internal nodes each store a key. The tree additionally satisfies the binary search tree property, which states that the key in each node must be greater than all keys stored in the left sub-tree, and smaller than all keys in right sub-tree.
You can find it in adt.h
and all respective member functions in adt.cpp
Tree<Type> *myBST=new Tree<Type>();
Tree<Type>{
void Insert(Comparable *Key);
void Delete(Comparable *Key);
void Reset();
void PrintInOrder();
void PrintGraph();
}
A TreeMap is a rooted binary tree, whose internal nodes each store a key and an associated value or data with it.
It's located in adt.h
and its members functions in adt.cpp
TreeMap<Comparable,Type> *myTreeMap=new TreeMap<COmparable,Type>();
TreeMap<Comparable,Type>{
void Insert(Comparable *Key, T *Data);
T* Extract(Comparable *Key);
void Edit(Comparable *Key, T *Data);
}