rafaelkallis / cpp_data_structures_training

Basic Data Structures in C++

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Data Structure Library for C++

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.

Structures Included:
Work In Progress: Red Black Trees, Hash Tables / Functions.

Stack

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

Constructor:
Stack<Type> *myStack=new Stack<Type>();
Member Functions:
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.

Queue

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

Constructor:
Queue<Type> *myQueue=new Queue<Type>();
Member Functions:
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.

ArrayList

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.

Constructor:
ArrayList<Type> *myArrayList=new ArrayList<Type>();
ArrayList_Ext<Type> *myArrayList=new ArrayList_Ext<Type>();
Member Functions:
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.
Only in ArrayList_Ext
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.

Heap

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

Constructor:
MinHeap<Type> *myMinHeap=new MinHeap<Type>();
MaxHeap<Type> *myMaxHeap=new MaxHeap<Type>();
Member Fucntions:
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.

PriorityQueue

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

Constructor:
MinPriorityQueue<Comparable,Type> *myMinPQ=new MinPriorityQueue<Comparable,Type>();
MaxPriorityQueue<Comparable,Type> *myMaxPQ=new MaxPriorityQueue<Comparable,Type>();
Member Functions:
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.

Tree

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

Constructor:
Tree<Type> *myBST=new Tree<Type>();
Member functions:
Tree<Type>{
    void     Insert(Comparable *Key);
    void     Delete(Comparable *Key);
    void     Reset();

    void     PrintInOrder();
    void     PrintGraph();
}

TreeMap

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

Constructor:
TreeMap<Comparable,Type> *myTreeMap=new TreeMap<COmparable,Type>();
Member Functions:
TreeMap<Comparable,Type>{
    void    Insert(Comparable *Key, T *Data);
    T*      Extract(Comparable *Key);
    void    Edit(Comparable *Key, T *Data);
}

About

Basic Data Structures in C++


Languages

Language:C++ 100.0%