Skip to content

Commit

Permalink
more sorting and binary tree algorithsm including heapify
Browse files Browse the repository at this point in the history
  • Loading branch information
altanai committed May 8, 2021
1 parent 8747419 commit 4aa3a53
Show file tree
Hide file tree
Showing 24 changed files with 1,704 additions and 4 deletions.
82 changes: 82 additions & 0 deletions binarytree_algorithms/HeapBottomUp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Bottom-Up Heap Construction
// heapify procedure calls itself recursively to build heap in top down manner.
//
// Created by @altanai on 31/03/21.
//

#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]
// void heapbottomup(int arr[] , int n){
// for (int i =0; i<=n/2; i++){
// int k = i;
// int v = arr[k];
// bool heap = false;
// while( !heap && 2*k <= n ){
// int j = 2*k;
// if(arr[j]< arr[j+1]) j++;
// if(v >= arr[j]) {
// heap = true;
// }else {
// arr[k] = arr[j];
// k = j;
// }
// }
// arr[k]=v;
// }
// }

void printarray(int A[], int size){
for (int i = 0; i < size; i++)
cout << A[i] << " ";
cout<< endl;
}


// Approach1 : 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 ) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}

void heapbottomup(int arr[] , int n){
int nonleftindex = n/2 -1;
for(int i = nonleftindex; i>=0; i--){
heapify(arr, n,i);
printarray(arr, n);
}
}


// approach 2 : Top Down heap order
// constructs a heap by successive insertions of a new key into a previously constructed heap
// height of a heap with n nodes is about log2 n, the time efficiency of insertion is in O(log n).
void heaptopdown(int arr[], int n){

}


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_size = sizeof(arr)/sizeof(arr[0]);
heapbottomup(arr, arr_size);
printarray(arr, arr_size);
return 0;
}

// g++ HeapBottomUp.cpp -o HeapBottomUp.out
// ./HeapBottomUp.out
42 changes: 42 additions & 0 deletions binarytree_algorithms/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Binary Tree operations


**using stucts instead of class for creating binary tree**

Although even classes can be sued for creating teh nodes, I use structs often.

class Node {
public:
int data;
Node *left, *right;
Node(int data)
{
this->data = data;
this->left = NULL;
this->right = NULL;
}
};

or

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

Structs are value type whereas Classes are reference type. Structs are stored on the stack whereas Classes are stored on the heap. ... When you copy struct into another struct, a new copy of that struct gets created modified of one struct won't affect the value of the other struct.

**YTraversing the binary tree, using queue based ietration vs recussion**

For simple operatiosn such as level order traversal or min , max while have O(n) class efificieny either approach is good .
I Use ietraion for simple operations where I know all n elemenst will be toughed such as height or tree , width of tree .

For operations like inorder, post order, preorder , either can be used .

For serach or sort operations however, it is better to adopt recussion since code looks much cleaner.
35 changes: 35 additions & 0 deletions binarytree_algorithms/binarytree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// 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;
}
};

int main(){

struct node* root = new node(5);

root->left = new node(3);
root->right = new node(7);
root->left->left = new node(2);

cout<<root->data ;
return 0;
}

// g++ binarytree.cpp -o binarytree
// ./binarytree
54 changes: 54 additions & 0 deletions binarytree_algorithms/check_binaryhtree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Check if the binary tree is a binary serach tree ( BST)

// binary serach tree :
// - left subtree of a node contains only nodes with keys < node’s key.
// - right subtree of a node contains only nodes with keys > node’s key.
//
// Created by @altanai on 31/03/21.
//

#include <iostream>
#include <climits>
using namespace std;

struct node
{
int data;
struct node* left;
struct node* right;

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

// appriach 1 :
// assumes no duplicate elements with value INT_MIN or INT_MAX
bool isBST(struct node* root, int min , int max){

// empty free is BST
if(root == NULL) return true;

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

// recisively check for iBS on left subtree and right subtree
return isBST(root->left, INT_MIN, root->data-1 ) && isBST(root->right, root->data+1, INT_MAX);
}

int main(){

struct node* root = new node(1);
root->left = new node(2);
root->right = new node(3);
root->left->left = new node(4);

cout<<isBST(root,INT_MIN, INT_MAX)<<endl ;
return 0;
}

// g++ check_binaryhtree.cpp -o checkbst.out
// ./checkbst.out
14 changes: 14 additions & 0 deletions binarytree_algorithms/heapify.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Heapify

// If the parent node is stored at index I,
// the left child can be calculated by 2 * I + 1 and right child by 2 * I + 2 (assuming the indexing starts at 0).

#include <iostream>
using namespace std;



void main(){
int arr[]={6,4,5,3,1,2};
int arr_size= sizeof(arr)/sizeof(arr[0]);
}
45 changes: 45 additions & 0 deletions binarytree_algorithms/height_binarytree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// Created by @altanai on 31/03/21.
//

// Heighgt 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;

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

// Computes recursively the height of a binary tree
// Input: A binary tree T
// Output: The height of T
// if T = ∅ return −1
// else return max{Height(T lef t ), Height(T right )} + 1
int height(node *currnode ){
if(currnode==NULL) return 0;// Base case: empty tree has a height of 0
// Single node tree has height of 1
return 1+ max(height(currnode-> left) , height(currnode->right));
}

int main() {
struct node* root = new node(1);
root->left = new node(2);
root->left->left = new node(3);
root->left->right = new node(4);
root->right = new node(5);
root->right->left = new node(6);
cout << "Height of tree " << height(root);
return 0;
}
Loading

0 comments on commit 4aa3a53

Please sign in to comment.