Results 1 to 3 of 3
  1. #1
    david19 is offline Member
    Join Date
    Jan 2017
    Posts
    1
    Rep Power
    0

    Default How to search for item in m-ary tree and add all nodes to linked list?

    Having a lot of trouble with this one. I have a m-ary tree with nodes of skill perks for an rpg type thing and I'm trying to write a method that traverses the tree until it finds the correct node and then adds all of the nodes it traversed to get to the match to a linked list that the method will return. I want to use a recursive traversal of the tree to find the named skill, and then add skills to the output list as the recursion unwinds but I'm stuck on how to do that. Any help is appreciated. Totally lost right now. My attempted method code is below. Thank you.

    Java Code:
    	public LinkedList280<Skill> treeTraverse(MAryNode280<Skill> curNode, Skill name, LinkedList280<Skill> list) {
    		
    		for (int i = 1; i <= curNode.lastNonEmptyChild; i++) {
    			
    			if(curNode.item.equals(name)) {
    				
    				break;
    	
    			} else {
    			
    			treeTraverse(curNode.subnode(i), name, list);
    			
    			}
    			
    		}
    
    		list.insert(curNode.item);
    		return list;
    		
    	}
    I can traverse the entire tree by the following
    Java Code:
    	public void treeTraverse(MAryNode280<Skill> curNode) {
    		
    		System.out.println(curNode.item.skillName);
    		
    		for (int i = 1; i <= curNode.lastNonEmptyChild; i++) {
    			
    			
    			treeTraverse(curNode.subnode(i), name);
    	
    		}
    		
    	}

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to search for item in m-ary tree and add all nodes to linked list?

    I have never actually done this before. But you might need a lateral pointer pointing to next sibling and possibly a mutable pointer to keep track of where you have been. My best recommendation would be do to a web search on m-ary tree traversal. You may find that do to so requires a specific minimum node configuration.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: How to search for item in m-ary tree and add all nodes to linked list?

    But what's the structure of your tree?

Similar Threads

  1. Linked list and binary tree
    By urielik in forum Advanced Java
    Replies: 1
    Last Post: 12-22-2011, 03:49 PM
  2. Linked list and binary tree
    By urielik in forum New To Java
    Replies: 1
    Last Post: 12-22-2011, 01:40 PM
  3. Homework Linked Binary Search Tree
    By brlau in forum New To Java
    Replies: 2
    Last Post: 12-21-2011, 09:14 AM
  4. How would I count number of nodes in linked list?
    By colerelm in forum New To Java
    Replies: 2
    Last Post: 10-17-2011, 07:26 AM
  5. linked list nodes all refernce same item.
    By yllawwally in forum New To Java
    Replies: 0
    Last Post: 12-18-2007, 09:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •