Given the post order array find if tree is BST

Problem An array which is a Post order traversal of a Binary Tree. Write a function to check if the Binary Tree formed from the array is a Binary Search Tree. Eg: 2 1 3 The array given as input would be 1 3 2. Write a function to say if the tree formed is a Binary Search Tree. Example 2: 4 is root. 0 is left child of 1 , 1 is left child of 2 and 2 is left child of 4. [Read More]

Binary Tree Post-Order Traversal - Recursive and Iterative Solution

Consider the tree: To traverse a binary tree in Postorder, following operations are carried-out (i) Traverse all the left external nodes starting with the left most subtree which is then followed by bubble-up all the internal nodes, (ii) Traverse the right subtree starting at the left external node which is then followed by bubble-up all the internal nodes, and (iii) Visit the root. Therefore, the Postorder traversal of the above tree will outputs: [Read More]

Binary tree traversal: Preorder, Inorder, Post-order, BFS, DFS, Level Order, zig zag order

In order to illustrate the 3 traversals - pre order, in-order and post-order lets take following tree: Preorder traversal: To traverse a binary tree in Preorder, following operations are carried-out (i) Visit the root, (ii) Traverse the left subtree, and (iii) Traverse the right subtree. Therefore, the Preorder traversal of the above tree will outputs: 7, 1, 0, 3, 2, 5, 4, 6, 9, 8, 10 Implementing pre-order traversal [Read More]

Construct a tree given its inorder and preorder traversal strings. Similarly construct a tree given its inorder and post order traversal strings.

For Inorder And Preorder traversals inorder = g d h b e i a f j c preorder = a b d g h e i c f j Scan the preorder left to right using the inorder sequence to separate left and right subtrees. For example, “a” is the root of the tree; “gdhbei” are in the left subtree; “fjc” are in the right subtree. “b” is the next root; “gdh” are in the left subtree; “ei” are in the right subtree. [Read More]

Implementing BST in C ( header file )

This post contains header file which contains binary search tree related function. The tree contains following functions: Search Create new node Get the size of tree Insert Delete Print inorder, pre order and post order Starting the header file: Lets name our header file as Tree1.h. #ifndef TREE1\_H #define TREE1\_H #include <stdio.h> #include <malloc.h> enum BOOL{false,true}; struct node { int data; struct node\* left; struct node\* right; } ; typedef struct node node; typedef enum BOOL BOOL; Defining some functions via c macros: [Read More]