Results 1 to 6 of 6
Thread: Full binary tree
- 12-23-2010, 07:27 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
- 12-23-2010, 01:55 PM #2
No. That's not how this works. We aren't a code mill or a homework service. That would cost money. How much are you offering to pay?
Read this before you post again: How To Ask Questions The Smart Way
- 12-23-2010, 02:15 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
Excuse me please. Give me an advice. I wrote the code:
I receive:PHP Code:public class Main { public static void main(String[] args) { new Main().run(); } static class Node { Node left; Node right; Node buf; int index; int value; public Node(int value) { this.value = value; //System.out.println(" Node Value " + this.value); } } public void run() { Node rootnode = new Node(25); System.out.println("Building tree with rootvalue " + rootnode.value); System.out.println("================================="); insert(rootnode, 11); insert(rootnode, 15); insert(rootnode, 16); insert(rootnode, 23); insert(rootnode, 45); insert(rootnode, 36); } public void insert(Node node, int value) { // System.out.println(" Value " + node.value); if (node.index==1) { if (node.right != null) { node.buf = node.right; insert(node.right, value); } else { System.out.println(" Inserted " + value + " to right of node " + node.value); node.right = new Node(value); node.index = 0; } } else { if (node.left != null) { insert(node.left, value); } else { System.out.println(" Inserted " + value + " to left of node " + node.value); node.left = new Node(value); node.index = 1; } } }
Inserted 11 to left of node 25
Inserted 15 to right of node 25
Inserted 16 to left of node 11
Inserted 23 to right of node 11
Inserted 45 to left of node 16
Inserted 36 to right of node 16
OR:
25
/ \
11 15
/ \
16 23
/ \
45 36
BUT I NEED:
25
/ \
11 15
/ \ / \
16 23 45 36
How can I do it?
- 12-23-2010, 02:21 PM #4
How would you do it "by hand" without worrying about the code? Write out the steps, as specifically as possible, so that you could give the instructions to a complete stranger and have them do the correct thing.
When you have that finished, it should be pretty easy to convert to code.
That'll be five dollars.
- 12-23-2010, 02:26 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
The nodes of a full binary search tree can be stored in an array where the root is stored at index #0 and the children of node #i are stored at indexes #(2*i+1) and #(2*i+2). Inserting a node is done by doing a binary search over the array and inserting the node at the wanted position.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-23-2010, 02:43 PM #6
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
binary tree
By ryamz in forum New To JavaReplies: 2Last Post: 08-12-2010, 02:45 AM -
Data Structures(Binary Search Tree to AVL Tree)ASAP pls
By jfAdik in forum Forum LobbyReplies: 0Last Post: 04-04-2010, 07:40 AM -
Binary Tree
By MuslimCoder in forum New To JavaReplies: 8Last Post: 11-19-2009, 05:57 PM -
Binary Search Tree
By anmadie in forum New To JavaReplies: 5Last Post: 11-17-2009, 02:39 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks