Results 1 to 3 of 3
 11252010, 08:29 AM #1Member
 Join Date
 Jan 2010
 Posts
 32
 Rep Power
 0
How to keep track of nodes in a tree?
I'm having trouble keeping track of nodes in a tree. I can insert them fine, but when I need to access, modify, or remove a node, I can't figure out to reference the node. Aside from creating a "new Node" for every single Node....
For example, if I have a tree with some root, and attach 5 nodes with elements H, A, P, P, Y one after the other so that Y is a child of P, P is a child of P, P is a child of A, etc...
I'd like to attach H,E,L,L starting from H but I don't know how to reference it. Currently I just have two separate H nodes.
 11252010, 08:47 AM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,271
 Blog Entries
 7
 Rep Power
 24
Are the nodes ordered? i.e. for a node N, a left child L and a right child R, L < N <= R? If so a simple recursive search can find a node where you have to attach another (new) node: (this implementation assumes that at least one root node is present)
Java Code:public void insert(Node n, Node child) { if (n.data > child.data) if (n.left == null) n.left= child; else insert(n.left, child); else if (n.right == null) n.right= child; else insert(n.right, child); }
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 11262010, 12:30 AM #3Member
 Join Date
 Jan 2010
 Posts
 32
 Rep Power
 0
Thank you, very helpful.
I have something like this:
Java Code:Position<Keystroke> currentPos = tree.root(); Iterator<Position<Keystroke>> childrenList; childrenList = tree.children(currentPos); for(int i=0; i<keys.length; i++){ while(childrenList.hasNext()){ Position<Keystroke> potentialPos = childrenList.next(); if(potentialPos.element().key == keys[i]){ currentPos = potentialPos; childrenList = tree.children(currentPos); break;//not needed, but prevents needless searching } } } System.out.println("found it: " + currentPos.element().words.first().element());
Similar Threads

How do I determine the height of nodes in a AVL tree?
By ryuzog in forum New To JavaReplies: 2Last Post: 11092010, 02:34 PM 
Data Structures(Binary Search Tree to AVL Tree)ASAP pls
By jfAdik in forum Forum LobbyReplies: 0Last Post: 04042010, 07:40 AM 
Adding/removing nodes to tree under TreeViewer
By Rodrigo Braz in forum SWT / JFaceReplies: 0Last Post: 04202009, 01:02 AM 
Track download
By nilesh.malode in forum Advanced JavaReplies: 1Last Post: 07132007, 09:44 PM
Bookmarks