PDA

View Full Version : Binary Tree in Java



daniel.mihalcea
November 3rd, 2009, 02:12 AM
Hi there.
I have a homework at school and I don't know where to start.
It is about a binary tree with some properties and I have to create a data type NODE and the operators(search, add, delete).
After I made the node I will have to make:
1. the add operation for a node in the binary tree.
2. the search operation of a value using a minimum number of operations and if the value was find I will have to return the number of nodes until that point, else return -1.

My problem is that I don't know where to start.
I am looking for a hint or to find that NODE data type somewhere on the internet.

Thank you.
Regards, Daniel.

ericwinter
November 3rd, 2009, 08:47 PM
The best way to start any program is pulling out pen and paper and jotting notes.

There is no general NODE data type on the internet...each NODE written by other programmers was designed solely for some purpose in some program.

Start simple, a single node for example. What is a node?

In your program, it seems like the purpose of your NODEs is they have a value and can point to two other nodes. Just there you have some attributes of your class NODE. 1 data type, probably an Integer, and at most 2 pointers which can point to two other child NODEs.

actionAction
November 8th, 2009, 11:49 PM
Hi Daniel,

Eric is correct, a Node is an abstract data type, the BST is a means of storing Nodes so they can be searched quickly. I'm not sure about the Java implementation as my Data Structures courses were C/C++ focused, but Nodes have two pointers, both initialized to NULL. All Nodes are the same, except for the root node, which has no parent. You really only have to figure out how to search one Node set to search an entire BST, it will definitely be a recursive method. As for adding a Node, it's really pretty straightforward given the structure of a BST. The only confusion is if you have to insert a node in the middle of a tree (rather than at the bottom), in which case you just do a little pointer swapping and you are good to go.

Hope this helps.