Results 1 to 16 of 16
Thread: Binary trees
- 11-19-2010, 03:33 PM #1
Member
- Join Date
- Nov 2010
- Location
- Mississauga, ON, Canada
- Posts
- 9
- Rep Power
- 0
Binary trees
Hi,
I have a set of data stored in a file, I want to know how to import this data and check:
1- if they construct binary tree
2- for their binary search property
and finally if they have AVL property. (not that important)
I'm really stuck on this. I have spent 1 week to figure it out but I couldn't.
I need your help.
if u provide a sample code, that would be great.
- 11-19-2010, 03:54 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
- 11-19-2010, 04:17 PM #3
Member
- Join Date
- Nov 2010
- Location
- Mississauga, ON, Canada
- Posts
- 9
- Rep Power
- 0
Thanks for ur reply :)
the data are arranged as follows:
1- number of nodes
2- position of nodes(where the first number is the index of the left node and the second number is the index of the right index) followed by the node name.
3- some transaction records(another story, I'm gonna ask about that later)
Thanks again
sample:
3
0 1 Victoria
2 1 Russia
0 1 France
delete Viroria
insert Egypt
search Poland
- 11-20-2010, 05:08 AM #4
Double posted at
binary tree
db
-
- 11-20-2010, 05:43 AM #6
Member
- Join Date
- Nov 2010
- Location
- Mississauga, ON, Canada
- Posts
- 9
- Rep Power
- 0
i double posted because no one replied yet
-
- 11-20-2010, 10:23 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
- 11-20-2010, 02:02 PM #9
Member
- Join Date
- Nov 2010
- Location
- Mississauga, ON, Canada
- Posts
- 9
- Rep Power
- 0
explanation to the index numbers:
the first field is the left child index, an integer that denotes the index of the left child of the current node. A left child index of 0 indicates that the current node has no left left child. the second field is the right child index, an integer that denotes the index of the right child of the current node. A right child index of 0 indicates that the current node has no right left child. the third field is the node`s name.
Example:
7
2 3 Lemons
4 0 Lesbos
0 5 Mikonos
6 0 Naxos
0 7 Paros
0 0 Rohdes
0 0 Queensland
- 11-20-2010, 02:03 PM #10
Member
- Join Date
- Nov 2010
- Location
- Mississauga, ON, Canada
- Posts
- 9
- Rep Power
- 0
- 11-20-2010, 02:51 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
Ok, I think I got it: your index values are 1 (one) based). Lemons is the root node with a left child Lesbos and a right child Mikonos etc. right? If so this data set doesn't even is a search tree because for a search tree ileft child <= root <= right child. Is my notion correct?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-20-2010, 03:10 PM #12
Member
- Join Date
- Nov 2010
- Location
- Mississauga, ON, Canada
- Posts
- 9
- Rep Power
- 0
That`s right, I have 5 sets of data and I should check for each set if it is binary tree or not.
so i need a sample code that can do that....
Thanks
- 11-20-2010, 03:41 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
I'm not going to write the program for you but here is the algorihm; define a class Node like this:
As you can see this is just a data class (no methods and all public members). The last member 'parent' is set to another node (its parent) if some other node has claimed that it is the parent of that particular position. All nodes are stored in an array or ArrayList. Set the parent node to refer to ifself for the root node.Java Code:public class Node { public String data; public Node left; public Node right; public Node parent; }
When you read a node for index i; check if there is a node 'claimed' by a parent (the parent member is not null) but not yet filled in (no data set yet). If so fill in the data field and create nodes (if any) for its children. Otherwise the data set doesn't form a tree.
Repeat the above for all nodes; when done check the entire array (or List) for claimed nodes that are not filled in. If you find any the data didn't form a tree; if you find null nodes the data didn't form a tree either.
If the data set forms a tree you can check whether or not the tree is an AVL tree.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-20-2010, 03:49 PM #14
Member
- Join Date
- Nov 2010
- Location
- Mississauga, ON, Canada
- Posts
- 9
- Rep Power
- 0
- 11-20-2010, 04:12 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
- 11-20-2010, 04:29 PM #16
Member
- Join Date
- Nov 2010
- Location
- Mississauga, ON, Canada
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Splay Trees
By Growler in forum New To JavaReplies: 0Last Post: 11-02-2010, 02:10 PM -
Need help with Trees...(8-puzzle)
By ventrue in forum New To JavaReplies: 2Last Post: 03-23-2009, 11:04 PM -
Tutorial on Binary Search Trees
By JordashTalon in forum New To JavaReplies: 3Last Post: 03-18-2009, 03:51 PM -
Game Trees
By javaBoy1 in forum Advanced JavaReplies: 0Last Post: 12-27-2008, 12:20 AM -
Help With Tournament Trees
By wiggsfly in forum New To JavaReplies: 2Last Post: 10-26-2008, 09:38 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks