Skip to content

Commit

Permalink
cprogram and pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
altanai committed Jan 7, 2022
1 parent 02ebc23 commit 8e14652
Show file tree
Hide file tree
Showing 66 changed files with 2,905 additions and 245 deletions.
102 changes: 0 additions & 102 deletions Polynomial_evaluation.cpp

This file was deleted.

23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@ If T(n) is the run time , Cop is the cost of basic operation and C(n) is the num

T(n) ≈ Cop ⨉ C(n)

### Prereq
Check if gdb is installed and updated

sudo apt-get install build-essential gdb

build-essential is already the newest version (12.4ubuntu1).
gdb is already the newest version (8.1.1-0ubuntu1).

check if gcc is updated too

gcc -v

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.5.0-3ubuntu1~18.04' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)

### Compiling a C program
The phases of gcc Compiler converts a C program into an executable.
Expand Down Expand Up @@ -70,4 +91,4 @@ Sites
Books
- Introduction to Algorithms by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein( CLRS )
- MIT press - Introduction to Algorithms, Third Edition https://mitpress.mit.edu/books/introduction-algorithms-third-edition
- https://code.visualstudio.com/docs/cpp/config-linux
2 changes: 1 addition & 1 deletion binarytree_algorithms/bt_insert_delete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void inorder(node* temp){
}

// -----------------------

// Insert in level order
// iterative level order traversal in BT of the given tree using queue to find a node with a null child
node* insert_levelorder(node* root, int newdata){

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

// ISO morphic / idenical trees

struct node{
int data;
struct node* left;
Expand All @@ -20,6 +22,26 @@ bool isidentical(struct node * root1 , struct node * root2){
return isidentical(root1->left,root2->left) && isidentical(root1->right, root2->right);
}

// Mirrorrs the tree
void mirrorTree(struct node* root){
if( root ==NULL) return;
struct node* temp;
mirrorTree(root->left);
mirrorTree(root->right);
temp = root->left; // swap(node->left, node->right);
root->left = root->right;
root->right = temp;
}

void printTree(struct node* node) {
if (node == NULL) {
return;
}
printTree(node->left);
cout << node->data << " ";
printTree(node->right);
}

int main(){
struct node* root1 = new node(10);
root1->left = new node(30);
Expand All @@ -31,4 +53,9 @@ int main(){

cout << isidentical(root1,root2);

cout << "\n orgnal tree "<< endl;
printTree(root1);
mirrorTree(root1);
cout << "\n mirror of tree " <<endl;
printTree(root1);
}
68 changes: 66 additions & 2 deletions binarytree_algorithms/isperfect_bt.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,69 @@
// Is the binary tree perfect
// Is binary tree perfect
//A Binary tree is a Perfect Binary Tree in which all internal nodes have two children
// and all leaves are at the same level.

// NOT WORKING
#include <iostream>
#include<queue>
using namespace std;

struct node{
int data;
struct node* left;
struct node* right;
node(int value){
data = value;
left = NULL;
right = NULL;
}
};
void addEdge(struct node* root , int key){
if(root == NULL){
root = new node(key);
}else if(key > root->data){
addEdge(root->right, key);
}else{
addEdge(root->left, key);
}
}


// approach 1 :
// Iterative approach to check if a Binary Tree is Perfect
// Iterative approach to check if a Binary Tree is Perfect
bool isperfect(node* root){
queue<node*> q;
q.push(root);
int flag=0;

while (!q.empty()){
node * temp = q.front();
q.pop();

if(temp->left && temp->right){
if(flag==1){
return false;
}else{
q.push(root->left);
q.push(root->right);
}
}else if(!temp->right && !temp->left){
flag=1;
}else if(!temp->right || !temp->left){
return false;
}
}
return true;
}

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

struct node* root= new node(arr[0]);
for(int i=1;i<n;i++){
addEdge(root,arr[i]);
}

cout << isperfect(root);
}
Loading

0 comments on commit 8e14652

Please sign in to comment.