二叉树-前序中序后序查找

前序查找:

  1.先判断当前节点的no是否等于要查找的

  2.若相等,则返回当前节点

  3.若不等,则判断当前节点的左子节点是否为空,若不为空,则递归前序查找

  4.若左递归前序查找,找到节点,则返回,否继续判断。当前节点的右子节点是否为空,若不为空,则继续向右递归前序查找

中序查找:

  1.判断当前节点的左子节点是否为空,若不为空,则递归中序查找

  2.若找到,则返回,若没有找到,就和当前节点比较,若是,则返回当前节点,否则继续进行右递归的中序查找

  3.若右递归中序查找找到就返回,否则返回null

后序查找:

  1. 判断当前节点的左子节点是否为空,若不为空,则继续递归后序查找

  2. 若找到,就返回,若没找到,就判断当前节点的右子节点是否为空,

  若不为空,则右递归进行后序查找,若找到,就返回

  3.没找到就和当前节点进行比较,若是返回,若不是返回null

  

  1 public class BinaryTreeDemo {
  2 
  3     public static int PRECOUNT = 0;
  4     public static int INFIXCOUNT = 0;
  5     public static int POSTCOUNT = 0;
  6     public static void main(String[] args) {
  7         //先需要创建一棵二叉树
  8         BinaryTree binaryTree = new BinaryTree();
  9         EmpNode root = new EmpNode(1, "aa");
 10         EmpNode emp2 = new EmpNode(2, "bb");
 11         EmpNode emp3 = new EmpNode(3, "cc");
 12         EmpNode emp4 = new EmpNode(4, "dd");
 13         EmpNode emp5 = new EmpNode(5, "ee");
 14 
 15         //手动创建二叉树,后面学习递归的方式创建二叉树
 16         root.setLeft(emp2);
 17         root.setRight(emp3);
 18         emp3.setRight(emp4);
 19         emp3.setLeft(emp5);
 20 
 21         //测试
 22         System.out.println("前序遍历");
 23         binaryTree.setRoot(root);
 24         binaryTree.preOrder();
 25 
 26         System.out.println("中序遍历");
 27         binaryTree.setRoot(root);
 28         binaryTree.infixOrder();
 29 
 30 
 31         System.out.println("后序遍历");
 32         binaryTree.setRoot(root);
 33         binaryTree.postOrder();
 34 
 35         //前序查找
 36         //前序查找次数
 37         System.out.println("前序查找方式");
 38         EmpNode resNode = binaryTree.preOrderSearch(5);
 39         if (resNode != null){
 40             System.out.printf("找到,信息为no=%d name=%s 查询次数为%s",resNode.getNo(),resNode.getName(),PRECOUNT);
 41 
 42         }else {
 43             System.out.printf("没找到 no=%d",5);
 44         }
 45 
 46         System.out.println();
 47 
 48         //中序查找
 49         //中序查找次数
 50         System.out.println("中序查找方式");
 51         EmpNode resNode1 = binaryTree.infixOrderSearch(5);
 52         if (resNode1 != null){
 53             System.out.printf("找到,信息为no=%d name=%s 查询次数为%s",resNode1.getNo(),resNode1.getName(),INFIXCOUNT);
 54 
 55         }else {
 56             System.out.printf("没找到 no=%d",5);
 57         }
 58 
 59         System.out.println();
 60 
 61         //后序查找
 62         //后序查找次数
 63         System.out.println("后序查找方式");
 64         EmpNode resNode2 = binaryTree.postOrderSearch(5);
 65         if (resNode1 != null){
 66             System.out.printf("找到,信息为no=%d name=%s 查询次数为%s",resNode2.getNo(),resNode2.getName(),POSTCOUNT);
 67 
 68         }else {
 69             System.out.printf("没找到 no=%d",5);
 70         }
 71 
 72 
 73 
 74 
 75     }
 76 
 77 }
 78 
 79 //定义一个BinaryTree
 80 class BinaryTree {
 81     private EmpNode root;
 82 
 83     public void setRoot(EmpNode root) {
 84         this.root = root;
 85     }
 86 
 87     //前序遍历
 88     public void preOrder() {
 89         if (this.root != null) {
 90             this.root.preOrder();
 91         } else {
 92             System.out.println("二叉树为空,无法遍历");
 93         }
 94     }
 95 
 96     //中序遍历
 97     public void infixOrder() {
 98         if (this.root != null) {
 99             this.root.infixOrder();
100         } else {
101             System.out.println("二叉树为空,无法遍历");
102         }
103     }
104 
105     //后序遍历
106     public void postOrder() {
107         if (this.root != null) {
108             this.root.postOrder();
109         } else {
110             System.out.println("二叉树为空,无法遍历");
111         }
112     }
113 
114     //前序查找
115     public EmpNode preOrderSearch(int no){
116         if (root != null){
117             return root.preOrderSearch(no);
118         }else {
119             return null;
120         }
121     }
122 
123     //中序查找
124     public EmpNode infixOrderSearch(int no){
125         if (root != null){
126             return root.infixOrderSearch(no);
127         }else {
128             return null;
129         }
130     }
131     //后序查找
132     public EmpNode postOrderSearch(int no){
133         if (root != null){
134             return root.postOrderSearch(no);
135         }else {
136             return null;
137         }
138     }
139 }
140 
141 //先创建HeroNode
142 class EmpNode {
143     private int no;
144     private String name;
145     private EmpNode left;//默认为null
146     private EmpNode right;//默认为null
147 
148     public EmpNode(int no, String name) {
149         this.no = no;
150         this.name = name;
151     }
152 
153     public int getNo() {
154         return no;
155     }
156 
157     public void setNo(int no) {
158         this.no = no;
159     }
160 
161     public String getName() {
162         return name;
163     }
164 
165     public void setName(String name) {
166         this.name = name;
167     }
168 
169     public EmpNode getLeft() {
170         return left;
171     }
172 
173     public void setLeft(EmpNode left) {
174         this.left = left;
175     }
176 
177     public EmpNode getRight() {
178         return right;
179     }
180 
181     public void setRight(EmpNode right) {
182         this.right = right;
183     }
184 
185     @Override
186     public String toString() {
187         return "EmpNode{" +
188                 "no=" + no +
189                 ", name='" + name + '\'' +
190                 '}';
191     }
192     //编写前序遍历方法
193 
194     public void preOrder() {
195         System.out.println(this);//先输出父节点
196         //递归向左子树前序遍历
197         if (this.left != null) {
198             this.left.preOrder();
199         }
200         //递归向右子树前序遍历
201         if (this.right != null) {
202             this.right.preOrder();
203         }
204 
205     }
206 
207     //编写中序遍历方法
208     public void infixOrder() {
209         //递归向左子树中序遍历
210         if (this.left != null) {
211             this.left.infixOrder();//2,1,3,4
212         }
213         //输出父节点
214         System.out.println(this);
215         //递归向右子树中序遍历
216         if (this.right != null) {
217             this.right.infixOrder();
218         }
219     }
220 
221     //编写后序遍历方法
222     public void postOrder() {
223         if (this.left != null) {
224             this.left.postOrder();
225         }
226         if (this.right != null) {
227             this.right.postOrder();
228         }
229         System.out.println(this);
230     }
231 
232     /**
233      * @param no 查找no
234      * @return 若找到就返回该Node,若没找到返回Null
235      */
236     //前序遍历查找
237     public EmpNode preOrderSearch(int no) {
238         BinaryTreeDemo.PRECOUNT++;
239         //System.out.println("进入前序查找");
240 
241         //比较当前节点是不是
242         if (this.no == no) {
243             return this;
244         }
245         //判断当前节点的左子节点是否为空,若不为空,则递归前序查找
246         //若左递归前序查找,找到节点,则返回
247 
248         EmpNode resNode = null;
249         if (this.left != null) {
250             resNode = this.left.preOrderSearch(no);
251         }
252         if (resNode != null) {
253             return resNode;
254         }
255 
256         //左递归前序查找,找到节点,则返回,否继续判断
257         //当前的结点的右子节点是否为空,若为空,则继续向右递归前序查找
258         if (this.right != null) {
259             resNode = this.right.preOrderSearch(no);
260         }
261         return resNode;
262 
263     }
264 
265     //中序遍历查找
266     public EmpNode infixOrderSearch(int no) {
267 
268         //判断当前节点的左子节点是否为空,若不为空,则递归中序查找
269         EmpNode resNode = null;
270         if (this.left != null) {
271             resNode = this.left.infixOrderSearch(no);
272         }
273         if (resNode != null) {
274             return resNode;
275         }
276         BinaryTreeDemo.INFIXCOUNT++;
277         //若找到,则返回,若没找到,就和当前节点比较,若是则返回
278         if (this.no == no) {
279             return this;
280         }
281         //否则继续进行右递归的中序查找
282         if (this.right != null) {
283             resNode = this.right.infixOrderSearch(no);
284         }
285         return resNode;
286 
287     }
288 
289     //后序遍历查找
290     public EmpNode postOrderSearch(int no) {
291 
292 
293         //判断当前节点的左子节点是否为空,若不为空,则继续递归后序查找
294         EmpNode resNode = null;
295         if (this.left != null) {
296             resNode = this.left.postOrderSearch(no);
297         }
298         if (resNode != null) {//说明左子树找到
299             return resNode;
300         }
301         //若左子树没找到,则向右子树递归进行后序遍历查找
302         if (this.right != null) {
303             resNode = this.right.postOrderSearch(no);
304         }
305         if (resNode != null) {
306             return resNode;
307         }
308         //若左右子树都没找到,就比较当前节点是不是
309         BinaryTreeDemo.POSTCOUNT++;
310         if (this.no == no) {
311             return this;
312         }
313         return resNode;
314     }
315 
316 }

 

posted @ 2022-04-02 18:25  doremi429  阅读(111)  评论(0编辑  收藏  举报