Java中的基本数组[]树数据结构

时间:2012-12-08 22:48:23

标签: java arrays

这是一个家庭作业问题,所以我不是在寻找完整的代码答案。

我有一个班级狗

package lab12;

import java.io.Serializable;

public class Dog implements Serializable{

    public Dog[] children;
    public String name;

    public Dog(String name)
    {
        this.name = name;
    }

    @Override
    public String toString()
    {
        return name;
    }

}

包含根狗Spot的数据文件,其子项存储在数组中。我需要编写可以打开数据文件的代码,然后单步执行树数据结构以查看输入名称是否是根(Spot)的后代。

我非常有信心可以打开数据文件。我正在努力创建具有数组作为链接的节点的语法。我们的教科书只涵盖二进制树,它们可以链接到左侧或右侧,但不能链接到可变数量的链接。我找到了一个使用List方法的通用示例。

public class Tree<T> 
{
    private Node<T> root;

    public static class Node<T> 
    {
        private T data;
        private Node<T> parent;
        private List<Node<T>> children;
    }

    public Tree(T rootData) 
    {
        root = new Node<T>();
        root.data = rootData;
        root.children = new ArrayList<Node<T>>();
    }
}

由于我必须使用数据文件,因此除了将子项存储在Dog []中之外,我无法将Node的结构更改为其他任何内容。我找不到使用基本数组来存储子节点的节点类的示例,我无法弄清楚这样做的语法。在我尝试学习它之前,我认为在没有泛型的情况下看到它会有所帮助。

到目前为止,这是我的代码:

package lab12;

public class DogTree 
{
    //Start Inner Class
    private static class Node
    {
        private String name;
        private Node parent;
        private Node Dog[] children; //This is where I'm confused
    }
    //End Inner Class

    private Node root;

    public DogTree()
    {
        root = null;
    }

    public boolean isDescendant(String name)
    {
        return isInSubtree(name, root);
    }

    private static boolean isInSubtree(String name, Node subTreeRoot)
    {
        if(subTreeRoot == null)
        {
            return false;
        }
        else if(subTreeRoot.name.equals(name))
        {
            return true;
        }
        else
        {
            //This is where my confusion on the 
            //node design causes implementation problems
            return isInSubtree(name, subTreeRoot.children); 
        }
    }
}

2 个答案:

答案 0 :(得分:1)

你需要遍历一系列孩子。

private static boolean isInSubtree(String name, Node subTreeRoot)
    {
        if(subTreeRoot == null)
        {
            return false;
        }
        else if(subTreeRoot.name.equals(name))
        {
            return true;
        }
        else
        {
           for( int i = 0; i< subTreeRoot.children.length();i++)
             if(isInSubtree(name, subTreeRoot.children[i]))
            return true;
        }
        return false;

    }

答案 1 :(得分:0)

这些算法中数组和数组列表的主要区别在于数组容量有限,需要处理项目分配(小狗);

如果任务是学习数组,则不应该关注泛型。通用允许您为对象的常见功能定义模板。最好将实现更改为通用,而不是从它们开始解决具体问题。

你现在拥有的是这样的课程:

class Dog {

    private String name;
    private Dog[] puppies

    public Dog(String name)
    {
        this.name = name;
    }

}

您可以做的是向其中添加将执行某些操作的特定方法。

class Dog {

    private final String name;
    private final Dog[] puppies = new Dog[20]; //We create 20 slots for puppies to fill
    private int  puppiesCount = 0;

    public Dog(String name)
    {
        this.name = name;
    }

    public addPuppie(Dog puppie) {
      this.puppies[puppiesCount] = puppie; //We assign the puppie to this Dog puppies.
      this.puppiesCount = puppiesCount + 1; //We increment the count of puppies
    }

    public Dog[] getPuppies() {
      return Arrays.copyOf(puppies, puppiesCount); 
    }

    public boolean isMyPuppie(Dog puppie) {

        for(int = 0; i < puppiesCount; i++) {

          if(puppies[i] == puppie) { //We compare the object references to state that are equal
             return true;
          }

        }

      return false;
    }
}

这如何转移到树中。

由于每个对象都有一个self数组,因此可以嵌套它们。

Dog max = new Dog("Max");

Dog tom = new Dog("Tom"); //childOfRoot;

Dog barry = new Dog("Barry);"// child of Tom

要创建树,您需要执行此类操作。

tom.addPuppie(barry); // We assign barry to tom
max.addPuppie(tom); // we assign tom to max.

这个概念应该通过深度搜索进行扩展,您需要引入一个重复搜索,并且可以添加从子级到父级的关系。