评论

收藏

[Java] JAVA二叉树的基本操作

编程语言 编程语言 发布于:2021-12-11 14:01 | 阅读数:288 | 评论:0

这篇文章主要介绍了JAVA二叉树的基本操作DEMO,想要详情了解的小伙伴请接着看下文吧
目录

  • 记录二叉树的基本操作DEMO

    • 1、创建一个二叉树类
    • 2、然后创建二叉树的节点


记录二叉树的基本操作DEMO

1、创建一个二叉树类
这里约束了泛型只能为实现了Comparable这个接口的类型。
/**
 * @author JackHui
 * @version BinaryTree.java, 2020年03月05日 12:45
 */
public class BinaryTree<T extends Comparable> {
 
  //树根
  BinaryTreeNode root;
 
  public boolean deleteData(T data) {
    if (root.data.equals(data)) {
      root = null;
      return true;
    }
    return root.deleteNode(data);
  }
 
  public T frontSearch(T data) {
    return (T) root.frontSearch(data);
  }
 
  public T midSearch(T data) {
    return (T) root.midSearch(data);
  }
 
  public T rearSearch(T data) {
    return (T) root.rearSearch(data);
  }
 
  public void frontEach() {
    this.root.frontEach();
  }
 
  public void midEach() {
    this.root.midEach();
  }
 
  public void rearEach() {
    this.root.rearEach();
  }
 
 
  public BinaryTreeNode getRoot() {
    return root;
  }
 
  public void setRoot(BinaryTreeNode root) {
    this.root = root;
  }
}
2、然后创建二叉树的节点
package binarytree;
 
/**
 * @author JackHui
 * @version BinaryTreeNode.java, 2020年03月06日 10:24
 */
public class BinaryTreeNode<T extends Comparable> {
  T data;
  BinaryTreeNode lChild;
  BinaryTreeNode rChild;
 
  public BinaryTreeNode(T data) {
    this.data = data;
  }
 
  //先序遍历
  public void frontEach() {
    System.out.print(this.data + "\t");
    if (lChild != null) {
      lChild.frontEach();
    }
    if (rChild != null) {
      rChild.frontEach();
    }
  }
 
  //中序遍历
  public void midEach() {
    if (lChild != null) {
      lChild.frontEach();
    }
    System.out.print(this.data + "\t");
    if (rChild != null) {
      rChild.frontEach();
    }
  }
 
  //后序遍历
  public void rearEach() {
    if (lChild != null) {
      lChild.frontEach();
    }
    if (rChild != null) {
      rChild.frontEach();
    }
    System.out.print(this.data + "\t");
  }
 
  //先序查找
  public T frontSearch(T data) {
    T target = null;
    System.out.println("[先序遍历]当前遍历到的元素:" + this.data + "\t查找的元素:" + data + "\t" + (this.data.compareTo(data) == 0 ? "查找到元素:" + data : ""));
    if (this.data.compareTo(data) == 0) {
      return data;
    } else {
      if (lChild != null && (target = (T) lChild.frontSearch(data)) != null) {
        return target;
      }
      if (rChild != null && (target = (T) rChild.frontSearch(data)) != null) {
        return target;
      }
    }
    return target;
  }
 
  //中序查找
  public T midSearch(T data) {
    T target = null;
    if (lChild != null && (target = (T) lChild.midSearch(data)) != null) {
      return target;
    }
    System.out.println("[中序遍历]当前遍历到的元素:" + this.data + "\t查找的元素:" + data + "\t" + (this.data.compareTo(data) == 0 ? "查找到元素:" + data : ""));
    if (this.data.compareTo(data) == 0) {
      return data;
    } else {
      if (rChild != null && (target = (T) rChild.midSearch(data)) != null) {
        return target;
      }
    }
    return target;
  }
 
  //后序查找
  public T rearSearch(T data) {
    T target = null;
    if (lChild != null && (target = (T) lChild.rearSearch(data)) != null) {
      return target;
    }
    if (rChild != null && (target = (T) rChild.rearSearch(data)) != null) {
      return target;
    }
    System.out.println("[后续遍历]当前遍历到的元素:" + this.data + "\t查找的元素:" + data + "\t" + (this.data.compareTo(data) == 0 ? "查找到元素:" + data : ""));
    if (this.data.compareTo(data) == 0) {
      return data;
    }
    return target;
  }
 
  //根据值删除节点
  public boolean deleteNode(T data) {
    System.out.println("[节点删除]当前遍历到的父节点:" + this.data + "\t" + "匹配的节点数据:" + data);
    //判断左子树是否匹配
    if (this.lChild != null && (this.lChild.data.compareTo(data) == 0)) {
      System.out.println("[节点删除]当前遍历到的父节点:" + this.data + "\t" + "匹配的节点数据:" + data + "\t节点删除成功!");
      this.lChild = null;
      return true;
    } else if (this.rChild != null && (this.rChild.data.compareTo(data) == 0)) {
      System.out.println("[节点删除]当前遍历到的父节点:" + this.data + "\t" + "匹配的节点数据:" + data + "\t节点删除成功!");
      this.rChild = null;
      return true;
    }
    if (this.lChild != null && this.lChild.deleteNode(data)) {
      return true;
    }
    if (this.rChild != null && this.rChild.deleteNode(data)) {
      return true;
    }
    return false;
  }
 
  public T getData() {
    return data;
  }
 
  public void setData(T data) {
    this.data = data;
  }
 
  public BinaryTreeNode getlChild() {
    return lChild;
  }
 
  public void setlChild(BinaryTreeNode lChild) {
    this.lChild = lChild;
  }
 
  public BinaryTreeNode getrChild() {
    return rChild;
  }
 
  public void setrChild(BinaryTreeNode rChild) {
    this.rChild = rChild;
  }
}
到此这篇关于JAVA二叉树的基本操作DEMO的文章就介绍到这了,更多相关JAVA二叉树内容请搜索CodeAE代码之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持CodeAE代码之家!
原文链接:https://blog.csdn.net/qq_41311209/article/details/104695849

关注下面的标签,发现更多相似文章