前中后序遍历时根绝根节点打印的顺序定义的
代码:
package com.wangyq.datastructrue.tree;
public class BinaryTree {
public static void main(String[] args) {
//定义6个节点
Node node1 = new Node(1, "小李");
Node node2 = new Node(2, "小赵");
Node node3 = new Node(3, "小钱");
Node node4 = new Node(4, "小孙");
Node node5 = new Node(5, "小周");
Node node6 = new Node(6, "小王");
//先手动构建树
node1.setLeftNode(node2);
node1.setRightNode(node3);
node3.setLeftNode(node4);
node3.setRightNode(node5);
node5.setRightNode(node6);
Tree tree = new Tree();
tree.setRoot(node1);
//前序遍历
System.out.println("前序遍历");
tree.frontTraversal();
//中序遍历
System.out.println("中序遍历");
tree.mediumTraversal();
//后序遍历
System.out.println("后序遍历");
tree.queeTraversal();
Node temp;
//前序查询
System.out.println("前序查询");
temp = tree.frontSearch(3);
System.out.println(temp != null ? temp.toString() : "没有查到");
//中序查询
System.out.println("中序查询");
temp = tree.mediumSearch(3);
System.out.println(temp != null ? temp.toString() : "没有查到");
//后序查询
System.out.println("后序查询");
temp = tree.queeSearch(3);
System.out.println(temp != null ? temp.toString() : "没有查到");
}
}
class Tree {
private Node root;
public Node getRoot() {
return root;
}
public void setRoot(Node root) {
this.root = root;
}
/**
* 前序遍历
*/
public void frontTraversal() {
if (null == root) {
System.out.println("这是空树");
return;
}
root.frontTraversal();
}
/**
* 中序遍历
*/
public void mediumTraversal() {
if (null == root) {
System.out.println("这是空树");
return;
}
root.mediumTraversal();
}
/**
* 后序遍历
*/
public void queeTraversal() {
if (null == root) {
System.out.println("这是空树");
return;
}
root.queeTraversal();
}
public Node frontSearch(int i) {
if (null == root) {
System.out.println("这是空树");
return null;
}
return root.frontSearch(i);
}
/**
* 中序查询
*
* @param i
* @return
*/
public Node mediumSearch(int i) {
if (null == root) {
System.out.println("这是空树");
return null;
}
return root.mediumSearch(i);
}
/**
* 后序查询
*
* @param i
* @return
*/
public Node queeSearch(int i) {
if (null == root) {
System.out.println("这是空树");
return null;
}
return root.queeSearch(i);
}
}
class Node {
private int id;
private String name;
private Node leftNode;
private Node rightNode;
public Node(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Node{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Node getLeftNode() {
return leftNode;
}
public void setLeftNode(Node leftNode) {
this.leftNode = leftNode;
}
public Node getRightNode() {
return rightNode;
}
public void setRightNode(Node rightNode) {
this.rightNode = rightNode;
}
/**
* 前序遍历
*/
public void frontTraversal() {
System.out.println(this.toString());
if (null != leftNode) {
leftNode.frontTraversal();
}
if (null != rightNode) {
rightNode.frontTraversal();
}
}
/**
* 中序遍历
*/
public void mediumTraversal() {
if (null != leftNode) {
leftNode.mediumTraversal();
}
System.out.println(this.toString());
if (null != rightNode) {
rightNode.mediumTraversal();
}
}
/**
* 后序遍历
*/
public void queeTraversal() {
if (null != leftNode) {
leftNode.queeTraversal();
}
if (null != rightNode) {
rightNode.queeTraversal();
}
System.out.println(this.toString());
}
/**
* 前序查询
*/
public Node frontSearch(int i) {
System.out.println("前序查询");
if (id == i) {
return this;
}
Node temp = null;
if (null != leftNode) {
temp = leftNode.frontSearch(i);
}
if (null != temp) {
return temp;
}
if (null != rightNode) {
temp = rightNode.frontSearch(i);
}
return temp;
}
/**
* 中序查找
*
* @param i
* @return
*/
public Node mediumSearch(int i) {
Node temp = null;
if (null != leftNode) {
temp = leftNode.mediumSearch(i);
}
if (null != temp) {
return temp;
}
System.out.println("中序查找");
if (id == i) {
return this;
}
if (null != rightNode) {
temp = rightNode.mediumSearch(i);
}
return temp;
}
/**
* 后续查找
*
* @param i
* @return
*/
public Node queeSearch(int i) {
Node temp = null;
if (null != leftNode) {
temp = leftNode.queeSearch(i);
}
if (null != temp) {
return temp;
}
if (null != rightNode) {
temp = rightNode.queeSearch(i);
}
if (null != temp) {
return temp;
}
System.out.println("后续查找");
if (id == i) {
return this;
}
return temp;
}
}
运行结果:
前序遍历
Node{id=1, name=‘小李’}
Node{id=2, name=‘小赵’}
Node{id=3, name=‘小钱’}
Node{id=4, name=‘小孙’}
Node{id=5, name=‘小周’}
Node{id=6, name=‘小王’}
中序遍历
Node{id=2, name=‘小赵’}
Node{id=1, name=‘小李’}
Node{id=4, name=‘小孙’}
Node{id=3, name=‘小钱’}
Node{id=5, name=‘小周’}
Node{id=6, name=‘小王’}
后序遍历
Node{id=2, name=‘小赵’}
Node{id=4, name=‘小孙’}
Node{id=6, name=‘小王’}
Node{id=5, name=‘小周’}
Node{id=3, name=‘小钱’}
Node{id=1, name=‘小李’}
前序查询
前序查询中
前序查询中
前序查询中
Node{id=3, name=‘小钱’}
中序查询
中序查找中
中序查找中
中序查找中
中序查找中
Node{id=3, name=‘小钱’}
后序查询
后序查找中
后序查找中
后序查找中
后序查找中
后序查找中
Node{id=3, name=‘小钱’}