Recursively Traverse Binary Tree - binary-tree

This is probably a simple task for expert coders, but is it possible to recursively traverse a binary unordered tree to find a node?
I can do this for a binary search tree, but I'm struggling with how to do this when the tree is unoredered since I can't figure out how to traverse back up when a node is not found in a branch...
C++ would he helpful.
Thanks guys.

use iteration. pseudo code below:
ITERATIVE-TREE-SEARCH(x, k)
while x ≠ NIL and k ≠ key[x]
do if k < key[x]
then x ← left[x]
else x ← right[x]
return x

Related

Algorithm to construct a binary search tree out of elements from another bst

I'm trying to come up with an algorithm to construct a binary search tree using the elements from another binary search tree, but with the restriction that those elements have to be greater or equal than some given integer, let's call it x.
I thought of a recursive approach (using in order traversal):
binary_tree (bst tree, int x) {
if (tree is empty)
return empty;
if (tree->element>=x)
insert tree->element in a new BST;
else ????
}
I have no idea what the last recursive call would be, I obviously can't write two returns like this:
else
return (tree->left, x)
return (tree->right, x)
And I can't think of anything else, sorry if this is a silly question! I'm just starting with recursion and it's really confusing.
Lets think about what we are doing here. We want to construct a tree from an existing binary search tree. Because the existing tree is a BST we get some helpful info.
For any node V, if V <= x then the subtree pointed to by V -> left will have nodes all smaller than x. So we no longer need to look in the left subtree anymore. However if we hit a node that is greater than or equal to x we need to continue the recursion. Lets bring this all together in pseudo code
newBST(root):
if root is null
return
if root.val >= x
addNewNode(root.val)
newBST(root.right)
newBST(root.left)
else:
newBST(root.right)
It's a little tricky to do this recursively, because there isn't a 1-1 correspondence between subtrees in the tree you have and subtrees in the tree you want.
The simplest way to do this is to copy the values >= x into a list in order, and then build a tree from the list recursively.

How to give a recursive algorithm

Give a recursive algorithm btProd which takes as input a binary tree, and outputs
the value that is the product of the numbers contained in the binary tree. If the input is the null tree, then the algorithm should return null.
Algorithm btProd(P)
Require: Input is a tree P
1: btProd(null) ← 0
2: btProd(leaf x) ← x
3: btProd(node L x R) ← btProd(L) + x + btProd(R )
That's the way i would do it but i'm not sure if that's correct
As mentioned in the comments, the product is commutative. Thus, you can traverse the tree in any order you like (pre-,in-,post-order). The recursion you motivated as pseudo-code seems correct, assuming that when you write + x + you mean btProd(L) times btProd(R).

Binary Tree Find number nearest and greater than the key

Say I have a balanced binary tree. I wish to search for a key k in the tree. However, if k doesn't exist in the binary tree, it should give me the next greatest number nearest to k.
For examples suppose I have these numbers [1,5,6,8,10] as keys in the tree. If I search for '7' it should return 8 and if I search for 2 it should return 5 etc.
What would have to be the modifications in the binary tree to be able to perform such a search? I want an O(log n) solution as well please.
Assuming you mean "binary search tree" rather than "binary tree", you don't need any modifications to find the minimum element y in the tree such that y >= x.
search(n, x, best_so_far) ::=
if n == nil { return best_so_far }
if n.value == x { return x }
if n.value > x { return search(n.left, x, min(best_so_far, n.value) }
if n.value < x { return search(n.right, x, best_so_far) }
You would call this function as search(root, x, +infinity).
The idea is that if you're exploring the left branch at a node n, you don't need to consider anything to the right of n: n.value is larger than x, and everything to the right is larger than n.value. Similarly, if you're exploring the right branch of a node n, then you can discard everything to the left of n: n.value is smaller than x and everything to the left of n is smaller than n.value.
The code's runtime is bounded by the height of the tree, so is O(log n) if the tree is balanced.

Binary Search Tree - Searching for a Scope

If I have a binary search tree S with the number pair (a,b) where (a<=b); is there an algorithm that would help me find the elements in S with the key values that are within the range of a,b inclusive ([a,b]).
The runtime restriction is O(h+k), h is the tree height of S, and k is the number of elements within the range.
The classic answer is from "Introduction to Algorithms":
http://staff.ustc.edu.cn/~csli/graduate/algorithms/book6/chap14.htm
Step 1: find a, using the normal binary tree lookup.
Step 2: call tree successor iteratively until you find b. Tree successor gives you the next item in the tree:
TREE-SUCCESSOR(x)
if right[x] ≠ NIL
then return TREE-MINIMUM (right[x])
y ← p[x]
while y ≠ NIL and x = right[y]
do x ← y
y ← p[y]
return y
TREE-MINIMUM (x)
while left[x] ≠ NIL
do x ← left[x]
return x

Binary Tree from inorder and postorder

I have just started studying Binary Tree. Is there an algorithm to find out the binary tree structure,given the Inorder and Postorder OR Inorder and Preorder? I have been trying to do it manually,but it never comes out correct.For eg.-These two are valid Inorder and Postorder traversal of a given tree:
Inorder: D B F E A G C L J H K
Postorder : D F E B G L J K H C A
Clearly A is the root as it is the last element in Postorder. Now looking in Inorder,the left subtree becomes: {D B F E} and right subtree becomes: {G C L J H K}. The root of right subtree would be the second last element in preorder i.e C. I can now further divide the right subtree(with C as root), giving {G} as right subtree and {L J H K} as left. Therefore I have this structure:
A
\
C
/
G
But,whatever algorithm I apply,next seems to work differently for different trees . Someone please explain.
If I understand what your asking, your trying to reverse engineer the underlying structure for a given binary tree search algorithm given the raw data in it's pre and post state. If this is the case you could be down a difficult road since although the basic algorithm is the same, there could be nuances to it depending on the developer that build the algorithm since in practice it is often the case the developers do not build a pure implementation of these algorithms.
If your just trying to get a better understanding of binary trees, this may explain it a little better: http://www.youtube.com/watch?v=ZkH3SSPwcwI
Let the inorder and preorder traversals be given in the arrays iorder and porder respectively.
The function to build the tree will be denoted by buildTree(i,j,k) where i,j refer to the range of the inorder array to be looked at and k is the position in the preorder array.
Initial call will be buildTree(0,n-1,0)
The algorithm has the following steps:
Traverse porder from start. The first node is the root, then we have the left subtree and then the right subtree. Create a node with this as the element.
Search the node in the iorder array. Suppose its found at x. Decrement k. k refers to the position in the porder array we are currently at. k has to be passed by reference.
Finally populate the left child and right child with the return value of the recursive calls
left child = buildTree(i,x-1,k)
right child = buildTree(x+1,j,k)
In the end return the node
PS: Got the code accepted with the above algorithm at
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=477

Resources