Binary Trees: //defining A Binary Tree
Binary Trees: //defining A Binary Tree
Binary Trees: //defining A Binary Tree
//Tree Traversals
void printPreOrder(BinaryTree *root) { if(NULL == root) return; cout << root->data << " "; printPreOrder(root->left); printPreOrder(root->right); } void printPostOrder(BinaryTree *root) { if(NULL == root) return; printPostOrder(root->left); printPostOrder(root->right); cout << root->data << " "; } void printInOrder(BinaryTree *root) { if(NULL == root) return; printInOrder(root->left); cout << root->data << " "; printInOrder(root->right); }
int getIndex(int* arr, int val, int size) { for(int i=0; i<size;i++) { if(arr[i] == val) return i; } return -1; }
BinaryTree* create_bintree(int* parray, int* iarray, int size) { if(size == 0) return NULL; int rootVal = parray[0]; BinaryTree* root = newTreeNode(rootVal); int newIdx = getIndex(iarray, rootVal, size); root->left = create_bintree(parray+1, iarray, newIdx); root->right = create_bintree(parray+newIdx+1, iarray+newIdx+1, sizenewIdx-1); return root; }
void printLevelOrder(BinaryTree *root) void printLevelOrder(BinaryTree *root, int level) { for(int i = 0; i <= depth(root); i++) { printGivenLevel(root, i); } }
4. Print maximum width of a tree. Also print the nodes of the level with the maximum width.
int getWidth(BinaryTree *root, int level) { if(NULL == root) return 0; if(1 == level) return 1; else if (level > 1) return getWidth(root->left, level-1) + getWidth(root->right, level-1); } int treeWidth(BinaryTree *root) { int width = 0; int level = 0; for(int i = 0; i <= depth(root); i++) { if(width < getWidth(root, i)) { width = getWidth(root, i); level = i; } } cout << "Level " << level << "nodes: " ; printGivenLevel(root, level); return width; }
5. Write functions to print pre-order, post-order and in-order tree traversal of nodes, using stack (without recursion)
//In-Order Tree Traversal Using Stack void InOrderWithStack(BinaryTree *root) {
push(S, node); node = node->left; } if(!isEmpty(S)) node = pop(S); cout << node->data << " "; node = node->right; } } //In-Order Tree Traversal Using Stack void PreOrderWithStack(BinaryTree *root) { Stack *S = createStack(); BinaryTree *node = root;
Stack* createStack() { Stack *S = (Stack*)malloc(sizeof(Stack *)); S->stack = (BinaryTree **)malloc(sizeof(BinaryTree *)*STACKSIZE); S->top = -1; return S; } ____________________________ void push(Stack *S, BinaryTree *node) { S->top++; S->stack[S->top] = node;
while(node!=NULL || !isEmpty(S)) { while(node != NULL) } { if(node->right) push(S, node->right); ____________________________ cout << node->data << " "; BinaryTree* pop(Stack *S) node = node->left; { } S->top--; if(!isEmpty(S)) return S->stack[S->top node = pop(S); + 1]; } } ____________________________ } //In-Order Tree Traversal Using Stack void PostOrderWithStack(BinaryTree *root) { Stack *S = createStack(); BinaryTree *node = root; while(node != NULL) { push(S, node); if(node->right) push(S, node->right); if(node->left) node = node->left; } while(!isEmpty(S)) { node = pop(S); cout << node->data << " "; } } bool isEmpty(Stack *S) { if(-1 == S->top) return true; else return false; }
struct BinaryTree** createQue(int *front, int *rear) { struct BinaryTree** que = (struct BinaryTree **)malloc(sizeof(struct BinaryTree*)*QUESIZE); *front = *rear = 0; return que; } ____________________________ void enQue(BinaryTree **que, int *rear, BinaryTree *node) { que[*rear] = node; (*rear)++; } ____________________________
BinaryTree *deQue(BinaryTree **que, int *front) int size(BinaryTree *root) { { (*front)++; if(root == NULL) return 0; return que[*front 1]; else } return (1 + (size(root->left)) + (size(root->right))); }
8. Write a function that returns true if any if the paths from root ->leaf equals a given value.
bool pathSum(BinaryTree *root, int sum) { if(NULL == root) return(sum == 0); else { return (pathSum(root->left, sum - root->data) || pathSum(root>right, sum - root->data)); } }
11.