Print binary search tree in increasing order

Given a binary search tree (aka an “ordered binary tree”), iterate over the nodes to print them out in increasing order. So the tree… 4 / \ 2 5 / \ 1 3 Produces the output “1 2 3 4 5”. This is known as an “inorder” traversal of the tree. Hint: For each node, the strategy is: recur left, print the node data, recur right. Here is code in c/cpp: [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]

Pre Order traversal - Recursive and Iterative solution

Example Consider the tree: div class="separator” style="clear: both; text-align: center;“> 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 Recursive solution preorder(N \*root) { if(root) { printf("Value : %d", root->value); preorder(root->left); preorder(root->right); } } Iterative solution [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]