#include
#include
#include "stack.h"
char* replace(char, char*);
void countNumberOfbits();
void constructHeap(int ar[]);
void fillPrimary(stack*,stack*);
void fillSecondry(stack*,stack*);
void traversal(Node * root);
int main() {
/*
char * ptr = (char*)malloc(6);
ptr[0] = 'a';
ptr[1] = 'b';
ptr[2] = 'c';
ptr[3] = 'c';
ptr[4] = 'e';
ptr[5] = '\0';
char * mainString = "itsasit is cb";
replace('c', ptr);
*/
int ar[10] = {0,1,2,3,4,5,6,7,8,9};
constructHeap(ar);
// countNumberOfbits(;
return 0;
}
void constructHeap(int ar[]) {
struct Node * root = NULL;
struct Node * ptr = NULL;
struct Node * mvptr = NULL;
stack * st_primary = new stack();
stack * st_secondry = new stack();
for (int i =0; i< 10; i++) {
ptr = new Node();
ptr->lfptr = NULL;
ptr->rptr = NULL;
ptr->data = ar[i];
if (st_primary->length() == 0) {
printf("\n -------tree is here to stay ---\n");fflush(stdout);
st_primary->push(ptr);
root = ptr;
} else {
mvptr = st_primary->pop();
if (mvptr->lfptr != NULL
&& mvptr->rptr != NULL) {
mvptr = st_primary->pop();
}
fillSecondry(st_primary,st_secondry);
if (mvptr->lfptr == NULL) {
mvptr->lfptr = ptr;
st_secondry->push(ptr);
fillPrimary(st_primary,st_secondry);
st_primary->push(mvptr);
} else if (mvptr->rptr == NULL) {
mvptr->rptr = ptr;
st_secondry->push(ptr);
fillPrimary (st_primary, st_secondry);
st_primary->push(mvptr);
}
}
}
traversal(root);
}
void fillPrimary(stack * st_primary,stack * st_secondry) {
int len = st_secondry->length();
for (int i = 0; i < len;i++) {
printf("\n -----fill primary --:%d:--\n",i);fflush(stdout);
st_primary->push(st_secondry->pop());
}
}
void fillSecondry(stack * st_primary, stack * st_secondry) {
int len = st_primary->length();
for (int i = 0; i < len;i++) {
st_secondry->push(st_primary->pop());
}
}
void traversal(Node * root) {
Node * mvptr = NULL;
stack* st_traverse = new stack();
st_traverse->push(root);
while (st_traverse->length() != 0) {
mvptr = st_traverse->pop();
if (mvptr->rptr) {
st_traverse->push(mvptr->rptr);
}
printf("\n ---Traversla :%d: --\n",mvptr->data);fflush(stdout);
while (mvptr->lfptr) {
mvptr = mvptr->lfptr;
printf("\n Traversal 1---:%d: ---\n",mvptr->data);fflush(stdout);
if (mvptr->rptr) {
st_traverse->push(mvptr->rptr);
}
}
}
}
/*void getNode(node * root) {
int level = 0;
if (root->lfptr == NULL) {
return root->leftPtr;
} else if (root->rptr == NULL) {
return root->rptr;
} else if (root->lfptr != NULL) {
level++;
pushlevel(level);
} else if (root->rptr != NULL) {
}
}
*/
void countNumberOfbits() {
char x = 23;
int count = 0;
printf("\n ---one is :%d: --\n",~(~0 << 1));
for (int i = 0; i < 7;i++) {
char y = (x >> i) & ~(~0 << 1);
printf("\n ---:%d:--x is \n",x);fflush(stdout);
if (y == 1) {
count++;
printf("\n ---x ==1 \n");fflush(stdout);
}
}
printf("\n Count is ---:%d: --\n",count);fflush(stdout);
}
char* replace(char ch, char * ptr) {
char * str = ptr;
printf("\n ---STR :%s:--\n",str);fflush(stdout);
char * ptr_f = str;
int j = 0;
for (int i=0;i < 6; i++) {
if (str[i] != ch) {
printf("\n ---i:%d:-j-:%d: \n",i, j);fflush(stdout);
str[j] = str[i];
j++;
}
}
str[j] = '\0';
printf("\n ---STR --:%s:\n",ptr);fflush(stdout);
return str;
}
No comments:
Post a Comment