gpt4 book ai didi

java - Java 中的 K-Ary 树和对称搜索/访问

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:03:20 24 4
gpt4 key购买 nike

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

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

我创建了两个类来实现 k 叉树: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

树.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

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

最佳答案

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

关于java - Java 中的 K-Ary 树和对称搜索/访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30540367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com