首页 > 编程语言 >二叉树的前、中、后序遍历以及查找-Java实现

二叉树的前、中、后序遍历以及查找-Java实现

时间:2023-04-12 20:33:05浏览次数:38  
标签:遍历 TreeNode val return 二叉树 Java null root public

对于遍历不过多的赘述,关于查找有关的思想,关键是如何实现查找的顺序以及结果的回传;附代码

  1 package dataSrtuct;
  2 
  3 public class BinaryTreeDemo {
  4     public static void main(String[] args) {
  5         BinaryTree binaryTree=new BinaryTree();
  6         TreeNode root=new TreeNode(1,"MLQ");
  7         TreeNode node1=new TreeNode(2,"ZBB");
  8         TreeNode node2=new TreeNode(3,"MXP");
  9         TreeNode node3=new TreeNode(4,"MYP");
 10         root.setLeftNext(node1);
 11         root.setRightNext(node2);
 12         node2.setRightNext(node3);
 13         binaryTree.setRoot(root);
 14         System.out.println("遍历结果1为");
 15         binaryTree.preOrder();
 16         System.out.println("遍历结果2为");
 17         binaryTree.infixOrder();
 18         System.out.println("遍历结果3为");
 19         binaryTree.postOrder();
 20         System.out.println("查找结果1为");
 21         System.out.println(binaryTree.preOrderSearch(3)+"查找的次数为:"+TreeNode.countPre);
 22         System.out.println("查找结果2为");
 23         System.out.println(binaryTree.infixOrderSearch(3)+"查找的次数为:"+TreeNode.countInfix);
 24         System.out.println("查找结果3为");
 25         System.out.println(binaryTree.postOrderSearch(3)+"查找的次数为:"+TreeNode.countPost);
 26     }
 27 
 28 }
 29 //二叉树
 30 class BinaryTree{
 31     private TreeNode root;
 32 
 33     public TreeNode getRoot() {
 34         return root;
 35     }
 36 
 37     public void setRoot(TreeNode root) {
 38         this.root = root;
 39     }
 40     //先序遍历
 41     public void preOrder(){
 42         if (this.root!=null)
 43             this.root.preOrder();
 44         else
 45             System.out.println("二叉树为空,不能遍历");
 46     }
 47     ///中序遍历
 48     public void infixOrder(){
 49         if (this.root!=null)
 50             this.root.infixOrder();
 51         else
 52             System.out.println("二叉树为空,不能遍历");
 53     }
 54     //后序遍历
 55     public void postOrder(){
 56         if (this.root!=null)
 57             this.root.postOrder();
 58         else
 59             System.out.println("二叉树为空,不能遍历");
 60     }
 61     //前、后、中序查找
 62     //先序查找
 63     public TreeNode preOrderSearch(int val){
 64         if (this.root!=null)
 65             return this.root.preOrderSearch(val);
 66         else {
 67             System.out.println("二叉树为空,不能遍历");
 68             return null;
 69         }
 70     }
 71     //中序查找
 72     public TreeNode infixOrderSearch(int val){
 73         if (this.root!=null)
 74             return this.root.infixOrderSearch(val);
 75         else {
 76             System.out.println("二叉树为空,不能遍历");
 77             return null;
 78         }
 79     }
 80     //后序查找
 81     public TreeNode postOrderSearch(int val){
 82         if (this.root!=null)
 83             return this.root.postOrderSearch(val);
 84         else {
 85             System.out.println("二叉树为空,不能遍历");
 86             return null;
 87         }
 88 
 89     }
 90 }
 91 //树节点
 92 class TreeNode{
 93     private int val;
 94     private String name;
 95     private TreeNode leftNext;
 96     private TreeNode rightNext;
 97     public static int countPre;
 98     public static int countInfix;
 99     public static int countPost;
100     static {
101         countPre=0;
102         countInfix=0;
103         countPost=0;
104     }
105 
106     public TreeNode(int val, String name) {
107         this.val = val;
108         this.name = name;
109     }
110 
111     public int getVal() {
112         return val;
113     }
114 
115     public void setVal(int val) {
116         this.val = val;
117     }
118 
119     public String getName() {
120         return name;
121     }
122 
123     public void setName(String name) {
124         this.name = name;
125     }
126 
127     public TreeNode getLeftNext() {
128         return leftNext;
129     }
130 
131     public void setLeftNext(TreeNode leftNext) {
132         this.leftNext = leftNext;
133     }
134 
135     public TreeNode getRightNext() {
136         return rightNext;
137     }
138 
139     public void setRightNext(TreeNode rightNext) {
140         this.rightNext = rightNext;
141     }
142 
143     @Override
144     public String toString() {
145         return "TreeNode{" +
146                 "val=" + val +
147                 ", name='" + name + '\'' +
148                 '}';
149     }
150     //先序查找
151     public TreeNode preOrderSearch(int val){
152         TreeNode.countPre++;
153         if (this.val==val)
154             return this;
155         TreeNode tempNode=null;
156         if (this.leftNext!=null)
157             tempNode=this.leftNext.preOrderSearch(val);
158         if (tempNode!=null)
159             return tempNode;
160         if (this.rightNext!=null)
161             tempNode= this.rightNext.preOrderSearch(val);
162         return tempNode;
163     }
164     //中序查找
165     public TreeNode infixOrderSearch(int val){
166         TreeNode.countInfix++;
167         TreeNode tempNode=null;
168         if (this.leftNext!=null)
169             tempNode=this.leftNext.infixOrderSearch(val);
170         if (tempNode!=null)
171             return tempNode;
172         if (this.val==val)
173             return this;
174         if (this.rightNext!=null)
175             tempNode=this.rightNext.infixOrderSearch(val);
176         return tempNode;
177     }
178     //后序查找
179     public TreeNode postOrderSearch(int val){
180         TreeNode.countPost++;
181         TreeNode tempNode=null;
182         if (this.leftNext!=null)
183             tempNode=this.leftNext.postOrderSearch(val);
184         if (tempNode!=null)
185             return tempNode;
186         if (this.rightNext!=null)
187             tempNode=this.rightNext.postOrderSearch(val);
188         if (tempNode!=null)
189             return tempNode;
190         if (this.val==val)
191             return this;
192         return tempNode;
193 
194     }
195     //先序遍历
196     public void preOrder(){
197         System.out.println(this);
198         if (this.leftNext!=null)
199             this.leftNext.preOrder();
200         if (this.rightNext!=null)
201             this.rightNext.preOrder();
202     }
203     ///中序遍历
204     public void infixOrder(){
205         if (this.leftNext!=null)
206             this.leftNext.infixOrder();
207         System.out.println(this);
208         if (this.rightNext!=null)
209             this.rightNext.infixOrder();
210     }
211     //后序遍历
212     public void postOrder(){
213         if (this.leftNext!=null)
214             this.leftNext.postOrder();
215         if (this.rightNext!=null)
216             this.rightNext.postOrder();
217         System.out.println(this);
218     }
219 }

 

标签:遍历,TreeNode,val,return,二叉树,Java,null,root,public
From: https://www.cnblogs.com/Mexcellent/p/17311137.html

相关文章

  • 带大家认识 java Script
    认识JavaScript1. JavaScript简称JS2. JavaScript是开发Web页面的脚本语言3. JavaScript发布于1995年的Netscape(网景)公司4. JavaScript截止到2012年所有浏览器都完整的支持ECMAScript5.1,旧版本的浏览器至少支持ECMAscript......
  • Java: 为Word文档添加水印
    Java:为Word文档添加水印原文链接:https://www.cnblogs.com/Gia-/p/16617148.htmlJava:为Word文档添加水印添加水印是文档操作中一个非常实用的功能,通过给文档添加指定文字或图片水印既可以标识文档的状态,也可以维护文档版权,丰富其外观。在这篇文章中,我将从以下四个板块介绍......
  • Javaweb文件上传至服务器/从服务器下载
    Javaweb文件上传至服务器/从服务器下载思路图文件上传思路:也可以直接看代码判断是不是文件表单(判断form的enctype是不是="multipart/form-data"),因为只有文件表单才能上传文件创建DiskFileItemFactory对象,用于构建一个解析上传数据的工具对象创建一个解析上传......
  • JavaWeb技术栈图(web服务器+web容器是何物)
    JavaWeb技术栈图(web服务器+web容器是何物)两个重要概念web服务器+web容器什么是Web服务器?Tomcat服务器就是一个免费的开放源代码的Web应用服务器web服务实际上就是解析了客户端/浏览器发来的http请求,并将其做出一定的处理。比如说将请求头和请求体中的各个元素拆开打包成一......
  • JavaScript基础知识
    JavaScript基础知识JavaScript是什么?JavaScript是一门编程语言,可以实现很多的网页交互效果。开web页面的脚本语言JavaScript的书写位置?内部JavaScript写在body结束标签上方script里面外部JavaScript通过scriptsrc=引入js文件但是script里面不要写内容,否则会被忽略JavaSc......
  • JavaWeb之Servlet详解(以及浏览器调用 Servlet 流程分析图)
    Servlet1.什么是ServletServlet(java服务器小程序)他是由服务器端调用和执行的(一句话:是Tomcat解析和执行)他是用java语言编写的,本质就是Java类他是按照Servlet规范开发的(除了tomcat->Servletweblogic->Servlet)功能强大,可以完成几乎所有的网站功能2.开发......
  • Java byte[] 和 String互相转换
    Javabyte[]和String互相转换原文链接:https://blog.csdn.net/qq_19734597/article/details/115865372通过用例学习Java中的byte数组和String互相转换,这种转换可能在很多情况需要,比如IO操作,生成加密hash码等等。除非觉得必要,否则不要将它们互相转换,他们分别代表了不同的数据,......
  • JavaWeb中Servlet、web应用和web站点的路径细节("/"究竟代表着什么)
    JavaWeb中Servlet、web应用和web站点的路径细节("/"究竟代表着什么) 1开门见山新建一个tomcatweb项目,配置tomcat的虚拟目录,取默认值(/项目名_war_exploded)那么如果你的tomcat的默认站点(即http://localhost:8080)没有更改的话,这个项目的两个重要的根目录就出来了web站点根目......
  • Java基础语法
    注释、标识符、关键字注释注释并不会被执行,是给我们程序员看的书写注释是一个非常好的习惯Java注释的分类:单行注释://多行注释:/****/文档注释标识符标识符的作用用来表示变量名、类名、方法名、数组名和文件名等是一个有效的字符序列......
  • javaweb-学习创建servlet
    Servlet创建、声明、映射,利⽤ServletContext统计⼀个⽹站的访问总量。1)、创建一个servelet选择要用到的方法2)、编辑serveletpackagecom.cont;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletContext;importjavax.servlet.Ser......