Binary Search Tree in Java?

You can constrain your type to implement ComparableCompareTo(root).

You can constrain your type to implement Comparable: public class BinarySearchTree Then you can call compareTo: // Instead of if (element when the search tree is constructed, and then use that to compare elements. That's actually a more flexible solution in my view - it means you could create different binary search trees for the same element type, but ordering them in different ways.

Try drawing various trees on a paper and see what you get. Remember that a binary tree is defined as a tree where each node may have 0 (in which case it is a leaf), 1 or 2 children. For your question you should examine the very unbalanced case of 1 child per node.

Consider: If you're trying to maximize the number of leaves, you want as few internal nodes as possible (and the reverse if you're trying to minimize the number of leaves). How can you accomplish that? To get a tree of maximal height, you'll put as few nodes in each level as possible.

How can you do that? Conversely, for the minimum height, what is the maximum number of nodes you can put at each level? How many ways are there to get to each node of a tree?

Thus, how many pointers do you need?

The maximum number of leaves is ceil(n / 2). The minimum number is 1 The maximum height is n. The minimum is floor(log_2(n)).

I'm assuming you're either coding in C or C++. A. A node, if the structure is defined like this: struct node { struct node *left, *right; }; You can observe that the structure can either have 0, 1, or 2 leaves.So, the max is 2, min is 0 leaves.

B. Minimal height is zero, in which would only contain the root node. Note that the root node does not count as a height of 1.

It's also called depth at times. Here is an algorithm for the height: int height(struct node *tree) { if (tree == NULL) return 0; return 1 + max (height (tree->left), height (tree->right)); } Read more: wiki.answers.com/Q/How_do_you_find_out_t... c. Pardon me if I take this the worng way, but I'm assuming if we mapped this out on a piece of paper, we'd be trying to find the number of "links" that we would use?

In that case, it'd simply be the number of nodes in the tree -1 for root node. This algorithm found on this page forums.techarena.in/software-development... can help you: check if root is null, then pass the left and right nodes as parameters into the function.Int countnodes(Node* root) { if (root == null || kInt totalnodes = countnodes(root) - 1; d. The time complexity for best case is O(nlogn) where n is the number of nodes to insert.

The worst case, is O(n). It is directly linear. If you have any other questions just google it, there's plenty of things to know about binary search trees.

But most of it is simply recursion that you can learn in 30 seconds. I hope this helps. Good luck on your exam!

I had mine a few months ago. ;).

925, 202, 911, 240, 912, 245, 363 Doesn't make sense From 911, you're taking the smaller branch to 240. You then somehow arrive at 912. This should be impossible If the left child of any node is smaller than its parent, then ALL elements in the left subtree should be smaller than their parent.912 > 911, therefore it's in the wrong subtree.

Doesn't make sense From 911, you're taking the smaller branch to 240. You then somehow arrive at 912. This should be impossible.

If the left child of any node is smaller than its parent, then ALL elements in the left subtree should be smaller than their parent. 912 > 911, therefore it's in the wrong subtree.

Aha,I get it the left sub tree should be less than the right sub tree thanks for your answer – user355002 Jun 26 '10 at 16:21 @matin1234 What I was trying to say is that the entire left sub tree should be less than the parent, not just less than the right sub tree – Jamie Wong Jun 26 '10 at 16:25 aha now I get the whole thanks – user355002 Jun 26 '10 at 16:45.

Nt: When searching in a sorted BST, the upper and lower bounds should only get tighter.

en.wikipedia.org/wiki/Recursion For recursion to work you need a base case, and a set of cases and actions that reduce towards the base case. For counting the number of nodes in a binary trees, we can use the base case of "a node does not exists", and other case of "a node does exist". For the other case, (node does exist) we will add 1 to the count of nodes add the number of nodes in each of the tree's subtrees (left and right) to the total count.

How do we find the number of nodes in the subtrees? Simply apply the same set of conditions that we used for the first node to the child nodes. By repeatedly breaking down the tree's subnodes we can obtain the total number of nodes in the tree Take for example: N1 /\ N2 N3 /\ \ N4 N5 N6 Let's call our counting method countNodes().

Pseudocode for countNodes int countNodes(Node parent): if parent: return 1 + countNodes(parent. Left) + countNodes(parent. Right) else: return 0 countNodes will first check if the node you pass to it exists.

If not (base case) return 0 logically this makes sense because if the node doesn't exist, there will be no nodes in it's subtree's that don't exist If the node exists you will return 1 + the sum of the number of nodes in the subtrees. To find the number of nodes in the subtrees we will just call our countNodes() method on each child node. In the example tree above we start with N1.

We see that N1 exists so what we need to find now is: 1 + # of nodes in the left subtree + # of nodes in the right subtree. N1's subtrees are: Left Right N2 N3 /\ \ N4 N5 N6 We start by calling the countNodes() method on N2 (N1's left subtree). N2 exists so now we are looking for 1 + # of nodes in N2's left subtree + # of nodes in N2's right subtree N2's subtrees are: Left Right N4 N5 calling countNodes() on N4 we are looking for 1 + # of nodes in N4's left subtree + # of nodes in N4's right subtree N4's left node is null so when countNodes() is called on N4's left node it will return 0 (base case).

N4's right node is also null so countNodes() for the right node is also going to return 0 N4's pending operations can complete and you have 1 + 0 + 0 (non-base case return value) Now N2's pending operation looks like 1 + 1 + # of nodes right subtree calling countNodes() on N5 results in the same value as N4 so N2's return value has become: 1 + 1 + 1 N2 returns 3 to N1's pending operation to make it look like: 1 + 3 + # nodes in right subtree # nodes in N3 subtree is: countNodes() on N3 returns 1 + countNodes->null (base) + countNodes()->N6 # nodes in N6 subtree is countNodes() on N6 returns 1 + countNodes->null (base) + countNodes()->null (base) return value = 1 # nodes in N3 subtree is 1 + 0 + 1 returns 2 finally the call on N1's countNodes() can complete with 1 + 3 + 2 countNodes() on N1 returns 6 To count all the right nodes in a tree you can use the following pseudocode: int countRightNodes(Node parent): if! Parent: return 0 if has right node: return 1 + (# of right nodes in left subtree) + (# of right nodes in right subtree) else: return (# of right nodes in left subtree).

The point of recursion, is to simplify a complicated problem until it is trivial, and use the solution of this trivial problem to find the solution of a more complicated problem, etc, until you can solve your actual problem. In your case 1), the complicated problem is "Find the number of nodes in the whole tree". But we can simplify this, by saying that it is numberOfNodesLeftSubtree + numberOfNodesRightSubtree + 1.

We can then write: public int nbNodes(Node root){ int count = 1 // our actual node if(root. Left! = null){ count += nbNodes(root.

Left); } if(root. Right! = null){ count += nbNodes(root.

Right); } return count; } It's that easy.

Thinking recursively takes some practice, but it almost seems magical when done well. We need to count the number of nodes in our tree, so let's think what that means. Consider any node and it's sub-tree.

The number of nodes in this sub-tree will be equal to the number of nodes in the left sub-tree plus the number of nodes in the right sub-tree, plus one (for the node in question). So if we assume we already had a method that could count nodes in a sub-tree, we could make two calls to that method. We also have to worry about the base case, and then once we have that recursion can work its magic.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions