二叉排序树:要求,一个节点的左子树值一定小于等于该节点,右子树的值一定大于该节点
构建有序二叉树:
递归:方法调用其本身:循环
- 递归表达式
- 递归出口
package tree;
import java.util.LinkedList;
public class BinaryTree {
// 新建代表整棵树的节点
TreeNode root;
public void insert(Integer value){
// 新建一个节点
TreeNode newNode = new TreeNode(value);
if(root == null){
root = newNode;
}else {
TreeNode currentNode = root;
TreeNode parentNode ;
while (true){
parentNode = currentNode;
if (newNode.getValue() > currentNode.getValue()){
currentNode = currentNode.getRightTreeNode();
if (currentNode == null){
parentNode.setRightTreeNode(newNode);
return;
}
}else {
currentNode = currentNode.getLeftTreeNode();
if (currentNode == null){
parentNode.setLeftTreeNode(newNode);
return;
}
}
}
}
}
public TreeNode insertDiGui(TreeNode node,Integer value){
TreeNode newNode = new TreeNode(value);
if (root == null){
return root = newNode;
}
if (newNode.getValue() > node.getValue()){
if (node.getRightTreeNode() == null){
node.setRightTreeNode(newNode);
return root;
}
return insertDiGui(node.getLeftTreeNode(),value);
}else {
if (node.getLeftTreeNode() == null){
node.setRightTreeNode(newNode);
return root;
}
return insertDiGui(node.getLeftTreeNode(),value);
}
}
/**
* 先序遍历
*/
public void beforeOrder(TreeNode node){
if (node == null){
return;
}
System.out.println(" "+ node.getValue());
beforeOrder(node.getLeftTreeNode());
beforeOrder(node.getRightTreeNode());
}
/**
* 中序遍历
*/
public void minOrder(TreeNode node){
if (node == null){
return;
}
minOrder(node.getLeftTreeNode());
System.out.println(" "+ node.getValue());
minOrder(node.getRightTreeNode());
}
/**
* 后序遍历
* @param node
*/
public void backOrder(TreeNode node){
if (node == null){
return;
}
backOrder(node.getLeftTreeNode());
backOrder(node.getRightTreeNode());
System.out.println(" "+node.getValue());
}
/**
* 找到想要删除的节点
*/
public TreeNode search(TreeNode node,int value){
if (node == null){
return null;
}
if (node.getValue() == value){
return node;
}else if (value < node.getValue()){
if (node.getValue() == null){
return null;
}
return search(node.getLeftTreeNode(),value);
}else {
if (node.getRightTreeNode() == null){
return null;
}
return search(node.getRightTreeNode(),value);
}
}
/**
* 查找父节点
* @param node
* @param value
* @return
*/
public TreeNode searchParent(TreeNode node,int value){
if (node == null){
return null;
}
if((node.getLeftTreeNode() != null && node.getLeftTreeNode().getValue() == value) ||
(node.getRightTreeNode() !=null && node.getRightTreeNode().getValue() == value)){
return node;
}else {
if (node.getLeftTreeNode() != null&&node.getLeftTreeNode().getValue() ==value){
return searchParent(node.getLeftTreeNode(),value);
}else if(node.getRightTreeNode() !=null && value > node.getValue()){
return searchParent(node.getRightTreeNode(),value);
}else {
return null; // 没有找到
}
}
}
/**
* 查找右子树当中的最小值
*/
public int delRightTreeMin(TreeNode node){
//定义一个指针
TreeNode currentNode = node;
//通过while循环找到最后一个值
while (currentNode.getLeftTreeNode() != null){
currentNode = currentNode.getLeftTreeNode();
}
delNode(node,currentNode.getValue());
return currentNode.getValue();
}
/**
* 删除节点
* @param node 代表我们要删除树
* @param value 删除的节点的值
*/
public void delNode(TreeNode node,int value){
// 判断这棵树是否为空
if(node == null){
return;
}
// 找到我们呀删除的节点
TreeNode targetNode = search(node,value);
//判断我们找的的这个节点是否为空
if(targetNode == null){
return;
}
//找到target节点的父节点
TreeNode parent = searchParent(node,value);
if(targetNode.getLeftTreeNode() == null && targetNode.getRightTreeNode() == null){//删除叶子节点
//判断targeNode节点是parent节点的左子树还是右子树
if(parent.getLeftTreeNode() !=null && parent.getLeftTreeNode().getValue() == value ){//左子树
parent.setLeftTreeNode(null);
}else if(parent.getRightTreeNode() !=null && parent.getRightTreeNode().getValue() == value ){//右子树
parent.setRightTreeNode(null);
}
}else if(targetNode.getLeftTreeNode() != null && targetNode.getRightTreeNode() != null){//有两个子树的节点
int MinValue = delRightTreeMin(targetNode.getRightTreeNode());//找到右子树中的最小数
targetNode.setValue(MinValue);
}else{ //只有一个子树的情况
if(targetNode.getLeftTreeNode() != null){ //判断我们要删除的节点是否有左子节点
if(parent.getLeftTreeNode().getValue() == value){ //判断我们要删除的节点是父节点的左子节点
parent.setLeftTreeNode(targetNode.getLeftTreeNode());
}else {
parent.setRightTreeNode(targetNode.getLeftTreeNode());
}
}else { //判断我们要删除的节点是否有右子节点
if(parent.getLeftTreeNode().getValue() == value){ //判断我们要删除的节点是父节点的左子节点
parent.setLeftTreeNode(targetNode.getRightTreeNode());
}else {
parent.setRightTreeNode(targetNode.getRightTreeNode());
}
}
}
}
public void levelOrder(TreeNode node){
if (node == null){
return ;
}
//定义队列
LinkedList<TreeNode> queue = new LinkedList<>();
queue.add(node);
while (!queue.isEmpty()){
node = queue.pop();
System.out.println(node.getValue() + " ");
if (node.getLeftTreeNode() != null){
queue.add(node.getLeftTreeNode());
}
if (node.getRightTreeNode() != null){
queue.add(node.getLeftTreeNode());
}
}
}
}
package tree;
/*
构建节点构建节点
构建节点构建节点
*/
public class TreeNode {
private TreeNode leftTreeNode;
private TreeNode rightTreeNode;
private Integer value;
public Integer getValue(){
return value;
}
public void setValue(Integer value) {
this.value = value;
}
public TreeNode(Integer value){
this.value = value;
}
public TreeNode getLeftTreeNode() {
return leftTreeNode;
}
public void setLeftTreeNode(TreeNode leftTreeNode) {
this.leftTreeNode = leftTreeNode;
}
public TreeNode getRightTreeNode() {
return rightTreeNode;
}
public void setRightTreeNode(TreeNode rightTreeNode) {
this.rightTreeNode = rightTreeNode;
}
@Override
public String toString() {
return "TreeNode{" +
"leftTreeNode=" + leftTreeNode +
", rightTreeNode=" + rightTreeNode +
", value=" + value +
'}';
}
}