树的遍历
Posted guosai1500581464
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了树的遍历相关的知识,希望对你有一定的参考价值。
package july.wild.All_Data_Structure_Impl; import java.util.Stack; /**使用递归一定要有返回值 * @author 郭赛 * @Company Huawei */ public class TreeNode { TreeNode left; TreeNode right; int data; public TreeNode(int data){ this.data = data; } public TreeNode(){ } TreeNode insertTreeNode(int data,TreeNode root){ if(root == null) { return new TreeNode(data);//新建节点 } if(data < root.data) root.left = insertTreeNode(data,root.left); else if(data > root.data) root.right = insertTreeNode(data,root.right); else ; //如果插入的数字和树中的元素相等,什么都不做 return root;//让后来的插入也能从root开始 /* if(root == null){ root = new TreeNode(data);//这里root.left每次都是赋值新的值 return root; } if(data < root.data) return insertTreeNode(data,root.left);//这样并没有给root.left赋值,root.left节点不存在 else if(data > root.data) return insertTreeNode(data,root.right);*/ } TreeNode findMin(TreeNode root){ if(root == null) return null; else if(root.left == null) return root; return findMin(root.left); } TreeNode findMax(TreeNode root){ if(root == null) return null; else if(root.right == null) return root; return findMax(root.right); } TreeNode deleteNode(TreeNode root,int data){//删除节点 if(root == null) //树为空,直接返回 return null; if(data < root.data) root.left = deleteNode(root.left,data); else if(data > root.data) root.right = deleteNode(root.right,data); else if(root.right != null &&root.left != null) { int element = findMin(root.right).data; root.data = element; deleteNode(root.right,element); } else if(root.right != null) root = root.right; else root = root.left; return root; } void firstPrint(TreeNode root){//先序遍历使用递归 if(root == null) return; else { System.out.println(root.data); firstPrint(root.left); firstPrint(root.right); } } void firstPrintImpl_stack(TreeNode root){ Stack<TreeNode> stack = new Stack<>();//声明一个堆栈 while(root != null || !stack.isEmpty()) { while (root != null) { System.out.println(root.data);//先序 stack.push(root); root = root.left; } if(!stack.isEmpty()){//栈不为空 root = stack.pop(); root = root.right; } } } void secondPrint(TreeNode root){//递归中序遍历 if(root != null){ secondPrint(root.left); System.out.println(root.data); secondPrint(root.right); } else return;//结束一次递归 } void secondPrintImpl_stack(TreeNode root) {//中序遍历stack实现 Stack<TreeNode> stack = new Stack<>(); while(root != null || !stack.isEmpty()){ while(root != null){ stack.push(root); root = root.left;//迭代到最左边 } if(!stack.isEmpty()) { root = stack.pop(); System.out.println(root.data); root = root.right; } } } /** * 后序遍历的堆栈实现,实现错误了,需要添加一个flag用作设置当前节点是否被打印了 * @param root */ void lastPrintImpl_stack(TreeNode root){ Stack<TreeNode> stack = new Stack<>(); while(root != null || !stack.isEmpty()){ while(root != null){ stack.push(root); root = root.left; } if(!stack.isEmpty()){ root = stack.pop(); if(root.right != null){//如果当前节点的右节点不为空 stack.push(root); root = root.right; }else { System.out.println(root.data); root = null; //如果右节点为空,直接输出当前节点 } } } } public static void main(String[] args) { TreeNode root = new TreeNode(5); for (int i = 0; i < 9; i++) { root.insertTreeNode(i,root); } while(root != null){ System.out.println(root.data); root = root.left; } /* System.out.println(root.findMin(root).data); System.out.println(root.findMax(root).data); System.out.println("================="); root.firstPrint(root); System.out.println("============"); System.out.println(root.insertTreeNode(4,root).data); root.firstPrint(root);*/ /* root.firstPrint(root); System.out.println("====================="); root.firstPrintImpl_stack(root); *//*root.deleteNode(root,3);//这样值传递不会改变root的值,将root的指针传递给deleteNode函数,然后通过指针访问tree root.firstPrint(root);*//* System.out.println("中序遍历的结果"); //root.secondPrint(root); root.secondPrintImpl_stack(root);*/ System.out.println("后序遍历的结果"); //root.lastPrintImpl_stack(root); } }
以上是关于树的遍历的主要内容,如果未能解决你的问题,请参考以下文章