1) Can someone give me a recursive algorithm that searches a tree(NOT BINARY) an
ID: 3547768 • Letter: 1
Question
1) Can someone give me a recursive algorithm that searches a tree(NOT BINARY) and determines if it contains an element(int). You are given the root node and an int value to search for, you must return null if its not found, and if it is return a reference to the node of the value.
2) Now we are writing a recursive method for a binary tree and we must indiciate whether and element(int) is in the tree. Return the reference if found and null if it is not. THE METHOD MUST HAVE TIME COMPLEXITY OF O(h) where h is the height of the tree.
Explanation / Answer
answer 1:
Node search(int value, node n)
{
if(n->value==value)
{
return n;
}
else if(sizeof(n->children))
{
for(int i=0;i<sizeof(n->children);i++)
{
search(int value,n->children[i]);
}
else
return null;
}
answer 2: java code
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.