二叉树
基本介绍
二叉树遍历
图解
代码实现
package com.company.tree;
import com.sun.source.tree.IfTree;
import com.sun.tools.javac.Main;
/**
* @Function :
* date 2021/5/19 - 23:06
* How :
*/
public class BinaryTree {
public static void main(String[] args) {
HeroNode zhangsan = new HeroNode(1, "zhangsan");
HeroNode zhangsan1 = new HeroNode(2, "zhangsan1");
HeroNode zhangsan2 = new HeroNode(3, "zhangsan2");
HeroNode zhangsan3 = new HeroNode(4, "zhangsan3");
HeroNode zhangsan4 = new HeroNode(5, "zhangsan4");
zhangsan.leftNode = zhangsan1;
zhangsan.rightNode = zhangsan2;
zhangsan2.leftNode = zhangsan3;
zhangsan2.rightNode = zhangsan4;
binaryTree1 binaryTree = new binaryTree1(zhangsan);
binaryTree.preOrder();
}
}
class binaryTree1{
private HeroNode root;
public binaryTree1(HeroNode root) {
this.root = root;
}
public void preOrder(){
if (root!=null){
root.preOrder();
}
}
}
//创建hero节点
class HeroNode{
public int id;
public String name;
public HeroNode leftNode;
public HeroNode rightNode;
public HeroNode(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "HeroNode{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
//前序遍历
public void preOrder(){
System.out.println(this.toString());
if (this.leftNode!=null){
this.leftNode.preOrder();
}
if (this.rightNode!=null){
this.rightNode.preOrder();
}
}
//中序遍历
public void inOrder(){
if (this.leftNode!=null){
this.leftNode.inOrder();
}
System.out.println(this.toString());
if (this.rightNode!=null){
this.rightNode.inOrder();
}
}
//后序遍历
public void postOrder(){
System.out.println(this.toString());
if (this.leftNode!=null){
this.leftNode.postOrder();
}
if (this.rightNode!=null){
this.rightNode.postOrder();
}
}
}
二叉树查找
代码实现
package com.company.tree;
import com.sun.source.tree.IfTree;
import com.sun.tools.javac.Main;
/**
* @Function :
* date 2021/5/19 - 23:06
* How :
*/
public class BinaryTree {
public static void main(String[] args) {
HeroNode zhangsan = new HeroNode(1, "zhangsan");
HeroNode zhangsan1 = new HeroNode(2, "zhangsan1");
HeroNode zhangsan2 = new HeroNode(3, "zhangsan2");
HeroNode zhangsan3 = new HeroNode(4, "zhangsan3");
HeroNode zhangsan4 = new HeroNode(5, "zhangsan4");
zhangsan.leftNode = zhangsan1;
zhangsan.rightNode = zhangsan2;
zhangsan2.leftNode = zhangsan3;
zhangsan2.rightNode = zhangsan4;
binaryTree1 binaryTree = new binaryTree1(zhangsan);
binaryTree.preOrder();
HeroNode searchId = binaryTree.preOrderSearch(3);
System.out.println(searchId);
}
}
class binaryTree1{
private HeroNode root;
public binaryTree1(HeroNode root) {
this.root = root;
}
public void preOrder(){
if (root!=null){
root.preOrder();
}
}
public HeroNode preOrderSearch(int no){
if (root!=null){
return root.preOrderSearch(no);
}
throw new RuntimeException("root节点为空");
}
}
//创建hero节点
class HeroNode{
public int id;
public String name;
public HeroNode leftNode;
public HeroNode rightNode;
public HeroNode(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "HeroNode{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
//前序遍历
public void preOrder(){
System.out.println(this.toString());
if (this.leftNode!=null){
this.leftNode.preOrder();
}
if (this.rightNode!=null){
this.rightNode.preOrder();
}
}
//中序遍历
public void inOrder(){
if (this.leftNode!=null){
this.leftNode.inOrder();
}
System.out.println(this.toString());
if (this.rightNode!=null){
this.rightNode.inOrder();
}
}
//后序遍历
public void postOrder(){
System.out.println(this.toString());
if (this.leftNode!=null){
this.leftNode.postOrder();
}
if (this.rightNode!=null){
this.rightNode.postOrder();
}
}
//前序查找
public HeroNode preOrderSearch(int no){
HeroNode heroNode = null;
if (this.id == no){
System.out.println("--找到--");
heroNode = this;
return this;
}
if (this.leftNode!=null){
heroNode = this.leftNode.preOrderSearch(no);
if (heroNode!=null){
return heroNode;
}
}
if(this.rightNode!=null){
heroNode = this.rightNode.preOrderSearch(no);
if (heroNode!=null){
return heroNode;
}
}
return heroNode;
}
//中序查找
public HeroNode inOrderSearch(int no){
HeroNode heroNode = null;
if (this.leftNode!=null){
heroNode = this.leftNode.preOrderSearch(no);
if (heroNode!=null){
return heroNode;
}
}
if (this.id == no){
System.out.println("--找到--");
heroNode = this;
return this;
}
if(this.rightNode!=null){
heroNode = this.rightNode.preOrderSearch(no);
if (heroNode!=null){
return heroNode;
}
}
return heroNode;
}
//后查找
public HeroNode postOrderSearch(int no){
HeroNode heroNode = null;
if (this.leftNode!=null){
heroNode = this.leftNode.preOrderSearch(no);
if (heroNode!=null){
return heroNode;
}
}
if(this.rightNode!=null){
heroNode = this.rightNode.preOrderSearch(no);
if (heroNode!=null){
return heroNode;
}
}
if (this.id == no){
System.out.println("--找到--");
heroNode = this;
return this;
}
return heroNode;
}
}
二叉树删除
代码实现
package com.company.tree;
import com.sun.source.tree.IfTree;
import com.sun.tools.javac.Main;
/**
* @Function :
* date 2021/5/19 - 23:06
* How :
*/
public class BinaryTree {
public static void main(String[] args) {
HeroNode zhangsan = new HeroNode(1, "zhangsan");
HeroNode zhangsan1 = new HeroNode(2, "zhangsan1");
HeroNode zhangsan2 = new HeroNode(3, "zhangsan2");
HeroNode zhangsan3 = new HeroNode(4, "zhangsan3");
HeroNode zhangsan4 = new HeroNode(5, "zhangsan4");
zhangsan.leftNode = zhangsan1;
zhangsan.rightNode = zhangsan2;
zhangsan2.leftNode = zhangsan3;
zhangsan2.rightNode = zhangsan4;
binaryTree1 binaryTree = new binaryTree1(zhangsan);
binaryTree.preOrder();
HeroNode searchId = binaryTree.preOrderSearch(3);
System.out.println("查找节点的信息为"+searchId);
binaryTree.delNOde(3);
binaryTree.preOrder();
}
}
class binaryTree1{
private HeroNode root;
public binaryTree1(HeroNode root) {
this.root = root;
}
public void preOrder(){
if (root!=null){
root.preOrder();
}
}
public HeroNode preOrderSearch(int no){
if (root!=null){
return root.preOrderSearch(no);
}
throw new RuntimeException("root节点为空");
}
public void delNOde(int id){
if (root!=null){
if (root.id==id){
root=null;
}
root.delNode(id);
}
}
}
//创建hero节点
class HeroNode{
public int id;
public String name;
public HeroNode leftNode;
public HeroNode rightNode;
public HeroNode(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "HeroNode{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
//前序遍历
public void preOrder(){
System.out.println(this.toString());
if (this.leftNode!=null){
this.leftNode.preOrder();
}
if (this.rightNode!=null){
this.rightNode.preOrder();
}
}
//中序遍历
public void inOrder(){
if (this.leftNode!=null){
this.leftNode.inOrder();
}
System.out.println(this.toString());
if (this.rightNode!=null){
this.rightNode.inOrder();
}
}
//后序遍历
public void postOrder(){
System.out.println(this.toString());
if (this.leftNode!=null){
this.leftNode.postOrder();
}
if (this.rightNode!=null){
this.rightNode.postOrder();
}
}
//前序查找
public HeroNode preOrderSearch(int no){
HeroNode heroNode = null;
if (this.id == no){
System.out.println("--找到--");
heroNode = this;
return this;
}
if (this.leftNode!=null){
heroNode = this.leftNode.preOrderSearch(no);
if (heroNode!=null){
return heroNode;
}
}
if(this.rightNode!=null){
heroNode = this.rightNode.preOrderSearch(no);
if (heroNode!=null){
return heroNode;
}
}
return heroNode;
}
//中序查找
public HeroNode inOrderSearch(int no){
HeroNode heroNode = null;
if (this.leftNode!=null){
heroNode = this.leftNode.preOrderSearch(no);
if (heroNode!=null){
return heroNode;
}
}
if (this.id == no){
System.out.println("--找到--");
heroNode = this;
return this;
}
if(this.rightNode!=null){
heroNode = this.rightNode.preOrderSearch(no);
if (heroNode!=null){
return heroNode;
}
}
return heroNode;
}
//后查找
public HeroNode postOrderSearch(int no){
HeroNode heroNode = null;
if (this.leftNode!=null){
heroNode = this.leftNode.preOrderSearch(no);
if (heroNode!=null){
return heroNode;
}
}
if(this.rightNode!=null){
heroNode = this.rightNode.preOrderSearch(no);
if (heroNode!=null){
return heroNode;
}
}
if (this.id == no){
System.out.println("--找到--");
heroNode = this;
return this;
}
return heroNode;
}
//删除节点
public void delNode(int id){
if (this.leftNode!=null && this.leftNode.id==id){
this.leftNode=null;
return;
}
if (this.rightNode!=null && this.rightNode.id==id){
this.rightNode=null;
return;
}
if (this.leftNode!=null){
this.leftNode.delNode(id);
}
if (this.rightNode!=null){
this.rightNode.delNode(id);
}
}
}