二叉树基本操作
2015-06-22 18:32
381 查看
package java_study.JianZhiOffer; /** * Created by ethan on 2015/6/21. */ public class TreeNode { char value; TreeNode lchild; TreeNode rchild; public TreeNode() { this.value = 0; this.lchild = null; this.rchild = null; } public TreeNode(char value){ this.value = value; this.lchild = null; this.rchild = null; } public TreeNode(char value, TreeNode lchild, TreeNode rchild){ this.value = value; this.lchild = lchild; this.rchild = rchild; } public char getValue() { return value; } public void setValue(char value) { this.value = value; } public TreeNode getLchild() { return lchild; } public void setLchild(TreeNode lchild) { this.lchild = lchild; } public TreeNode getRchild() { return rchild; } public void setRchild(TreeNode rchild) { this.rchild = rchild; } } package java_study.JianZhiOffer; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; /** * Created by ethan on 2015/6/21. */ public class TreeUtil { // 接收前序和中序创建二叉树 public static TreeNode buildTree(String pre_str, int pre_start, int pre_end, String in_str, int in_start, int in_end){ if (pre_str==null || in_str==null) return null; if (pre_start>pre_end || in_start>in_end) return null; TreeNode root = new TreeNode(pre_str.charAt(pre_start)); int index_mid = in_start; for (; index_mid<in_end; index_mid++) if (in_str.charAt(index_mid) == pre_str.charAt(pre_start)) break; if (index_mid > in_start) root.setLchild(buildTree(pre_str, pre_start+1, pre_start+index_mid-in_start, in_str, in_start, index_mid-1)); if (index_mid < in_end) root.setRchild(buildTree(pre_str, pre_start+index_mid-in_start+1, pre_end, in_str, index_mid+1, in_end)); return root; } // 前序递归遍历 public static void preOrder(TreeNode root){ if (root != null){ System.out.print(root.getValue() + " "); preOrder(root.getLchild()); preOrder(root.getRchild()); } } // 中序递归遍历 public static void inOrder(TreeNode root){ if (root != null ){ inOrder(root.getLchild()); System.out.print(root.getValue() + " "); inOrder(root.getRchild()); } } // 后序递归遍历 public static void postOrder(TreeNode root){ if (root!=null){ postOrder(root.getLchild()); postOrder(root.getRchild()); System.out.print(root.getValue() + " "); } } // 前序非递归遍历, 全压栈 public static void preOrderNoRecursive_1(TreeNode root){ if (root == null) return; Stack<TreeNode> stack = new Stack<TreeNode>(); stack.push(root); while (!stack.isEmpty()){ TreeNode tmp = stack.pop(); System.out.print(tmp.getValue() + " "); if (tmp.getRchild()!=null){ stack.push(tmp.getRchild()); } if (tmp.getLchild()!=null){ stack.push(tmp.getLchild()); } } System.out.println(); } // 前序非递归遍历, 全压栈 public static void preOrderNoRecursive_2(TreeNode root){ if (root==null) return; Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode tmp = root; while (tmp!=null || !stack.isEmpty()){ while (tmp!=null){ System.out.print(tmp.value+" "); stack.push(tmp); tmp=tmp.getLchild(); } if (!stack.isEmpty()){ tmp = stack.pop().getRchild(); } } System.out.println(); } // 前序非递归遍历 public static void preOrderNoRecursive(TreeNode root){ if (root==null) return; Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode tmp = root; while (tmp!=null){ System.out.print(tmp.value+" "); stack.push(tmp); tmp=tmp.getLchild(); } while (!stack.isEmpty()){ TreeNode top = stack.pop(); TreeNode tmp_1 = top.getRchild(); while (tmp_1!=null){ System.out.print(tmp_1.value+" "); stack.push(tmp_1); tmp_1=tmp_1.getLchild(); } } System.out.println(); } // 中序非递归遍历 public static void inOrderNoRecursive_1(TreeNode root){ if (root==null) return; Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode tmp = root; while (tmp!=null || !stack.isEmpty()){ while (tmp!=null){ stack.push(tmp); tmp = tmp.getLchild(); } if (!stack.isEmpty()){ TreeNode myNode = stack.pop(); System.out.print(myNode.getValue()+" "); tmp = myNode.getRchild(); } } System.out.println(); } // 中序非递归遍历 public static void inOrderNoRecursive(TreeNode root){ if (root==null) return; Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode tmp = root; while (tmp!=null){ stack.push(tmp); tmp = tmp.getLchild(); } while (!stack.isEmpty()){ TreeNode top = stack.pop(); System.out.print(top.value + " "); TreeNode tmp_1 = top.getRchild(); while (tmp_1!=null){ stack.push(tmp_1); tmp_1=tmp_1.getLchild(); } } System.out.println(); } // 后序非递归遍历 public static void postOrderNoRecursive(TreeNode root){ if (root==null) return; TreeNode tmp = root; TreeNode preVisited = null; Stack<TreeNode> stack = new Stack<TreeNode>(); while (tmp!=null || !stack.isEmpty()){ while (tmp!=null){ stack.push(tmp); tmp = tmp.getLchild(); } tmp = stack.peek(); if (tmp.getRchild()==null || tmp.getRchild()==preVisited){ System.out.print(tmp.getValue()+ " "); preVisited = tmp; stack.pop(); tmp=null; }else{ tmp = tmp.getRchild(); } } System.out.println(); } // 层次遍历 利用队列 public static void levelTraverse(TreeNode root){ if (root == null) return; Queue<TreeNode> stack = new LinkedList<TreeNode>(); stack.add(root); while (!stack.isEmpty()){ TreeNode cur = stack.remove(); System.out.print(cur.getValue() + " "); if (cur.getLchild()!=null) stack.add(cur.getLchild()); if (cur.getRchild()!=null){ stack.add(cur.getRchild()); } } System.out.println(); } // 二叉树的高度 public static int getDepth(TreeNode root){ if (root==null) return 0; int depth_l = getDepth(root.getLchild()); int depth_r = getDepth(root.getRchild()); return (depth_l > depth_r ? depth_l : depth_r)+1; } // 二叉树的结点的个数 public static int countNodes(TreeNode root){ if (root==null) return 0; return 1+countNodes(root.getLchild()) + countNodes(root.getRchild()); } } package java_study.JianZhiOffer; import org.junit.Test; /** * Created by ethan on 2015/6/21. */ public class TestTreeUtil { String pre_str = "abdhecfg"; String in_str = "hdbeafcg"; public TreeNode root = TreeUtil.buildTree(pre_str, 0, pre_str.length()-1, in_str, 0, in_str.length()-1); // 手动初始化 // public TreeNode init(){ // TreeNode node0 = new TreeNode('a'); // TreeNode node1 = new TreeNode('b'); // TreeNode node2 = new TreeNode('c'); // TreeNode node3 = new TreeNode('d'); // TreeNode node4 = new TreeNode('e'); // TreeNode node5 = new TreeNode('f'); // TreeNode node6 = new TreeNode('g'); // TreeNode node7 = new TreeNode('h'); // node0.setLchild(node1); // node0.setRchild(node2); // node1.setLchild(node3); // node1.setRchild(node4); // node2.setLchild(node5); // node2.setRchild(node6); // node3.setLchild(node7); // return node0; // } @Test public void test3(){ System.out.println(TreeUtil.getDepth(root)); System.out.println(TreeUtil.countNodes(root)); } @Test public void test2(){ TreeUtil.preOrderNoRecursive(root); TreeUtil.preOrderNoRecursive_1(root); TreeUtil.preOrderNoRecursive_2(root); TreeUtil.inOrderNoRecursive(root); TreeUtil.inOrderNoRecursive_1(root); TreeUtil.postOrderNoRecursive(root); TreeUtil.levelTraverse(root); } @Test public void test1(){ String pre_str = "abdhecfg"; String in_str = "hdbeafcg"; TreeNode root = TreeUtil.buildTree(pre_str, 0, pre_str.length()-1, in_str, 0, in_str.length()-1); TreeUtil.postOrder(root); } @Test public void test(){ TreeUtil.preOrder(root); System.out.println(); TreeUtil.inOrder(root); System.out.println(); TreeUtil.postOrder(root); System.out.println(); } } a1b4
相关文章推荐
- 深入遍历二叉树的各种操作详解(非递归遍历)
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Symmetric Tree
- 二叉树的层次遍历
- 二叉树的非递归遍历
- 从上往下打印二叉树(面试题 23)
- 二叉树的非递归遍历及算法分析
- leetCode解题报告之Binary Tree Postorder Traversal
- 【LeetCode】Populating Next Right Pointers in Each Node II典型错误示例与分析
- 二叉树题目
- oracle层次查询-查询每个学生所选择的课程列表(同一行显示)
- 编写按层次遍历二叉树的算法
- 数据结构 - 二叉树的遍历
- Populating Next Right Pointers in Each Node II
- 【数据结构】二叉树的层次遍历
- 【数据结构】二叉树的层次遍历2
- 层次遍历二叉树
- 二叉树非递归遍历C语言实现