关于二叉树的所有 前序、中序、后序、层次遍历
2012-11-19 21:49
381 查看
package test; import java.util.*; public class BinaryTree { protected Node root; public BinaryTree(Node root) { this.root = root; } public Node getRoot() { return root; } /** 构造树 */ public static Node init() { Node a = new Node('A'); Node b = new Node('B', null, a); Node c = new Node('C'); Node d = new Node('D', b, c); Node e = new Node('E'); Node f = new Node('F', e, null); Node g = new Node('G', null, f); Node h = new Node('H', d, g); return h;// root } /** 访问节点 */ public static void visit(Node p) { System.out.print(p.getKey() + " "); } /** 递归实现前序遍历 */ static void preorder(Node p) { if (p != null) { visit(p); preorder(p.getLeft()); preorder(p.getRight()); } } /** 递归实现中序遍历 */ static void inorder(Node p) { if (p != null) { inorder(p.getLeft()); visit(p); inorder(p.getRight()); } } /** 递归实现后序遍历 */ static void postorder(Node p) { if (p != null) { postorder(p.getLeft()); postorder(p.getRight()); visit(p); } } /** 层次遍历*/ static void levelorder(Node p){ if(p==null) return; Queue< Node> queue=new LinkedList< Node>(); queue.offer(p); while(queue.size()>0){ Node temp=queue.poll(); visit(temp); if(temp.getLeft()!=null){ queue.offer(temp.getLeft()); } if(temp.getRight()!=null){ queue.offer(temp.getRight()); } } } // 求二叉树的高度 static int height(Node tree) { if (tree == null) return 0; else { int leftTreeHeight = height(tree.getLeft()); int rightTreeHeight = height(tree.getRight());; return leftTreeHeight > rightTreeHeight ? leftTreeHeight + 1: rightTreeHeight + 1; } } // 求二叉树的结点总数 static int nodes(Node tree) { if (tree == null) return 0; else { int left = nodes(tree.getLeft()); int right = nodes(tree.getRight()); return left + right + 1; } } // 求二叉树叶子节点的总数 static int leaf(Node tree) { if (tree == null) return 0; else { int left = leaf(tree.getLeft()); int right = leaf(tree.getRight()); if (tree.getLeft() == null && tree.getRight() == null) return left + right + 1; else return left + right; } } //将二叉树所有结点的左右子树交换 static void swapTree(Node root){ if(root != null) { Node tmp = root.getLeft(); root.setLeft(root.getRight()); root.setRight(tmp); swapTree(root.getLeft()); swapTree(root.getRight()); } } /** * getLeafNodes: 递归求解给定二叉树的所有叶子结点 * @param root 给定二叉树的根结点 * @param leaflist 给定二叉树的所有叶子结点 */ static void getLeafNodes(Node root, List< Node> leaflist) { if (root != null) { if (root.getLeft() == null && root.getRight() == null) { leaflist.add(root); return ; } getLeafNodes(root.getLeft(), leaflist); getLeafNodes(root.getRight(), leaflist); } } /** * longestPath: 递归求解给定二叉树的一条最长路径 如果有多条,输出其中一条 * @param root 给定二叉树的根结点 * @param longestPath 存放二叉树的最长路径上的结点 */ static void longestPath(Node root, List< Node> longestPath) { if (root != null) { longestPath.add(root); if (root.getLeft() == null && root.getRight() == null) { // 左右子树均空 return ; } List< Node> leftLongestPath = new ArrayList< Node>(); List< Node> rightLongestPath = new ArrayList< Node>(); longestPath(root.getLeft(), leftLongestPath); longestPath(root.getRight(), rightLongestPath); if (leftLongestPath.size() >= rightLongestPath.size()) { longestPath.addAll(leftLongestPath); } else if (leftLongestPath.size() < rightLongestPath.size()) { longestPath.addAll(rightLongestPath); } } } /** * @param args */ public static void main(String[] args) { BinaryTree tree = new BinaryTree(init()); System.out.print(" 前序遍历:"); preorder(tree.getRoot()); System.out.println(); System.out.print(" 中序遍历:"); inorder(tree.getRoot()); System.out.println(); System.out.print(" 后序遍历:"); postorder(tree.getRoot()); System.out.println(); System.out.println(); System.out.println("层次遍历"); levelorder(tree.getRoot()); System.out.println(); System.out.println(); System.out.println("叶子结点数"); System.out.println(leaf(tree.getRoot())); System.out.println("总结点数"); System.out.println(nodes(tree.getRoot())); System.out.println("树的高度"); System.out.println(height(tree.getRoot())); } }
相关文章推荐
- (1)建立二叉树的二叉链表。 (2)写出对用二叉链表存储的二叉树进行先序、中序和后序遍历的递归和非递归算法。 (3)写出对用二叉链表存储的二叉树进行层次遍历算法。 (4)求二叉树的所有叶子及结点总数。
- 二叉树的建树、遍历(先序、中序、后序、层次)(递归和非递归)--Java实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- [置顶] 数据结构之 二叉树的构造与遍历(先序,中序,后序,层次)
- 二叉树的各种操作的(递归非递归)的实现,如(递归非递归)先序后序中序层次遍历 二叉树的高度 叶子节点数,所有节点数
- 二叉树的的所有操作——转化为静态二叉链表,求深度,求先序排列,求中序排列,求后序排列,求层次遍历,求度为0.1.2的节点个数
- 二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树的先序、中序、后序、层次遍历的递归和非递归解法
- 二叉树:前序、中序、后序、层次遍历及实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 用非递归实现二叉树的前序、中序、后序、层次遍历,用递归实现查找、统计个数、比较、求深度
- 二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- java语言实现二叉树的前序、中序与后序遍历(递归与非递归) 层次遍历
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- python实现二叉树的建立以及遍历(递归前序、中序、后序遍历,队栈前序、中序、后序、层次遍历)
- 二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 用java实现二叉树的前序、中序、后序、层次遍历(递归和非递归版)
- 二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树中结构体的应用,以及先序,中序,后序,层次遍历的输出
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现