树的基本操作
2015-06-16 14:53
357 查看
package datastructure; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.Stack; class BiTree{ char data; BiTree leftchild; BiTree rightchild; } class Counter{ // String array="ABD###CE##F##"; String array="ABDG###EHI####CF###"; int i = -1; char next(){ i++; return array.charAt(i); } } class LevelCounter{ int level; public LevelCounter(int level){ this.level = level; } LevelCounter prelevel(){ level--; return this; } } class SNode{ BiTree node; boolean visited; public SNode(BiTree node, boolean visited){ this.node = node; this.visited = visited; } } class Tree{ public BiTree buildBiTree(BiTree t, Counter counter){ char ch = counter.next(); if(ch == '#'){ t = null; }else{ t = new BiTree(); t.data = ch; t.leftchild = buildBiTree(t, counter); t.rightchild = buildBiTree(t, counter); } return t; } public void preorder(BiTree t){ if(t != null){ System.out.print(t.data); preorder(t.leftchild); preorder(t.rightchild); } } public void inorder(BiTree t){ if(t != null){ inorder(t.leftchild); System.out.print(t.data); inorder(t.rightchild); } } public void postorder(BiTree t){ if(t != null){ postorder(t.leftchild); postorder(t.rightchild); System.out.print(t.data); } } public void preorder2(BiTree t){ Stack<BiTree> stack = new Stack<BiTree>(); while(t != null || !stack.isEmpty()){ while(t != null){ System.out.print(t.data); stack.add(t); t = t.leftchild; } if(!stack.isEmpty()){ BiTree node = stack.pop(); t = node.rightchild; } } } public void preorder3(BiTree t){ Stack<BiTree> stack = new Stack<BiTree>(); if(t != null){ stack.add(t); while(!stack.isEmpty()){ BiTree node = stack.pop(); System.out.print(node.data); if(node.rightchild != null) stack.add(node.rightchild); if(node.leftchild != null) stack.add(node.leftchild); } } } public void inorder2(BiTree t){ Stack<BiTree> stack = new Stack<BiTree>(); while(t != null || !stack.isEmpty()){ while(t != null){ stack.add(t); t = t.leftchild; } if(!stack.isEmpty()){ BiTree node = stack.pop(); System.out.print(node.data); t = node.rightchild; } } } public void postorder2(BiTree t){ Stack<SNode> stack = new Stack<SNode>(); while(t != null){ stack.add(new SNode(t, false)); t = t.leftchild; } while(!stack.isEmpty()){ SNode snode = stack.peek(); if(snode.visited == true || snode.node.rightchild == null){ System.out.print(snode.node.data); stack.pop(); }else{ snode.visited = true; BiTree rnode = snode.node.rightchild; while(rnode != null){ stack.push(new SNode(rnode,false)); rnode = rnode.leftchild; } } } } public void leveltravel(BiTree t){ Queue<BiTree> queue = new LinkedList<BiTree>(); if(t != null) queue.add(t); while(!queue.isEmpty()){ BiTree node = queue.poll(); System.out.print(node.data); if(node.leftchild != null) queue.add(node.leftchild); if(node.rightchild != null) queue.add(node.rightchild); } } public void printatlevel(BiTree t, int level){ if(t != null){ if(level == 0){ System.out.print(t.data); return; } printatlevel(t.leftchild, level - 1); printatlevel(t.rightchild, level - 1); } } public int treedepth(BiTree t){ if(t == null) return 0; else{ int left = treedepth(t.leftchild); int right = treedepth(t.rightchild); return 1 + Math.max(left, right); } } public int treewidth(BiTree t){ if(t == null) return 0; Queue<BiTree> queue = new LinkedList<BiTree>(); queue.add(t); int curlevelwidth = 1; int lastlevelwidth = 1; int tempwidth; int width = 1; while(!queue.isEmpty()){ tempwidth = lastlevelwidth; while(tempwidth != 0){ BiTree node = queue.poll(); if(node.leftchild != null) queue.add(node.leftchild); if(node.rightchild != null) queue.add(node.rightchild); tempwidth--; } curlevelwidth = queue.size(); width = (curlevelwidth > width) ? curlevelwidth : width; lastlevelwidth = curlevelwidth; } return width; } public int treewidth2(TreeNode t) { if (t == null) return 0; LinkedList<TreeNode> queue = new LinkedList<>(); queue.add(t); int width = 1; int count; TreeNode node; while (!queue.isEmpty()) { count = queue.size(); while (count-- > 0) { node = queue.poll(); if (node.left != null) queue.add(node.left); if (node.right != null) queue.add(node.right); } width = Math.max(width, queue.size()); } return width; } public boolean nodepath(BiTree t, char value, List<BiTree> path){ if(t == null) return false; if(t.data != value){//if else if else if(nodepath(t.leftchild, value, path) || nodepath(t.rightchild, value, path)){ path.add(t); return true; }else return false; }else{ path.add(t); return true; } } public BiTree commonancestor(BiTree t, char value1, char value2){ ArrayList<BiTree> path1 = new ArrayList<BiTree>(); ArrayList<BiTree> path2 = new ArrayList<BiTree>(); nodepath(t, value1, path1); nodepath(t, value2, path2); int offset = path1.size() - path2.size(); int i = 0; int j = 0; if(offset > 0) i += offset; else if(offset < 0) j -= offset; for(; i < path1.size(); i++,j++){ if(path1.get(i).data == path2.get(j).data){ return path1.get(i); } } return null; } } public class DSTreet1 { public static void main(String[] args) { BiTree t = null; Tree tree = new Tree(); Counter c = new Counter(); //建树 t = tree.buildBiTree(t,c); System.out.println(t); //前序 tree.preorder(t); System.out.print(" | "); tree.preorder2(t); System.out.print(" | "); tree.preorder3(t); System.out.println(); //中序 tree.inorder(t); System.out.print(" | "); tree.inorder2(t); System.out.println(); //后序 tree.postorder(t); System.out.print(" | "); tree.postorder2(t); System.out.println(); //层次遍历 tree.leveltravel(t); System.out.println(); //遍历某一层 //tree.printatlevel(t, new LevelCounter(2)); tree.printatlevel(t, 3); System.out.println(); //深度 System.out.println("depth = " + tree.treedepth(t)); //宽度 System.out.println("width = " + tree.treewidth(t)); //最近公共祖先 B System.out.println("最近公共祖先LCA:" + tree.commonancestor(t, 'G', 'I').data); } }
相关文章推荐
- C#实现获取系统目录并以Tree树叉显示的方法
- C语言实现输入一颗二元查找树并将该树转换为它的镜像
- 纯jsp打造无限层次的树代码
- PHP树的深度编历生成迷宫及A*自动寻路算法实例分析
- Java Swing中的表格(JTable)和树(JTree)组件使用实例
- python数据结构树和二叉树简介
- B+树到MySQL之innoDB
- ExtJS 4 树
- 数据库表TreeView树的快速生成
- Oracle 查询所有的父节点和子节点
- 树的双亲存储:
- 树
- 二叉树(2)——遍历的非递归实现
- 二叉树(2)——遍历的非递归实现
- 二叉树(3)——三叉链表示的二叉树
- Android树状列表实现
- Sum Root to Leaf Numbers,Path Sum II
- Binary Tree Preorder, Inorder, Postorder Traversal,Flatten Binary Tree to Linked List
- Populating Next Right Pointers in Each Node I,II,Add Two Numbers,Integer to Roman
- Binary Tree Zigzag Level Order Traversal,Restore IP Addresses,Word Search,Simplify Path