java非递归生成树及遍历

 

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Stack;

import javax.swing.tree.DefaultMutableTreeNode;

import com.model.tree.TreeNode;

public class TestList {
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  List<TreeNode> list = new ArrayList<TreeNode>();
  TreeNode n = new TreeNode();
  n.setNodeid("01");
  n.setSuperid("0");
  list.add(n);

  n = new TreeNode();
  n.setNodeid("02");
  n.setSuperid("0");
  list.add(n);

  n = new TreeNode();
  n.setNodeid("0101");
  n.setSuperid("01");
  list.add(n);
  n = new TreeNode();
  n.setNodeid("0102");
  n.setSuperid("01");
  list.add(n);
  n = new TreeNode();
  n.setNodeid("0103");
  n.setSuperid("01");
  list.add(n);
  n = new TreeNode();
  n.setNodeid("0201");
  n.setSuperid("02");
  list.add(n);
  n = new TreeNode();
  n.setNodeid("0202");
  n.setSuperid("02");
  list.add(n);
  n = new TreeNode();
  n.setNodeid("020201");
  n.setSuperid("0202");
  list.add(n);
  n = new TreeNode();
  n.setNodeid("010101");
  n.setSuperid("0101");
  list.add(n);
  n = new TreeNode();
  n.setNodeid("01010101");
  n.setSuperid("010101");
  list.add(n);
  
  Collections.sort(list);

  DefaultMutableTreeNode tree = createTree(list);

  showTree(tree);
 }

 public static void showTree(DefaultMutableTreeNode tree){
  if(tree!=null){
   Stack<javax.swing.tree.TreeNode> stack=new Stack<javax.swing.tree.TreeNode>();
   javax.swing.tree.TreeNode t=null;
   stack.push(tree);
   while(!stack.empty()){
    javax.swing.tree.TreeNode tpop=stack.pop();
    System.out.println(tpop);
    int count=tpop.getChildCount();
    if(count>0){
     for(int i=0;i<count;i++){
      stack.push(tpop.getChildAt(i));
     }
      
   }
  }
 }

 public static final DefaultMutableTreeNode createTree(List<TreeNode> list) {
  DefaultMutableTreeNode tree = new DefaultMutableTreeNode("root");
  Map<String, DefaultMutableTreeNode> map = getTreeNodeMap(list);
  while (list.size() > 0) {
   TreeNode n = (TreeNode) list.get(list.size() - 1);
   if (map.get(n.getSuperid()) == null) {
    tree.add(map.get(n.getNodeid()));
    map.remove(n.getNodeid());
    list.remove(n);
   } else {
    DefaultMutableTreeNode treeNode = map.get(n.getSuperid());
    treeNode.add(map.get(n.getNodeid()));
    map.remove(n.getNodeid());
    list.remove(n);
   }
  }
  return tree;
 }

 public static final Map<String, DefaultMutableTreeNode> getTreeNodeMap(
   List<TreeNode> nodes) {
  Map<String, DefaultMutableTreeNode> treeNodeMap = new HashMap<String, DefaultMutableTreeNode>();
  if (nodes == null) {
   return null;
  }
  Iterator I = nodes.iterator();
  while (I.hasNext()) {
   TreeNode n = (TreeNode) I.next();
   treeNodeMap.put(n.getNodeid(), new DefaultMutableTreeNode(n
     .getNodeid()));
  }
  treeNodeMap.put("root", new DefaultMutableTreeNode("root"));
  return treeNodeMap;
 }

 

}

 

 

/*

TreeNode 类有两个主要属性 nodeid superid

并且有实现Comparable接口. 主要用于排序,

很简单,这里不列出

*/


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值