-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
1,086 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 % |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.