本文主要是介绍java基本树型结构实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近复习数据结构,用java写一个树的模板,代码如下,写的比较仓促,如果有哪里写的不好,或者有错误,请在评论区提出,谢谢。
package Tree;import java.util.ArrayList;
import java.util.List;
/*** 树节点* @author ** @param <T>*/
public class Node<T> {private T cont;//节点内容private Node<T> parent;//父节点public List<Node<T>> nodeList;//子节点列表/*** 构造函数* @param cont* @param parent* @param nodeList*/public Node(T cont) {super();this.cont = cont;this.parent = null;this.nodeList = new ArrayList<Node<T>>();}public Node() {super();// TODO Auto-generated constructor stub}/*** get &set* @return*/public T getCont() {return cont;}public void setCont(T cont) {this.cont = cont;}public Node<T> getParent() {return parent;}public void setParent(Node<T> parent) {this.parent = parent;}public List<Node<T>> getNodeList() {return nodeList;}public void setNodeList(List<Node<T>> nodeList) {this.nodeList = nodeList;}}
——————————————-我是分割线—————————————
package Tree;
/*** 树类* @author ** @param <T>*/
public class Tree<T> {private Node<T> root;public Node<T> getRoot() {return root;}/*** 构造函数* @param root*/public Tree(Node<T> root) {super();// root.setParent(null);this.root = root;}public Tree(T rootCont) {super();this.root = new Node(rootCont);}/*** 增加新节点* @param curNode* @param newNode*/public void addNewNode(Node<T> curNode,Node<T> newNode){if(newNode==null||curNode==null){return ;}newNode.setParent(curNode);curNode.nodeList.add(newNode);}/*** 遍历*/public void traversal(Node<T> root){System.out.println("Traversal to"+root.getCont());for(Node<T> tempNode:root.getNodeList() ){traversal(tempNode);}}/*** 查找* @param root* @param searchcont* @return*/private void searchHelp(Node<T> root,Node<T> result,T searchcont){if(root.getCont()==searchcont){result.setCont(root.getCont());result.setNodeList(root.nodeList);result.setParent(root.getParent());}for(Node<T> tempNode:root.getNodeList() ){searchHelp(tempNode,result,searchcont);}}public Node<T> search(Node<T> root,T searchcont){if(searchcont==null){return null;}Node<T> result=new Node<T>();searchHelp(root,result,searchcont);//System.out.println("search completed"+result.getCont());return result;}}
——————————————-我是分割线—————————————
package Tree;public class test {public static void main(String[] args) {// TODO Auto-generated method stubTree<String> strTree=new Tree<String>(new Node<String>("系统管理"));strTree.addNewNode(strTree.search(strTree.getRoot(), "系统管理"), new Node<String>("部门管理"));strTree.addNewNode(strTree.search(strTree.getRoot(), "系统管理"), new Node<String>("岗位管理"));strTree.addNewNode(strTree.search(strTree.getRoot(), "系统管理"), new Node<String>("用户管理"));strTree.addNewNode(strTree.search(strTree.getRoot(), "岗位管理"), new Node<String>("岗位添加"));strTree.addNewNode(strTree.search(strTree.getRoot(), "岗位管理"), new Node<String>("岗位删除"));strTree.addNewNode(strTree.search(strTree.getRoot(), "用户管理"), new Node<String>("用户添加"));strTree.addNewNode(strTree.search(strTree.getRoot(), "用户管理"), new Node<String>("用户修改"));strTree.traversal(strTree.getRoot());//遍历结果}}
这篇关于java基本树型结构实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!