【问题标题】:K-Ary Tree and Symmetric Search/Visit in JavaJava中的K-Ary树和对称搜索/访问
【发布时间】:2015-05-31 23:00:32
【问题描述】:

我正在处理一个大问题:我几乎完成了我的第一个大学项目,但现在我陷入了困境。

我需要创建一个方法(用java语言)来对称访问ak-ary树(这是我教授的话:k-ary tree的对称访问是从第一个到(k /2)-th,接着是根的访问,接着是从(k/2)-th到k-th的子树的对称访问(“访问一个节点”是指访问其中包含的信息,“访问a subtree”表示子树中所有节点的访问)。

我创建了两个类来实现 k-ary 树:Node 和 Tree。

Node.java:

Field Summary:

List<Node<T>> children; //the children of the current node
T info; //property that the node contain
static int maxNrOfChildren; //equals to k-arity of the tree
Node<T> parent; //the parent of current node

Constructor Summary:
Node(T info) //creates a node containing some informations

Method Summary
void addChild(Node<T> childNode) //add a child to a node
void addChildAtIndex(Node<T> childNode, int index) //add a child to a node at a specific index
List<Node<T>> getChildren() //returns a list of the children of a node
T getInfo() //returns the informations contained in a node
void setInfo(T info) //set an information to a node
java.util.List<T> getInfoChildrenOfNode() //returns a list containing all the infos conitaned in the children of a node
int getNumberOfChildren() //return the number of children of a node
Node<T> getParent() //returns the parent of a node
boolean isLeaf() //returns true if a node is a leaf

Tree.java:

Field Summary:

int ar; //equals to k-arity of the tree (int k of the constructor)
Node<T> root; //the root node of a tree

Constructor Summary:
Tree(int k) //creates a Tree of k arity

Method Summary:
void addRoot(T info) //add a root to a tree
void changeRoot(T info) //changes the root of a tree creating a new one with new info
Node<T> getRoot() //return the root of the current tree
void addNewNodeVasithChildOfNodeU(Node<T> u, T info, int i) //add a new node as it is the i-th child of the node u
int getHeight() //returns the height of the tree
int getHeight(Node<T> n) //returns the height from a specific node
void innesta(Node<T> u, Tree<T> subTree) //add a tree (subtree) as a child of a node of the principal tree
int numberOfNodesInTree() //returns the number of the nodes in the tree
int numberOfNodesInTree(Node<T> node) //returns the number of the nodes in the tree starting from a specific node
LinkedList<T> visitaBFS() //returns a list of the infos obtained with a BFS
LinkedList<T> visitaDFSA() //returns a list of the infos obtained with a DFS (pre-order)
LinkedList<T> visitaDFSP() //returns a list of the infos obtained with a DFS (post-order)
LinkedList<T> visitaSIM()//THE ONE I DON'T KNOW HOW TO DO! should return the infos obtained with a symmetrical visit of the tree

非常感谢所有可以帮助我编写最后一个方法的人。

【问题讨论】:

  • 从traversal of a binary tree 开始,看看你能想出什么。
  • 已经试过了。我在这次访问 alg 上花了大约三个小时,我想出的唯一有趣的事情是我肯定需要两个 cicles,一个从 0 到 arity/2,另一个从 arity/2 到 arity。我还需要使用堆栈(LinkedList 实现)..

标签: java algorithm tree


【解决方案1】:

有一个循环,对于每个节点,递归访问第一个 k/2 子树。返回后,访问当前节点,然后在循环中递归访问剩余的子树。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 2013-02-20
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 1970-01-01
    • 2014-01-10
    相关资源
    最近更新 更多