zizi21
asked on
Inorder binary search tree
Hi,
I am trying to understand recursion for inorder traversal of : http://geeksquiz.com/binary-search-tree-set-2-delete/
// A utility function to do inorder traversal of BST
void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
}
}
Say the tree is the following...
/* Let us create following BST
50
/ \
30 70
/ \ / \
20 40 60 80 */
For inorder, we get 20,30,40,50, 60,70,80
Am i correct in understanding the recursion code is that, until you have not reached the most left, just keep on going left... is this right? any help to get me started is really appreciated.
if (root != NULL)
{
inorder(root->left);
I am trying to understand recursion for inorder traversal of : http://geeksquiz.com/binary-search-tree-set-2-delete/
// A utility function to do inorder traversal of BST
void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
}
}
Say the tree is the following...
/* Let us create following BST
50
/ \
30 70
/ \ / \
20 40 60 80 */
For inorder, we get 20,30,40,50, 60,70,80
Am i correct in understanding the recursion code is that, until you have not reached the most left, just keep on going left... is this right? any help to get me started is really appreciated.
if (root != NULL)
{
inorder(root->left);
ASKER
Thanks for your reply.
But, if both left and right are null, this does not get executed if (root != NULL)
sorry, if this sounds stupid but how does it get printed then..
But, if both left and right are null, this does not get executed if (root != NULL)
sorry, if this sounds stupid but how does it get printed then..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for replying.
Am I right, that big oh notation for this is O(n) ?
Am I right, that big oh notation for this is O(n) ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
the "output (printf function here)" is after the "inorder" function, so you need to proceed to get the leftmost node, then print that node (as there both left and right are null then, and the called function is not doing anything anymore)