简单二叉树就好像链表,唯一得区别就是多了left和right两个节点
这篇文章主要讲了简单二叉树得前,中,后序遍历和按节点得前,中,后序遍历查找,首先定义一个节点类HeroNode类用于创建节点,再定义一个BinaryTree类用于创建二叉树,对树进行操作
博主今天实在是有点累了,直接上代码吧。
package two_x_tree;
public class Tree {
public static void main(String[] args) {
//创建一颗二叉树
BinaryTree binaryTree=new BinaryTree();
//创建需要得节点
HeroNode root=new HeroNode(1,"宋江");
HeroNode node2=new HeroNode(2,"吴用");
HeroNode node3=new HeroNode(3,"卢俊义");
HeroNode node4=new HeroNode(4,"林冲");
HeroNode node5=new HeroNode(5,"关胜");
root.left=node2;
root.right=node3;
node3.left=node4;
node3.right=node5;
binaryTree.setRoor(root);
//测试
//前序遍历
// System.out.println("前序遍历");
// binaryTree.preOrder();
//
// System.out.println("中序遍历");
// binaryTree.infixOrder();;
//
// System.out.println("后序遍历");
// binaryTree.postOrder();;
// //前序遍历查找
// System.out.println(binaryTree.preOrderSearch(3));
//
// //前序遍历查找
// System.out.println(binaryTree.infixOrderSearch(3));
//
// //前序遍历查找
// System.out.println(binaryTree.postOrderSearch(3));
binaryTree.delNode(5);
binaryTree.postOrder();
}
//定义二叉树
public static class BinaryTree{
private HeroNode root;
public void setRoor(HeroNode root){
this.root=root;
}
//前序遍历
public void preOrder(){
if(this.root!=null){
this.root.proOrder();
}else{
System.out.println("二叉树为空");
}
}
//中序遍历
public void infixOrder(){
if(this.root!=null){
this.root.infixOrder();
}else{
System.out.println("二叉树为空");
}
}
//后序遍历
public void postOrder(){
if(this.root!=null){
this.root.postOrder();
}else{
System.out.println("二叉树为空");
}
}
//前序遍历查找
public HeroNode preOrderSearch(int no){
if(this.root!=null){
return this.root.preOrderSearch(no);
}else{
System.out.println("二叉树为空");
return this.root;
}
}
//中序遍历查找
public HeroNode infixOrderSearch(int no){
if(this.root!=null){
return this.root.infixOrderSearch(no);
}else{
System.out.println("二叉树为空");
return this.root;
}
}
//后序遍历查找
public HeroNode postOrderSearch(int no){
if(this.root!=null){
return this.root.postOrderSearch(no);
}else{
System.out.println("二叉树为空");
return this.root;
}
}
//删除节点
public void delNode(int no){
if(root!=null){
if(root.getNo()==no){
root=null;
}else{
root.delNode(no);
}
}else{
System.out.println("空树不能删除");
}
}
}
//创建节点
public static class HeroNode{
private int no;
private String name;
private HeroNode left;
private HeroNode right;
public HeroNode(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;
}
@Override
public String toString(){
return "HeroNode[no="+no+",name="+name+"]";
}
//前序遍历
public void proOrder(){
System.out.println(this);
//递归向左前序遍历
if(this.left!=null){
this.left.proOrder();
}
//递归向右遍历
if(this.right!=null){
this.right.proOrder();
}
}
//中序遍历
public void infixOrder(){
//递归向左遍历
if(this.left!=null){
this.left.infixOrder();
}
System.out.println(this);
if(this.right!=null){
this.right.infixOrder();
}
}
//后序遍历
public void postOrder(){
if(this.left!=null){
this.left.postOrder();
}
if(this.right!=null){
this.right.postOrder();
}
System.out.println(this);
}
//按节点搜索---前序遍历
public HeroNode preOrderSearch(int no){
//先判断当前节点
if(this.no==no){
return this;
}
//定义一个辅助变量
HeroNode resNode=null;
//向左递归
if(this.left!=null){
resNode=this.left.preOrderSearch(no);
}
//若找到,直接返回
if(resNode!=null){
return resNode;
}
//向右递归
if(this.right!=null){
resNode=this.right.preOrderSearch(no);
}
return resNode;
}
//按节点搜索---中序遍历
public HeroNode infixOrderSearch(int no){
HeroNode resNode=null;
if(this.left!=null){
resNode=this.left.infixOrderSearch(no);
}
if(resNode!=null){
return resNode;
}
if(this.no==no){
return this;
}
if(this.right!=null){
resNode=this.right.infixOrderSearch(no);
}
return resNode;
}
//按节点搜索---后序遍历
public HeroNode postOrderSearch(int no){
HeroNode resNode=null;
if(this.left!=null){
resNode=this.left.postOrderSearch(no);
}
if(resNode.no==no){
return resNode;
}
if(this.right!=null){
resNode=this.right.postOrderSearch(no);
}
if(resNode.no==no){
return resNode;
}
if(this.no==no){
return this;
}
return resNode;
}
//删除节点
public void delNode(int no){
if(this.left!=null&&this.left.no==no){
this.left=null;
return;
}
if(this.right!=null&&this.right.no==no){
this.right=null;
return;
}
if(this.left!=null){
this.left.delNode(no);
}
if(this.right!=null){
this.right.delNode(no);
}
}
}
}