Skip to content

Commit

Permalink
heapify min/ max , arr to bt
Browse files Browse the repository at this point in the history
  • Loading branch information
altanai committed May 17, 2021
1 parent 9f9bacb commit 961a681
Show file tree
Hide file tree
Showing 28 changed files with 1,086 additions and 40 deletions.
61 changes: 56 additions & 5 deletions binarytree_algorithms/HeapBottomUp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <iostream>
using namespace std;


//Constructs a heap from elements of a given array by the bottom-up algorithm
//Input: An array H [1..n] of orderable items
//Output: A heap H [1..n]
Expand Down Expand Up @@ -37,8 +36,8 @@ void printarray(int A[], int size){
}


// Approach1 : bottom-up order.
// Heapify procedure applied to a node only if its children nodes are heapified
// 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;
Expand Down Expand Up @@ -69,14 +68,66 @@ void heaptopdown(int arr[], int n){

}

// ----------------------- utility function
struct node{
int data;
struct node* left;
struct node* right;
node(int value){
data=value;
left=NULL;
right=NULL;
}
};
node* insertarr_bt(int arr[], node* root, int i, int n){
if(i<n){
root = new node(arr[i]);
root->left = insertarr_bt(arr, root->left, 2*i+1, n);
root->right = insertarr_bt(arr, root->right, 2*i+2, n);
}
return root;
}
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);
}

int main(){
int arr[] = {12,15,19,10,8,16,5};//{2,9,7,6,5,8}; //{3,5,1,7,2,8};
int arr[] = {12,15,19,10,8,16,5}; //{2,9,7,6,5,8}; //{3,5,1,7,2,8};
int arr_size = sizeof(arr)/sizeof(arr[0]);

heapbottomup(arr, arr_size);
printarray(arr, arr_size);

int n = sizeof(arr)/sizeof(arr[0]);
struct node* root = insertarr_bt(arr, root, 0, n);
printBT(root);

return 0;
}

// g++ HeapBottomUp.cpp -o HeapBottomUp.out
// ./HeapBottomUp.out
// ./HeapBottomUp.out

// Outpupt
// 12 15 19 10 8 16 5
// 12 15 19 10 8 16 5
// 19 15 16 10 8 12 5
// 19 15 16 10 8 12 5
// └──19
// ├──15
// │ ├──10
// │ └──8
// └──16
// ├──12
// └──5
79 changes: 79 additions & 0 deletions binarytree_algorithms/arr_bt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Binary tree from array , via level order readings
//
// Created by @altanai on 31/03/21.
//

#include<iostream>
using namespace std;

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

node* insertarr_bt(int arr[],node* root, int i, int n){
if(i<n){
root = new node(arr[i]);
root->left = insertarr_bt(arr, root->left, 2*i+1, n);
root->right = insertarr_bt(arr, root->right, 2*i+2, n);
}
return root;
}

void inorder(struct node* root){
if(!root) return;
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}

// ----------------------- utility function start
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);
}


int main(){

// int arr[] = { 5,1,7 };
int arr[] = { 1,2,3,4,5,-1,6 }; // assume -1 is null
// int arr[] = { 1, 2, 3, 4, 5, 6, 6, 6, 6 };

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

struct node* root = insertarr_bt(arr, root ,0, n);
printBT(root);
inorder(root);
return 0;
}

// g++ arr_bt.cpp -o arr_bt.out
// ./arr_bt.out

// output for array [ 1,2,3,4,5,-1,6]

// └──1
// ├──2
// │ ├──4
// │ └──5
// └──3
// ├──-1
// └──6
// 4 2 5 1 -1 3 6 %
145 changes: 145 additions & 0 deletions binarytree_algorithms/bst_insert_delete.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// Insert / Delete node in BST by level order

//
// Created by @altanai on 31/03/21.
//

#include<iostream>
using namespace std;

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


// Search(data)
// Begin
// If node == null or node->data == val
// return node
// If(data >root->data)
// Search(node->right)
// Else If(data < root->data)
// Search(node->left,data)
// Return node;
// end
node* searchbst(struct node* root, int val){
if(root==NULL || root->data == val)
return root;
if(val > root->data)
searchbst(root->right, val);
else if( val < root->data)
searchbst(root->left, val);
}

// Insert(data)
// Begin
// If node == null
// Return createNode(data)
// If(data >root->data)
// Node->right = insert(node->right,data)
// Else If(data < root->data)
// Node->left = insert(node->left,data)
// Return node;
// end
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;
}

node* deletebst(struct node* root, int val){
if(root->data == val && root->left == NULL && root->right == NULL)
root = NULL;
if(val > root->data)
root->right = deletebst(root->right, val);
else if(val < root->data)
root->left = deletebst(root->left, val);
return root;
}

// ----------------------- utility function start
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);
}

int main(){

struct node* root = new node(5);
root->left = new node(1);
root->right = new node(7);

printBT(root);

// root = insertbst(root,11);
// printBT(root);

// root = insertbst(root,2);
// printBT(root);

// root = insertbst(root,3);
// printBT(root);


struct node* snode = searchbst(root,1);
printBT(snode); // Will print seg fault if u look up element no presnet in BST ,todo : handle case for errors
cout<< snode->data;

// When the tobe deleted node is a Leaf Node
// root = deletebst(root,7);
// printBT(root);

// When the tobe deleted node has 1 child

// When the tobe deleted node has 2 children

return 0;
}

// g++ bst_insert_delete.cpp -o bst_insert_delete.out
// ./bst_insert_delete.out

// outout insert

// └──5
// ├──1
// └──7
// └──5
// ├──1
// └──7
// └──11
// └──5
// ├──1
// │ └──2
// └──7
// └──11
// └──5
// ├──1
// │ └──2
// │ └──3
// └──7
// └──11
Loading

0 comments on commit 961a681

Please sign in to comment.