P. 1
binary tree 2

binary tree 2

|Views: 66|Likes:
Published by Siu Chung Mok

More info:

Published by: Siu Chung Mok on Feb 10, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

07/04/2013

pdf

text

original

/* program to construct tree using inorder and preorder traversals */ #include<stdio.h> #include<stdlib.

h> /* A binary tree node has data, pointer to left child and a pointer to right child */ struct node { char data; struct node* left; struct node* right; }; /* Prototypes for utility functions */ int search(char arr[], int strt, int end, char value); struct node* newNode(char data); /* Recursive function to construct binary of size len from Inorder traversal in[] and Preorder traversal pre[]. Initial values of inStrt and inEnd should be 0 and len -1. The function doesn't do any error checking for cases where inorder and preorder do not form a tree */ struct node* buildTree(char in[], char pre[], int inStrt, int inEnd) { static int preIndex = 0; if(inStrt > inEnd) return NULL; /* Pick current node from Preorder traversal using preIndex and increment preIndex */ struct node *tNode = newNode(pre[preIndex++]); /* If this node has no children then return */ if(inStrt == inEnd) return tNode; /* Else find the index of this node in Inorder traversal */ int inIndex = search(in, inStrt, inEnd, tNode->data); /* Using index in Inorder traversal, construct left and right subtress */ tNode->left = buildTree(in, pre, inStrt, inIndex-1); tNode->right = buildTree(in, pre, inIndex+1, inEnd); return tNode;

/* now recur on right child */ printInorder(node->right). for(i = strt. } /* Driver program to test above functions */ int main() .} /* UTILITY FUNCTIONS */ /* Function to find index of value in arr[start. int strt. char value) { int i.. /* first recur on left child */ printInorder(node->left). */ struct node* newNode(char data) { struct node* node = (struct node*)malloc(sizeof(struct node)). return(node). node->right = NULL. } /* This funtcion is here just to test buildTree() */ void printInorder(struct node* node) { if (node == NULL) return. node->left = NULL. i <= end. node->data). } } /* Helper function that allocates a new node with the given data and NULL left and right pointers. i++) { if(arr[i] == value) return i. node->data = data.end] The function assumes that value is present in in[] */ int search(char arr[].. /* then print the data of node */ printf("%c ". int end.

} /* Recursive helper function -.1). len . and check to see if the sum is 0 when you run out of tree. 'B'.given a node. 'E'. one per line. printInorder(root). 'F'}. and an array containing . printPathsRecur(node. return true if there is a path from the root down to a leaf. 'C'}. 'C'. 'A'. path. 0. getchar(). print out all of its root-to-leaf paths. 'E'. struct node *root = buildTree(in. 'F'. } 7. printPaths() Solution (C/C++) /* Given a binary tree. Uses a recursive helper to do the work. subSum)). subSum) || hasPathSum(node->right. 0).node->data. hasPathSum() Solution (C/C++) /* Given a tree and a sum. pre.{ char in[] = {'D'. } } 8. 'D'. */ int hasPathSum(struct node* node. return(hasPathSum(node->left. int len = sizeof(in)/sizeof(in[0]). char pre[] = {'A'. int sum) { // return true if we run out of tree and sum==0 if (node == NULL) { return(sum == 0). /* Let us test the built tree by printing Insorder traversal */ printf("\n Inorder traversal of the constructed tree is \n"). such that adding up all the values along the path equals the given sum. } else { // otherwise check both subtrees int subSum = sum . */ void printPaths(struct node* node) { int path[1000]. 'B'. Strategy: subtract the node value from the sum when recurring down.

4 / \ 5 2 / \ 3 1 */ void mirror(struct node* node) { if (node==NULL) { return. 4 / \ 2 5 / \ 1 3 is changed to. printPathsRecur(node->right. print out all the root-leaf paths.. pathLen++. pathLen). i++) { printf("%d ". ints[i]). pathLen)..the path from the root node up to but not including this node. int path[]. So the tree. // append this node to the path array path[pathLen] = node->data.. // it's a leaf. for (i=0.. int pathLen) { if (node==NULL) return. path. */ void printPathsRecur(struct node* node. } else { // otherwise try both subtrees printPathsRecur(node->left. pathLen). mirror() Solution (C/C++) /* Change a tree so that the roles of the left and right pointers are swapped at every node. int len) { int i. so print the path that led to here if (node->left==NULL && node->right==NULL) { printArray(path. . path. } printf("\n"). i<len. } 9. } } // Utility that prints out an array on a line. void printArray(int ints[].

.. So the tree. // swap the pointers in this node temp = node->left.. doubleTree() Solution (C/C++) /* For each node in a binary search tree. mirror(node->right). The resulting tree should still be a binary search tree. // do the subtrees doubleTree(node->left). // duplicate this node to its left oldLeft = node->left. if (node==NULL) return. // do the subtrees mirror(node->left). node->left = node->right. create a new duplicate node. } } 10. node->left = newNode(node->data).. 2 / \ 1 3 Is changed to. node->left->left = oldLeft. and insert the duplicate as the left child of the original node. node->right = temp. } . 2 / \ 2 3 / / 1 3 / 1 */ void doubleTree(struct node* node) { struct node* oldLeft.} else { struct node* temp. doubleTree(node->right).

b->right) ). // 2. both empty -> true if (a==NULL && b==NULL) return(true). } . struct node* b) { // 1. } // 3. both non-empty -> compare them else if (a!=NULL && b!=NULL) { return( a->data == b->data && sameTree(a->left. */ int sameTree(struct node* a. return true if they are structurally identical. one not -> false else return(false).11. one empty. b->left) && sameTree(a->right. sameTree() Solution (C/C++) /* Given two trees.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->