二叉树创建:
1.创建树的结点TreeNode,包含结点的编号no,结点的名字name,左子树left,右子树right,
2.创建树,创建树只需要创建有一个根节点(TreeNode root)就ok
二叉树遍历:
1,先序遍历:先输出根节点,再递归左子树,然后递归右子树
2,中序遍历:先递归左子树,然后输入根节点,再递归右子树
3,后序遍历:先递归左子树,再递归右子树,然后输出根节点
二叉树的查找:
前序查找:1,先判断当前结点是否等于需要查找的
2,如果相等,直接返回当前结点
3,如果不等,则判断当前结点的左子节点是否为空,如果不为空,递归查询左子结点。
4,如果左子结点递归查找找到了结点,则就返回该结点,如果没有,则继续判断,直到左子节点找到值或者遇上空结点,然后判断右子节点是否为空,再递归查找
中序和后续思路也是一样
实验:
创建如下的二叉树并遍历
代码
package Tree; public class BinaryTreeDemo { public static void main(String[] args) { BinaryTree binaryTree = new BinaryTree(); //创建结点 TreeNode root = new TreeNode(1,"宋江"); TreeNode treeNode2 = new TreeNode(2,"林冲"); TreeNode treeNode3 = new TreeNode(3,"吴用"); TreeNode treeNode4 = new TreeNode(4,"关胜"); TreeNode treeNode5 = new TreeNode(5,"卢俊义"); //创建树结构 root.setLeft(treeNode2); root.setRight(treeNode3); treeNode3.setRight(treeNode5); treeNode3.setLeft(treeNode4); //挂上树 binaryTree.setTree(root); System.out.println("------------------"); System.out.println("前序遍历"); binaryTree.preOrder(); System.out.println("------------------"); System.out.println("中序遍历"); binaryTree.midOrder(); System.out.println("------------------"); System.out.println("后序遍历"); binaryTree.endOrder(); //二叉树查找先序 binaryTree.queryByNodeNo(2); //中序 binaryTree.queryNodeByMid(3); //后序 binaryTree.queryNodeByEnd(3); } } //创建树 class BinaryTree{ private TreeNode root; public void setTree(TreeNode root) { this.root = root; } public TreeNode getRoot() { return root; } //前序遍历 public void preOrder(){ if (this.root != null){ this.root.preOrder(); } else { System.out.println("空树"); } } //中序遍历 public void midOrder(){ if (this.root != null){ this.root.midOrder(); } else { System.out.println("空树"); } } //后序遍历 public void endOrder(){ if (this.root != null){ this.root.endOrder(); } else { System.out.println("空树"); } } //二叉树查找-前序 public void queryByNodeNo(int nodeNo){ if (this.root != null){ TreeNode treeNode = this.root.queryByNodeNo(nodeNo); System.out.println("查找到的树结点为 "+treeNode); }else { System.out.println("空树"); } } //中序查找 public void queryNodeByMid(int nodeNo){ if (this.root != null){ TreeNode treeNode = this.root.queryNodeByMid(nodeNo); System.out.println("中序遍历结果 "+treeNode); }else { System.out.println("空树"); } } //后序查找 public void queryNodeByEnd(int nodeNo){ if (this.root != null){ TreeNode treeNode = this.root.queryNodeByEnd(nodeNo); System.out.println("后序遍历结果 "+treeNode); }else { System.out.println("空树"); } } } //创建树结点 class TreeNode { private int no;//结点编号 private String name;//结点名称 private TreeNode left;//左结点指针 private TreeNode right;//右结点指针 public TreeNode(int no, String name) { this.no = no; this.name = name; } public int getNo() { return no; } public void setNo(int no) { this.no = no; } public String getName() { return name; } public void setName(String name) { this.name = name; } public TreeNode getLeft() { return left; } public void setLeft(TreeNode left) { this.left = left; } public TreeNode getRight() { return right; } public void setRight(TreeNode right) { this.right = right; } @Override public String toString() { return "TreeNode{" + "no=" + no + ", name='" + name + '\'' + '}'; } //前序遍历 public void preOrder() { //输出root结点 System.out.println(this); if (this.left != null) { this.left.preOrder();//递归左子结点 } if (this.right != null) { this.right.preOrder(); } } //中序遍历 public void midOrder() { //先遍历左子树 if (this.left != null) { this.left.midOrder(); } //输出根结点 System.out.println(this); if (this.right != null) { this.right.midOrder(); } } //后续遍历 public void endOrder() { if (this.left != null) { this.left.endOrder(); } if (this.right != null) { this.right.endOrder(); } //输出根结点 System.out.println(this); } //二叉树前序查找 public TreeNode queryByNodeNo(int nodeNo) { //判断当前结点和需要查找的是否相等 if (this.no == nodeNo) { return this; } //定义一个结点对象来接收查找的结果 TreeNode result = null; if (this.left != null){ //递归左子树 result = this.left.queryByNodeNo(nodeNo); } //如果结点不为空,证明找到了,如果为空,证明找到了左子树的最后一个结点,没有结果 if (result != null){ return result; } if (this.right != null){ //递归右子树 result = this.right.queryByNodeNo(nodeNo); } return result; } //中序遍历 public TreeNode queryNodeByMid(int nodeNo){ TreeNode result = null; //判断左子节点是否为空 if (this.left != null){ //不为空,递归 result = this.left.queryNodeByMid(nodeNo); } if (result != null){ return result; } //判断父节点 if (this.no == nodeNo){ return this; } //判断右子节点 if (this.right != null){ //递归 result = this.right.queryNodeByMid(nodeNo); } return result; } //后序遍历 public TreeNode queryNodeByEnd(int nodeNo){ TreeNode result = null; if (this.left != null){ result = this.left.queryNodeByMid(nodeNo); } if (result != null){ return result; } if (this.right != null){ result = this.right.queryNodeByMid(nodeNo); } if (result != null){ return result; } if (this.no == nodeNo){ return this; } return this; } }
满二叉树
完全二叉树