Skip to content

Commit

Permalink
greedy floyd warshall dij
Browse files Browse the repository at this point in the history
  • Loading branch information
altanai committed Jun 6, 2021
1 parent 961a681 commit d3ed6cd
Show file tree
Hide file tree
Showing 39 changed files with 2,631 additions and 71 deletions.
3 changes: 1 addition & 2 deletions binarytree_algorithms/HeapBottomUp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ void printarray(int A[], int size){
}


// approach 1 : bottom-up order.
// approach 1 : bottom-up order.
// Heapify procedure applied to a node only if its children nodes are heapified
// 2(n − log 2 (n + 1))
void heapify(int arr[], int n, int i){
int largest = i;
int l = 2*i +1 ;
int r = 2*i +2 ;

if( l<n && arr[l]>arr[largest]) largest=l;
if( r<n && arr[r]>arr[largest]) largest=r;
if(largest !=i ) {
Expand Down
222 changes: 222 additions & 0 deletions binarytree_algorithms/arr_bst_inorder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
#include<iostream>
#include</home/altanai/Documents/algorithms/algorithmscpp/binarytree_algorithms/printBT.cpp>
#include <queue>
using namespace std;
// #include</home/altanai/Documents/algorithms/algorithmscpp/binarytree_algorithms/traversal.h>
// #include</home/altanai/Documents/algorithms/algorithmscpp/binarytree_algorithms/delete_insert.h>

typedef struct node{
int data;
struct node* left;
struct node* right;
node(int val){
data=val;
left= NULL;
right = NULL;
}
} node;

// ----------------------- utility function insertbst and deletebst
struct node* insertbst(struct node* root, int val){
if(root == NULL)
root = new node(val);
if(val > root->data)
root->right = insertbst(root->right, val);
else if(val < root->data)
root->left = insertbst(root->left, val);
return root;
}
struct node* minValueNode(struct node* node){
struct node* current = node;
while (current && current->left != NULL)
current = current->left;
return current;
}

struct node* deletebst(struct node* root, int key){
if (root == NULL) return root;

if (key < root->data)
root->left = deletebst(root->left, key);
else if (key > root->data)
root->right = deletebst(root->right, key);
else {
if (root->left==NULL ){
struct node* temp = root->right;
free(root);
return temp;
}else if (root->right == NULL) {
struct node* temp = root->left;
free(root);
return temp;
}
struct node* temp = minValueNode(root->right);
root->data = temp->data;
root->right = deletebst(root->right, temp->data);
}
return root;
}

// ----------------------- utility function array to BST
// pointer to pointer
// first pointer is used to store the address of the variable.
// And the second pointer is used to store the address of the first pointer.
// use : To retain change in the Memory-Allocation or Assignment even outside of a function call
void insert(node ** root, int val){
if(*root == NULL)
*root = new node(val);
else if((*root)->data <= val)
insert(&((*root)->right), val);
else if((*root)->data > val)
insert(&((*root)->left), val);
}
node* getBST(int * arr, int n){
node * root = NULL;
for(int i = 0; i < n; i++)
insert(&root, arr[i]);
// insertbst(root,arr[i]);
return root;
}
void inorder(node * root, int i, int arr[]){
if(root && root->left) inorder(root->left, i++, arr);
if(root){
std::cout<<root->data<<" , ";
arr[i]=root->data;
}
if(root && root->right) inorder(root->right, i++, arr);
}


// ----------------------- utility function traversal
// Inorder is L -> Root -> R
void inorder(node* root){
if(root ==NULL) return ;
inorder(root->left);
cout << root->data << " " ;
inorder(root->right);
}
// Preorder is Root -> L -> R
void preorder(node* root){
if(root ==NULL) return;
cout << root->data << " " ;
preorder(root->left );
preorder(root->right);
}
// Postorder is L -> R -> Root
void postorder(node* root){
if(root ==NULL) return;
postorder(root->left);
postorder(root->right);
cout << root->data << " " ;
}

// ----------------------- utility function printBT
void printBT(const std::string& prefix, const struct node* root, bool isLeft){
if( root != nullptr )
{
std::cout << prefix;

std::cout << (isLeft ? "├──" : "└──" );

std::cout << root->data << std::endl;
printBT( prefix + (isLeft ? "" : " "), root->left, true);
printBT( prefix + (isLeft ? "" : " "), root->right, false);
}
}
void printBT(const struct node* root)
{
printBT("", root, false);
}

// ----------------------- utility function level order traversal
int height(node *root ){
if(root==NULL) return 0;
return 1+ max(height(root-> left) , height(root->right));
}
void printq(queue<node*> q){
queue<node*> qcopy = q;
cout<<"\n print Queue ";
while(!qcopy.empty()){
cout << " \t " << qcopy.front();
qcopy.pop();
}
cout << '\n';
}
void levelordertraversal(node* root, int i , int orderarr[]){
int h = height(root);
cout << " height "<< h << endl;
if(root == NULL ) return;
queue<node*> q; // empty queue
node* curr; // node to store front element
q.push(root);
q.push(NULL);
while(q.size()>1){
curr = q.front();
q.pop();

if(curr ==NULL){
q.push(NULL);
cout << "\n";
}else{
if(curr->left) q.push(curr->left);
if(curr->right) q.push(curr->right);
cout << curr->data << " " ; orderarr[i]=curr->data; i++;
}
}
printq(q);
}


// ----------------------- main
int main()
{
// int arr[] = {10,5,15,5,6,7,8,89};
// int arr[] = {28,39,2,17,56,10,30,5,12,85,64};
int arr[]={10,3,12,1,7,11,14,13,15};
int n = sizeof(arr)/sizeof(arr[0]);

BinTree<int> bt;
for(int j=0;j<n;j++)
bt.insert(arr[j]);
cout << "Tree from OP:\n\n";
bt.Dump();
cout << "\n\n";
bt.clear();

struct node * root = getBST(arr, n);

cout << "\n inorder ";
inorder(root);

cout << "\n postorder ";
postorder(root);

cout << "\n preorder ";
preorder(root);
cout<<endl;


// int arr2[n];
// // int rarr2[n];
root = deletebst(root, 3);
root = deletebst(root, 12);
root = deletebst(root, 10);
// printBT(root);

root = insertbst(root,2);
root = insertbst(root,21);

cout<< "\n level order tarversal ";
levelordertraversal(root, 0 , arr);
BinTree<int> bt2;
for(int j=0;j<n;j++)
bt2.insert(arr[j]);
cout << "\n Balanced BST :\n\n";
bt2.Dump();
cout << "\n\n";
bt2.clear();



return 0;
}
2 changes: 1 addition & 1 deletion binarytree_algorithms/check_binaryhtree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ bool isBST(struct node* root, int min , int max){
if(root == NULL) return true;

// not BST if this node violates the min/max constraint
if(root->data <min || root->data > max) return false;
if(root->data < min || root->data > max) return false;

// recisively check for iBST on left-subtree and-right subtree
return isBST(root->left, INT_MIN, root->data-1) && isBST(root->right, root->data+1, INT_MAX);
Expand Down
79 changes: 79 additions & 0 deletions binarytree_algorithms/heapsort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// HeapSort
// heapify procedure calls itself recursively to build heap in top down manner.
// find the max at mtop abd place it ar end , repeat for all
//
// Created by @altanai on 31/03/21.
//

// Time complexity
// best case upper bound : O(nLogn)
// avg case upper bound : O(nLogn)
// worst case upper bound : O(nLogn)

// space complexity
// worst : O(1)

#include<iostream>
using namespace std;

void heapify(int arr[], int n, int i){
int largest = i;
int l = 2*i+1;
int r = 2*i+2;
if(l<n && arr[l] > arr[largest]) largest= l;
if(r<n && arr[r] > arr[largest]) largest= r;
if(largest != i){
swap(arr[i],arr[largest]);
heapify(arr,n,l);
}
}

void printArr(int arr[], int n){
for(int i=0;i<n;i++){
cout << arr[i];
}
cout<<endl;
}

void heapsort(int arr[], int n ){
for(int i=(n/2-1);i>=0;i--){
heapify(arr,n,i); // build heap
}
cout << "Heapified "<<endl ;
printArr(arr,n);

for(int i=(n-1);i>=0;i--){
swap(arr[0],arr[i]); // push max( current root) to the end
heapify(arr,i,0); // heapify the remainig elements
printArr(arr,n);
}
}

int main(){
// int arr[]={1,2,3,6,4,7,5};

// int arr[]={2,9,7,6,5,8};
int arr[]={1, 8, 6, 5, 3, 7, 4};

int n = sizeof(arr)/sizeof(arr[0]);

heapsort(arr, n);
cout << "Heapsort result "<<endl ;
printArr(arr,n);

return 0;
}

// g++ heapsort.cpp -o heapsort.out
// ./heapsort.out
// Heapified
// 7612435
// 6512437
// 5412367
// 4312567
// 3214567
// 2134567
// 1234567
// 1234567
// Heapsort result
// 1234567
4 changes: 1 addition & 3 deletions binarytree_algorithms/height_binarytree.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
//
// Created by @altanai on 31/03/21.
//

// Heighgt of binary tree / number of levels in binary tree
// Height of binary tree / number of levels in binary tree

// The approach is to calculate the height of the left and right subtree. The height of a subtree rooted at any node will be one more than the maximum height of its left and right subtree.
// Recursively apply this property to all tree nodes in a bottom-up manner (postorder fashion) and return the subtree’s maximum height rooted at that node


#include <iostream>
using namespace std;

Expand Down
17 changes: 17 additions & 0 deletions binarytree_algorithms/prioritq.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include<iostream>
using namespace std;

struct{
int item;
int priority;
};

// priroty queues using arrays
// insert : adding element at end O(1)
// gethighestpriority : linear seraching all item for highesr priority O(n)
// delete : expensive as it involves to move all elemenet 1 space

// priority queues using heaps
// insert : O(logn)
// gethighestpriroty : O(1)
// delete : O(logn)
26 changes: 26 additions & 0 deletions binarytree_algorithms/search_min_heap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Search min in MaxHeap

// Since heap is balanced binary tree with parental dominance , the min element will be in leaf nodes
// start from ceil(n/2) to n
// Time complexisty O(n) as 1/2 doesnt affect asymptotic complexity


// same process can be used to find the maximum element in min heap

#include<bits/stdc++.h>
using namespace std;

int findmin(int heap[], int n){
int minelm = heap[n/2];
for(int i=1+n/2; i<n;i++){
minelm = min(minelm,heap[i]);
}
return minelm;
}

int main(){
int heap[] = { 20, 18, 10, 12, 9, 9, 3, 5, 6, 8 };
int n = sizeof(heap)/sizeof(heap[0]);
cout << " min element "<< findmin(heap,n);
return 0;
}
Binary file added binarytree_algorithms/t_insert_delete.cpp
Binary file not shown.
Loading

0 comments on commit d3ed6cd

Please sign in to comment.