/** Function to print a tree in the format: post order
 *	@param which_tree Pointer to the tree to be printed
 */
void printPostOrder(Tnode* which_tree) {
	if (which_tree != NULL) {
		printPostOrder(which_tree->left); //print left child
		printPostOrder(which_tree->right); //print right child
		printf("%s\n", which_tree->data); //print data
	}
}
示例#2
0
void printPostOrder(struct node* root)
{
	if(root==NULL)
		return;
	printPostOrder(root->left);		
	printPostOrder(root->right);
	printf(" %d ",root->data);
}
void printPostOrder(struct TreeNode *T)
{
    if (T != NULL) {
        printPostOrder(T->lchild);
        printPostOrder(T->rchild);
        printf("%d ", T->value);
    }
}
// Private printPostOrder function.  It prints the value of the tree in PostOrder.
void BinaryTree::printPostOrder(TreeNode *p) const
{
	if(p)
	{
		printPostOrder(p->left);
		printPostOrder(p->right);
		cout << p->value << endl;
	}
}
/*Print the nodes of a binary search tree according to a postorder bottom-up traversal.
    4
   / \
  2   5
 / \
1   3 The output of the function should be 1,3,2,5,4.
   
 */
void printPostOrder(struct node* n) {

if(n->left != NULL) {
  printPostOrder(n->left);
}

if(n->right != NULL) {
  printPostOrder(n->right);
}
printf("%d ",n->data);

}
示例#6
0
/*
  Xavier Thomas
  Section U01
  I affirm that this program is entirely my own work and none of it is the work of any other person.
*/
int main(int argc, char *argv[]){
    printf("Welcome To assignment 1\n");

    int caseSensitive = 0;
    char *outputFile = NULL;
    char *inputFile = NULL;
    parseCommandLineOptions(argc,argv,&caseSensitive,&outputFile,&inputFile);

    // printf("Case '%d' , outputFile '%s' inputfile '%s'\n",caseSensitive,outputFile,inputFile);

    int readType = determineRead(inputFile);

    // printf("Read Type '%d'\n", readType);

    if(readType){
        readFile(inputFile,caseSensitive);
    }else{
        readFromInput(caseSensitive);
    }

    determineOutput(outputFile);

    printPostOrder(&root);
    printf("End Of Assingment\n");
    return 0;
}
/**	Function to print a tree in different formats: in order, pre order, post order
 *	@param which_tree Pointer to the tree to be pointed
 */
void print_tree(Tnode *which_tree) {
	printf("Print in order\n");
	printInOrder(which_tree); //print in order
	printf("Print pre order\n");
	printPreOrder(which_tree); //print pre order
	printf("Print post order\n");
	printPostOrder(which_tree); //print post order
}
/*Test function for printPostOrder()*/
void testPrintPostOrder() {
struct node* rootNode = insert(NULL,4);
rootNode = insert(rootNode,2);
rootNode = insert(rootNode,5);
rootNode = insert(rootNode,3);
rootNode = insert(rootNode,1);
printPostOrder(rootNode);
}
示例#9
0
int main()
{
  TreeNode *root=NULL;
  createBinaryTree(&root);
  printf("\n\n");
  printPreOrder(root);
  printf("\n");
  printInOrder(root);
  printf("\n");
  printPostOrder(root);
  printf("\n");
  return 0;
}
示例#10
0
int main()
{
  int internalNodes ;
  TreeNode *root=NULL;
  createBinaryTree(&root);
  printf("\n\n");
  printPreOrder(root);
  printf("\n");
  printInOrder(root);
  printf("\n");
  printPostOrder(root);
  printf("\n");
  internalNodes = countInternalNodes(root);
  printf("Internal nodes =%d\n",internalNodes);
  return 0;
}
示例#11
0
int main()
{
	/*
	 * Create a pointer that will be the root of the tree.
	 */
	bTreeNode *rootPtr = NULL;
	/*
	 * Insert a bunch of numbers for testing purposes.
	 * The tree constructed should look like:
	 *
	 *           5
	 *         /   \
	 *		  3	    7
	 *       / \   / \
	 *      1   4 6   9
	 */
	rootPtr = insert(rootPtr, 5);
	rootPtr = insert(rootPtr, 3);
	rootPtr = insert(rootPtr, 7);
	rootPtr = insert(rootPtr, 1);
	rootPtr = insert(rootPtr, 4);
	rootPtr = insert(rootPtr, 6);
	rootPtr = insert(rootPtr, 9);

	/*
	 * Traversing the tree in Preorder
	 */
	printf("\nPreOrder\n");
	printPreOrder(rootPtr);

	/*
	* Traversing the tree in Inorder
	*/
	printf("\nInOrder\n");
	printInOrder(rootPtr);

	/*
	* Traversing the tree in Postorder
	*/
	printf("\nPostOrder\n");
	printPostOrder(rootPtr);

	return 0;
}
示例#12
0
/* Driver program to test above functions*/
int main()
{
     struct node *root  = newNode(1);
     root->left             = newNode(2);
     root->right           = newNode(3);
     root->left->left     = newNode(4);
     root->left->right   = newNode(5); 
 
     printf("\n Preorder traversal of binary tree is \n");
     printPreorder(root);
 
     printf("\n Inorder traversal of binary tree is \n");
     printInorder(root);  
 
     printf("\n Postorder traversal of binary tree is \n");
     printPostOrder(root);
 
     getchar();
     return 0;
}
int main(int argc, char**argv) {
    int n, i;
    int array[110];
    while (scanf("%d", &n) != EOF) {
        for (i = 0; i < n; i++) {
            scanf("%d", &array[i]);
        }
        struct TreeNode *T = NULL;
        for (i = 0; i < n; i++) {
            T = insertTreeNode(T, array[i]);
        }
        printPreOrder(T);
        printf("\n");
        printMidOrder(T);
        printf("\n");
        printPostOrder(T);
        printf("\n");
    }
    
    return 0;
}
示例#14
0
int main()
{

#if 0
        int a[] = {2, 4, 1, 8, 3, 5, 9, 6, 7};
        tree_t root = NULL;

        for(int i = 0; i < 9; ++i)
        {
                root = insert(root,a[i]);
        }

        display(root);

        printf("----------------------\n");
#endif
#if 0
        printf("In-order traversal \n");
        display(root);

        printf("No of nodes: %d\n", count(root));

        printf("----------------------\n");
        printf("No of leaf nodes: %d\n",count_leaf(root));

        printf("----------------------\n");
        printf("Pre-order traversal\n");
        post_traversal(root);
        printf("----------------------\n");
        printf("Post-order traversal\n");
        pre_traversal(root);
        printf("----------------------\n");

        printf("Height of the tree is: %d\n", height(root));
        printf("Least value in tree is: %d\n", least_of_tree(root));
        printf("Max value of tree is: %d\n", max_of_tree(root));

        int height_ = height(root);
        int vertical_sum[10000];
        for(int i = 0; i < 10000; ++i)
        {
                vertical_sum[i] = 0;
        }
        /*
                I assumed Vertical sum for root is 1... :P if there are more no of nodes on the
                left subtree of the root..they we cant implement this using arrays in c..as negative indices is not possible
        */

        verticalSumArray(root,vertical_sum,1);
        for(int i = 0;vertical_sum[i] != 0 ; ++i)
        {
                printf("%d : %d \n",i,vertical_sum[i]);
        }

        // tree_t mirrored_root = mirror_tree(root);
        printf("----------------------\n");
        // display(mirrored_root);
        //printf("%d\n", RLMirrors(root));
        //inorder(root);

        tree_t largest = largestSumSubtree(root);
        printf("%d\n",largest -> key);

        int key = 1;
        if(search(root,key) != NULL)
        {
                printf("%d is present\n",key);
        }
        else
        {
                printf("%d is absent\n",key);
        }

        iterativeInorder(root);

        printf("----------------------\n");

        iterativePreorder(root);


        printf("----------------------\n");
        iterativePostorder(root);

        levelorder(root);

        int height = 0;
        printf("the diameter of the tree: %d\n",diameter(root));

        int key = 9;
        printAncestorsI(root,key);

        int k = 3;
        printKdistance(root,k);

        int sum = 36;
        if(hasSumPath(root,sum) == 1)
        {
                printf("Yes\n");
        }
        else
        {
                printf("No\n");
        }

        root  = getNode(10);
        root -> llink = getNode(8);
        root -> rlink = getNode(2);
        root -> llink -> llink = getNode(3);
        root -> llink -> rlink = getNode(5);
        root -> rlink ->rlink = getNode(2);

        display(root);
        printf("----------------------\n");
        if(isSum(root))
        {
                printf("Yes\n");
        }
        else
        {
                printf("No\n");
        }

        root  = getNode(20);
        root -> llink = getNode(8);
        root -> rlink = getNode(22);
        root -> llink -> llink = getNode(4);
        root -> llink -> rlink = getNode(12);
        root -> llink -> rlink -> rlink = getNode(14);
        root -> llink -> rlink -> llink = getNode(10);

        int n1 = 4;
        int n2 = 14;
        printf("Least common ancestor of %d & %d is %d \n", n1,n2,lowestCommonAncestor(root,n1,n2));
        printf("\n");
#endif
        // printPaths(root);
#if 0
        int num = 4;
        printf("The no of bst for %d keys is %d\n", num,countTrees(num));i
#endif
#if 0
        root = getNode(20);
        root -> llink = getNode(8);
        root -> rlink = getNode(22);
        root -> llink -> llink = getNode(4);
        root -> llink -> rlink = getNode(12);
        root -> llink -> rlink -> llink = getNode(10);
        root -> llink -> rlink -> rlink = getNode(14);

        tree_t successor = inorderSuccessor(root,root -> llink);
        if(successor != NULL)
        {
                printf("thei inorder successor is %d\n",successor -> key);
        }
#endif
#if 0
        root = getNode(20);
        root -> llink = getNode(8);
        root -> rlink = getNode(22);
        root -> llink -> llink = getNode(100);
        if(checkAllLeavesSameLevel(root) == 1)
        {
                printf("All the leaves are in the same level\n");
        }
#endif
#if 0
        display(root);
        printf("-------------------------------------\n");
        replaceNodeWithSum(root,0);
        display(root);
int getSum(tree_t root)
{
        if(root == NULL)
        {
                return 0;
        }
        else
        {
                return root -> key + getSum(root -> rlink);
        }
}
#endif
        // printf("%d\n",countTrees(3));
#if 0
        root = getNode(10);
        root->llink = getNode(20);
        root->rlink = getNode(30);



        if(isBalanced(root) == 1)
        {
                printf("The tree is balanced\n");
        }
        else
        {
                printf("Ther tree is unbalanced\n");
        }
#endif
#if 0
        tree_t root = NULL;
        int array[] = {1,2,3,4,5,6,7,8,9};
        int n = 9;
        root = createMinimalBST(root,array,n);
        display(root);
#endif
//        tree_t root = NULL;
#if 0
        root = getNode(20);
        root -> llink = getNode(8);
        root -> rlink = getNode(22);
        root -> llink -> llink = getNode(4);
        root -> llink -> rlink = getNode(12);
        root -> llink -> rlink -> llink = getNode(10);
        root -> llink -> rlink -> rlink = getNode(14);

        // printPathWithSum(root,40);

        // levelorderRecursive(root);
        // rightView(root);
#endif
#if 0
        int inorder[] = {4, 2, 5, 1, 6, 3};
        int preorder[] = {1, 2, 4, 5, 3, 6};

        int n = 6;
        // int preIndex = 0;
        // root = buildTreeWithInorderAndPreOrder(inorder,preorder,0,n - 1,&preIndex);
        // display(root);

        int index = 0;
        printPostOrder(inorder,preorder,0,n - 1,&index);
#endif
#if 0
        /*
                        10
                    -2      7
                 8      -4

        */
        tree_t root = NULL;
        root = getNode(10);
        root->llink = getNode(-2);
        root->rlink = getNode(7);
        root->llink->llink = getNode(8);
        root->llink->rlink = getNode(-4);
 //       printf("Sum: %d\n",maxSumPath(root));

        pre_traversal(root);
        printf("----------------------------------");
        display(root);
#endif

        return 0;
}
// Public printPostOrder function
void BinaryTree::printPostOrder() const
{
	printPostOrder(root); 
}