Jump to content

Tail Recursion (Binary Search Tree height() implementation)

- - - - -

  • Please log in to reply
1 reply to this topic

#1
CoryG89

CoryG89

    Newbie

  • Members
  • Pip
  • 2 posts
Ok, so I am in a Data Structures class that uses Java for language. We are currently studying different types of Binary Search Trees. But before our last test we went into detail about recursion and tail recursion. I missed the lecture that went into tail recursion and am trying to build an understanding of it on my own. I need to know two things essentially:

1. What is the difference between a recursive (non tail-recursive form) and tail recursive implementation (example if possible)?

2. When is it beneficial to use either a non-tail recursive implementation or a tail recursive implementation. Or is one always or efficient than the other?


Currently I am building up a BinarySearchTree class and I am currently implementing a height() function which uses recursion. I was wondering if either of these implementations use tail recursion. And either way would one of these implementations have better performance than the other?

First implementation:


	/**

	 * Recursively returns the height of a particular

	 * subtree.

	 *

	 * @param The subtree to calculate height of

	 * @return The height of the subtree

	 */

	public int height(BSTNode tree)

	{

		if (tree == null)

		{

			return 0;

		}

	

		int left = height(tree.getLeft());

		int right = height(tree.getRight());

	

		return 1 + Math.max(left, right);

	}

	

	/**

	 * @return The height of the entire tree

	 */

	public int height()

	{

		return height(root);

	}


Second implementation:


	/**

	 * Recursively returns the height of a particular

	 * subtree.

	 *

	 * @param The subtree to calculate height of

	 * @return The height of the subtree

	 */

	public int height(BSTNode tree)

	{

		if (tree == null)

		{

			return 0;

		}

	

		return 1 + Math.max(height(tree.getLeft(), tree.getRight());

	}

	

	/**

	 * @return The height of the entire tree

	 */

	public int height()

	{

		return height(root);

	}


#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
To answer the first part of your question, tail recursion is a way to perform recursive functions in constant stack space. Here are a couple of examples:
algorithm - What is tail-recursion? - Stack Overflow
algorithm - What Is Tail Call Optimization? - Stack Overflow
That should get you started.

I would say that neither one of your above implementations are tail recursive, since you have the "1 +" in the return line on both of them, forcing the computer to recursively call the function each time before it evaluates the statement.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users