cancel
Showing results for 
Search instead for 
Did you mean: 

Search fucntionality of Tree Node element in TreeHierarchy-need Sample code

Former Member
0 Kudos

Hi All

Currentlly we have the requirement for search fucntionality of a Tree Node element in a Tree Hierarchy

Rathen using "equalsIgnoreCase()" we are using contains() method for searching with the given the word with respect to the entire hierarchy in the tree"

for example : if the entered text for search is "General" then on action of search button then the entered text will compare with all the node elements in the tree hiearchy with contains() method, suppose the entered text "general" contains three tree node elements then it is hightlighting only the last element in the Tree strucuture,

actually our requirement , after entering the "general" and click of action then it has to highlihht the first tree element which matched the key word "General" then next time again when we click on the search button it has to highlight the second tree element in the hiearchy and next time search button it has to highlight the tree element which matches the keyword "General" with contains comparision like that it will complete the search opeartion with given entered ketword till the end of the tree hierarchy and highlight the tree element on click of search button each time

.

Could you please provide the sample code on the same?? currently you had given the sample code with "equalsIgnoreCase" comparision but we are using comparision with " IsText.contains(TreeNodelement)" , when we are using the contains() method for comparision if it returns 3 Node elements but first time on click of the search button it has to highlight first tree element and next time on click of search button it has to highlight the second tree element in the Tree hiearchy until the end of tree hiearchy.

Could you please provide sample code on the same requirement??

Thanks in advance

Regards

Kalki Reddy

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

If I understand your question correctly, you want to implement a findNext() method that returns the next tree node (in DFS order) that matches a given search condition.

Such a method can be implemented using a non-recursive tree traversal. This method can then be called repeatedly and will return all matching nodes one by one until the stack is empty.


package test.treesearch;

public class TreeSearcher<T>
{
  private Stack<TreeNode<T>> stack;
  
  public TreeSearcher(TreeNode<T> root)
  {
    stack = new Stack<TreeNode<T>>();
    stack.push(root);
  }
  
  public TreeNode<T> findNext(T value)
  {
    while (!stack.isEmpty())
    {
      TreeNode<T> node = stack.pop();
      for (int i = node.getChildCount() - 1; i >= 0; --i)
      {
        TreeNode<T> child = node.getChild(i);
        stack.push(child);
      }
      if (node.getValue().equals(value))
      {
        return node;
      }
      else
      {
        System.out.println("No match: " + node.getAddress());
      }
    }
    return null;
  }
}

Using class:


public class TreeSearch
{
  public static void main(String[] args)
  {
    TreeNode<Animal> root = new TreeNode<Animal>(Animal.random());
    addAnimals(root, 3, 3);
    new TreePrinter<Animal>().print(root);
    findAll(root, Animal.Dog);
    findAll(root, Animal.Cat);
    findAll(root, Animal.Mouse);
  }
  
  private static void findAll(TreeNode<Animal> root, Animal animal)
  {
    System.out.println(animal + ":");
    TreeSearcher<Animal> searcher = new TreeSearcher<Animal>(root);
    while (true)
    {
      TreeNode<Animal> node = searcher.findNext(animal);
      if (node == null)
        break;
      System.out.println(node.getAddress());
    }
  }

Armin

Former Member
0 Kudos

package test.treesearch;

import java.util.ArrayList;
import java.util.List;

public class TreeNode<T>
{
  private T value;
  private String address;
  private List<TreeNode<T>> children;
  
  public TreeNode(T value)
  {
    this.value = value;
  }
  
  public TreeNode<T> addChild(TreeNode<T> child)
  {
    if (children == null)
    {
      children = new ArrayList<TreeNode<T>>(3);
    }
    children.add(child);
    int index = children.size();
    child.address = (address == null) ? String.valueOf(index) : address + "." + index;
    return child;
  }

  public TreeNode<T> getChild(int index)
  {
    return children.get(index);
  }
  
  public int getChildCount()
  {
    return children == null ? 0 : children.size();
  }
  
  public T getValue()
  {
    return value;
  }

  public String getAddress()
  {
    return address;
  }
}
Former Member
0 Kudos

package test.treesearch;

public enum Animal
{
  Dog, Cat, Mouse;
  
  public static Animal random()
  {
    return values()[(int) (Math.random() * values().length)];
  }
}
Former Member
0 Kudos

package test.treesearch;

import java.util.ArrayList;
import java.util.List;

public class Stack<T>
{
  private List<T> items = new ArrayList<T>();
  
  void push(T item)
  {
    this.items.add(0, item);
  }
  
  T top()
  {
    return items.get(0);
  }
  
  T pop()
  {
    return items.remove(0);
  }
  
  boolean isEmpty()
  {
    return items.size() == 0;
  }
}
Former Member
0 Kudos

package test.treesearch;

public class TreePrinter<T>
{
  public void print(TreeNode<T> root)
  {
    Stack<Object> stack = new Stack<Object>();
    stack.push(root);
    stack.push("");
    while (!stack.isEmpty())
    {
      String indent = (String) stack.pop();
      TreeNode<T> node = (TreeNode<T>) stack.pop();
      System.out.println(indent + node.getValue() + "(" + node.getAddress() + ")");
      for (int i = node.getChildCount() - 1; i >= 0; --i)
      {
        TreeNode<T> child = node.getChild(i);
        stack.push(child);
        stack.push(indent + "  ");
      }
    }
  }
}

Answers (0)