Given a binary tree, print out the tree in level order (ie, from left to right, level by level). Output a newline after the end of each level. Breadth First Search (BFS) is not allowed. We have already seen how to do level order traversal here.
Example
So consider the tree:
1
/ \
2 3
/ \ / \
4 5 6 7
The BFS or level order traversal here is :
[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 full binary tree from a pre-order list of marked nodes
Question: construct a full binary tree from a list of nodes in pre-order. The nodes are marked ‘i’ if they are inner nodes and are marked ‘l’ if they are leaf nodes.
Example given a list of nodes marked as (i, i, i, l, l, i, l, l, i, l, l), we need to construct a tree from those nodes so that the result looks like this:
Notice how all “l” nodes are leaf nodes and “i” nodes are inner nodes?
[Read More]
What does flattening list of items into tree means
If I had a list of lists, “flatten” would be the operation that returns a list of all the leaf elements in order, i.e., something that changes:
[[a, b, c], [d, e, f], [g, h i]]
Into
[a, b, c, d, e, f, g, h, i]
For trees, flatting is generating a list of all leaves in natural traversal order (NB: since only leaves are in the result, it doesn’t matter whether you think of this as pre-, in- or post-order traversal.
[Read More]
Iterative or Non recursive Inorder traversal on tree
This process when implemented iteratively also requires a stack and a boolean to prevent the execution from traversing any portion of a tree twice. The general process of in-order traversal follows the following steps:
Create an empty stack S. Initialize current node as root Push the current node to S and set current = current->left until current is NULL If current is NULL and stack is not empty then
[Read More]
DFS (Depth First Search) on tree or graph
Consider this tree (which is a graph without a cycle) :
Now DFS means traversing to the depth…and then going up, so here DFS traversal will result in 5 2 -4 3 21 19 25. We can use non recursive as well recursive approach to solve this problem.
A depth first search (DFS) through a tree starts at the root and goes straight down a single branch until a leaf is reached.
[Read More]
BFS (Breadth first search ) OR Level Order Traversal on tree
Algorithm
Starting at some arbitrarily chosen vertex s (s stands for start vertex) , we mark v so that we know we’ve visited it, process v, and then visit i.e. mark the vertex as visited and process all of v’s neighbors. Now that we’ve visited and processed all of v’s neighbors, we need to visit and process all of v’s neighbors neighbors Example
So consider the tree:
[Read More]
Given a binary tree, print the sum( inclusive of the root ) of sub-tree rooted at each node in in-order
Given a binary tree. Find the sum( inclusive of the root ) of sub-tree rooted at each node. Print the sum in in-order.
E.g.
3
/ \\
2 4
/ / \\
2 2 -1
Output: 2, 4, 12, 2, 5, -1.
Solution
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode
{
struct TreeNode \*left, \*right;
int data;
}TreeNode;
typedef TreeNode \* Tree;
/\*
Two passes with constant space
\*/
int printSum(Tree t, int toPrint)
{
if(t == NULL)
return 0;
int lsum = printSum(t->left, toPrint);
int rsum = printSum(t->right, 0);
int sum = lsum + t->data + rsum;
if(toPrint)
printf("%d ", sum);
printSum(t->right, toPrint);
return sum;
}
/\*
One pass with n space
\*/
int printSum2(Tree t, int a\[\], int \*pos)
{
if(t == NULL)
return 0;
int lsum = printSum2(t->left, a, pos);
(\*pos)++;
int p = \*pos;
int rsum = printSum2(t->right,a, pos);
return a\[p\] = lsum + t->data + rsum;
}
/\*Utilities\*/
inline TreeNode \* makeTreeNode(int data)
{
TreeNode \*n = calloc(sizeof(TreeNode), 1);
n->data = data;
return n;
}
int main()
{
/\*level 0\*/
Tree t = makeTreeNode(3);
/\*level 1\*/
t->left = makeTreeNode(2);
t->right = makeTreeNode(4);
/\*level 2\*/
t->left->left = makeTreeNode(2);
t->right->left = makeTreeNode(2);
t->right->right = makeTreeNode(-1);
/\*print sum\*/
printSum(t, 1);
printf("\\n");
int a\[100\];
int pos = -1;
printSum2(t, a, &pos);
int i;
for(i = 0; i <= pos; ++i) {
printf("%d ", a\[i\]);
}
printf("\\n");
return 0;
}
Nth node in inorder traversal
Problem
Here we have to return pointer to nth node in the inorder traversal
Solution
// Get the pointer to the nth inorder node in "nthnode" void nthinorder(node \*root, int n, mynode \*\*nthnode) { static whichnode; static found; if(!found) { if(root) { nthinorder(root->left, n , nthnode); if(++whichnode == n) { printf("\\nFound %dth node\\n", n); found = 1; \*nthnode = root; // Store the pointer to the nth node. } nthinorder(root->right, n , nthnode); } } } 17\.
[Read More]
Print binary search tree in increasing order
Very good information indeed. Thanks for posting t…
CPP Tutorials - Jan 1, 2015
Very good information indeed. Thanks for posting this here. You can find more information on Binary search tree (BST) here in the following link with examples.