package day17;
public class binaryTreedemo {
public static void main(String[] args) {
// 创建需要的节点
heronode root = new heronode(1, "宋江");
heronode node2 = new heronode(2, "吴用");
heronode node3 = new heronode(3, "卢俊义");
heronode node4 = new heronode(4, "林冲");
//创建一颗二叉树
// 创建树的时候就加入他的root节点
BinaryTree binaryTree = new BinaryTree(root);
//说明,我们先手动创建二叉树,后面学习递归的方式
root.setLeft(node2);
node2.setLeft(node3);
node2.setRight(node4);
//binaryTree.setRoot(root);
// 测试
// System.out.println("前序遍历");
// binaryTree.preOrder();
// System.out.println("中序遍历");
// binaryTree.infixOrder();
// System.out.println("后序遍历");
// binaryTree.postOrder();
// System.out.println("前序查找");
// heronode preoedersearch = binaryTree.preoedersearch(4);
// System.out.println(preoedersearch);
// System.out.println("中序查找");
// System.out.println(binaryTree.infixordersearch(4));
// System.out.println("后续查找");
// System.out.println(binaryTree.postordersearch(4));
System.out.println("删除节点");
binaryTree.deltreenode(2);
binaryTree.preOrder();
}
// 思考:流程:先创建一个二叉树,然后new一个binarytree对象,这个对象包含了heronode(节点)属性,
// 调用他的preorder方法,preoeder方法
}
// 定义一个树,树是由节点组成的,每个树都有一个root节点。
class BinaryTree{
private heronode root;
public BinaryTree(heronode root) {
this.root = root;
}
public void setRoot(heronode root) {
this.root = root;
}
// 前序遍历
public void preOrder(){
if(this.root!=null){
this.root.preOrder();
}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 preoedersearch(int no){
if (root!=null){
return root.preselect(no);
}else {
return null;
}
}
// 中序查找
public heronode infixordersearch(int no){
if (root!=null){
return root.infixselect(no);
}else {
return null;
}
}
// 后序查找
public heronode postordersearch(int no){
if (root!=null){
return root.postselet(no);
}else {
return null;
}
}
//----------------------------------------删除-----------------------------------------------------
public void deltreenode(int no){
if (root!=null){
// 只有一个root
if (root.getNo()==no){
root=null;
}else {
// 递归删除
root.delnode(no);
}
}else {
System.out.println("空树");
}
}
}
//--------------------------------------------------heronode----------------------------------------
//定义节点,有no,name,左节点,右节点
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;
}
public heronode getLeft() {
return left;
}
public void setLeft(heronode left) {
this.left = left;
}
public heronode getRight() {
return right;
}
public void setRight(heronode right) {
this.right = right;
}
@Override
public String toString() {
return "heronode{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
//前序遍历
//先父节点 后左子树, 然后右子树。
public void preOrder(){
System.out.println(this); //this表示的是当前的对象,即节点。当new一个节点的时候,则指的是他本身。
//先输出父节点
//递归左子树
if (this.left!=null){
this.left.preOrder();
}
//递归向右子树
if (this.right!=null){
this.right.preOrder();
}
}
// 中序遍历
// 先左后中后右。
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 preselect(int no){
System.out.println("前序查找的次数");
if(this.no==no){ //当前的节点是否为要查找的节点
return this;
}
//判断当前节点的左节点是否为空,如果不为空,递归前序查找(首先判断是否相等,然后继续向左寻找直到left为空退出)
//
heronode resnode=null;
if (this.left!=null){
resnode=this.left.preselect(no);
}
if (resnode!=null){ //说明左边找到了
return resnode;
}
if (this.right!=null){
resnode=this.right.preselect(no);
}
return resnode;
}
//中序查找
public heronode infixselect(int no){
System.out.println("中序查找的次数");
heronode resnode =null;
if (this.left!=null){
resnode=this.left.infixselect(no);
}
if (resnode!=null){
return resnode;
}
if (this.no==no){
return this;
}
if (this.right!=null){
resnode = this.right.infixselect(no);
}
return resnode;
}
//后序查找
public heronode postselet(int no){
System.out.println("后续查找的次数");
heronode resnode =null;
if (this.left!=null){
resnode=this.left.postselet(no);
}
if (resnode!=null){
return resnode;
}
if (this.right!=null){
resnode=this.right.postselet(no);
}
if (resnode!=null){
return resnode;
}
if (this.no==no){
return this;
}
return resnode;
}
//-----------------------------------------删除-------------------------------------------------------
public void delnode(int no){
// 删除一个节点就要把他的父节点的left置为null.
// 如果删除root节点,则把整个树都删除。
// 此处为练习删除父节点,把他的左节点当作父节点。
if (this.left!=null && this.left.no==no){
if (this.left.left!=null&&this.left.right!=null){ //左右都不为空
this.left=this.left.left;
return;
}else if (this.left.left != null){ //左边不为空
this.left=this.left.left;
return;
}else if(this.left.right!=null){
this.left=this.left.right;
return;
}
}
if (this.right!=null && this.right.no==no){
this.right=null;
return;
}
//向左递归
if (this.left!=null){
this.left.delnode(no);
// 递归的作用:debug发现:会把整个流程走一遍,判断直到条件不满足。
}
// 向右递归
if (this.right!=null){
this.right.delnode(no);
}
//流程:先判断root,然后判断左,右节点,然后递归判断左节点的左右节点,如果为空了或者没找到,则判断右节点
}
}
二叉树的遍历,查找,删除
最新推荐文章于 2022-09-20 16:44:46 发布