0% found this document useful (0 votes)
39 views6 pages

Binary Trees: //defining A Binary Tree

The document discusses binary tree data structures and traversal algorithms. It defines a binary tree node struct with left, right, and data pointers. Functions are provided to create nodes, traverse trees in preorder, inorder and postorder, find the tree depth and print nodes at a given level. Additional functions find the maximum tree width, perform traversal using stacks, print the size and check if paths sum to a value. Mirroring a tree, determining tree equality, and level order traversal using a queue are also demonstrated.

Uploaded by

jaishu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views6 pages

Binary Trees: //defining A Binary Tree

The document discusses binary tree data structures and traversal algorithms. It defines a binary tree node struct with left, right, and data pointers. Functions are provided to create nodes, traverse trees in preorder, inorder and postorder, find the tree depth and print nodes at a given level. Additional functions find the maximum tree width, perform traversal using stacks, print the size and check if paths sum to a value. Mirroring a tree, determining tree equality, and level order traversal using a queue are also demonstrated.

Uploaded by

jaishu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

BINARY TREES

//Defining a binary tree


struct BinaryTree { int data; BinaryTree* left; BinaryTree* right; };

//Creating a new node


BinaryTree* newTreeNode(int data) { BinaryTree* newNode = new BinaryTree; newNode->data = data; newNode->left = NULL; newNode->right = NULL; return newNode; }

//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); }

1. Construct a binary tree, given pre-order and in-order tree traversals.

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; }

2. Find the depth of a binary tree


int depth(BinaryTree *root) { if(NULL == root) return 0; int left = depth(root->left); int right = depth(root->right); if(left > right) return (left + 1); else return (right + 1); }

3. Print the nodes of a tree in level order traversal


void printGivenLevel(BinaryTree *root, int level) { if(NULL == root) return; if(1 == level) cout << root->data << " "; else if (level > 1) { printGivenLevel(root->left, level-1); printGivenLevel(root->right, level-1); } }

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) {

Utility Functions For Stacks


Stack *S = createStack();; BinaryTree* node = root; while(node != NULL || !isEmpty(S)) { while(node != NULL) { struct Stack { int top; BinaryTree** stack; };

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; }

6. Print level order tree traversal using queue.


void printLevelOrderUsingQue(BinaryTree *root) { int rear, front; BinaryTree** que = createQue(&front, &rear); BinaryTree* temp_node = root; while(temp_node) { cout << temp_node->data << " "; if(temp_node->left) enQue(que, &rear, temp_node->left); if(temp_node->right) enQue(que, &rear, temp_node->right); temp_node = deQue(que, &front); } }

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))); }

7. Print size of a binary tree

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)); } }

9. Given a binary tree, construct its mirror


void mirror(BinaryTree *root) { if(root == NULL) return; BinaryTree *temp = NULL; mirror(root->left);

mirror(root->right); temp = root->right; root->right = root->left; root->left = temp; }

10. Write a function to determine if two trees are identical


bool sameTree(BinaryTree *root1, BinaryTree *root2) { if(NULL == root1 && NULL == root2) return true; else if(root1 != NULL && root2 != NULL) { return ((root1->data == root2->data) && sameTree(root1->left, root2->left) && sameTree(root1->right, root2->right)); } else return false; }

11.

You might also like