# 二叉树详解-Java实现二叉树数据结构

12 篇文章 0 订阅

### 二叉树

##### 二叉树的节点类:
public class Node {
private Object data; //节点数据 private Node leftChild; //左子节点的引用 			   private Node rightChild; //右子节点的引用 //打印节点内容
public void display(){
System.out.println(data); }
}

##### 二叉树的具体方法:
public interface Tree { //查找节点
public Node find(Object key); //插入新节点
public boolean insert(Object key); //删除节点
public boolean delete(Object key);
//Other Method......
}


### 查找节点

1、查找值比当前节点值大，则搜索右子树;
2、查找值等于当前节点值，停止搜索(终止条件);
3、查找值小于当前节点值，则搜索左子树;

//查找节点
public Node find(int key) {
Node current = root; while(current != null){
if(current.data > key){//当前值比查找值大，搜索左子树 current = current.leftChild;
}else if(current.data < key){//当前值比查找值小，搜索右子树 current = current.rightChild;
}else{
return current;
} }
return null;//遍历完整个树没找到，返回null
}


### 插入节点

//插入节点
public boolean insert(int data) {
Node newNode = new Node(data);
if(root == null){//当前树为空树，没有任何节点
root = newNode;
return true;
}else{
Node current = root; Node parentNode = null; while(current != null){
parentNode = current;
if(current.data > data){//当前值比插入值大，搜索左子节点
current = current.leftChild;
if(current == null){//左子节点为空，直接将新值插入到该节点
parentNode.leftChild = newNode;
return true;
}
}else{
current = current.rightChild;
if(current == null){//右子节点为空，直接将新值插入到该节点
parentNode.rightChild = newNode;
return true;
}
} }
}
return false;
}


### 遍历树

1、前序遍历:根节点——>左子树——>右子树
2、中序遍历:左子树——>根节点——>右子树
3、后序遍历:左子树——>右子树——>根节点

//中序遍历
public void infixOrder(Node current){
if(current != null){
infixOrder(current.leftChild);
System.out.print(current.data+" ");
infixOrder(current.rightChild);
}
}
//前序遍历
public void preOrder(Node current){
if(current != null){
System.out.print(current.data+" ");
preOrder(current.leftChild);
preOrder(current.rightChild);
}
}
//后序遍历
public void postOrder(Node current){
if(current != null){
postOrder(current.leftChild);
postOrder(current.rightChild);
System.out.print(current.data+" ");
}
}


### 查找最大值和最小值

//找到最大值
public Node findMax(){
Node current = root; Node maxNode = current; while(current != null){
maxNode = current;
current = current.rightChild; }
return maxNode;
}
//找到最小值
public Node findMin(){
Node current = root; Node minNode = current; while(current != null){
minNode = current;
current = current.leftChild; }
return minNode;
}


### 删除节点

1、该节点是叶节点(没有子节点)
2、该节点有一个子节点
3、该节点有两个子节点

###### 删除没有子节点的节点

public boolean delete(int key) {
Node current = root;
Node parent = root;
boolean isLeftChild = false; //查找删除值，找不到直接返回false while(current.data != key){
parent = current; if(current.data > key){
isLeftChild = true;
current = current.leftChild; }else{
isLeftChild = false;
current = current.rightChild; }
if(current == null){ return false;
} }
//如果当前节点没有子节点
if(current.leftChild == null && current.rightChild == null){
if(current == root){ root = null;
}else if(isLeftChild){ parent.leftChild = null;
}else{
parent.rightChild = null;}
return true;}
return false;
}

###### 删除有一个子节点的节点

//当前节点有一个子节点
if(current.leftChild == null && current.rightChild != null){
if(current == root){
root = current.rightChild;
}else if(isLeftChild){
parent.leftChild = current.rightChild;
}else{
parent.rightChild = current.rightChild; }
return true;
}else{
//current.leftChild != null && current.rightChild == null
if(current == root){
root = current.leftChild;
}else if(isLeftChild){
parent.leftChild = current.leftChild;
}else{
parent.rightChild = current.leftChild;
}
return true;
}

###### 删除有两个子节点的节点

public Node getSuccessor(Node delNode){
Node successorParent = delNode; Node successor = delNode;
Node current = delNode.rightChild; while(current != null){
successorParent = successor; successor = current;
current = current.leftChild;
}
//将后继节点替换删除节点
if(successor != delNode.rightChild){
successorParent.leftChild = successor.rightChild;
successor.rightChild = delNode.rightChild; }
return successor;
}

###### 删除有必要吗?

public class Node {
int data; //节点数据
Node leftChild; //左子节点的引用
Node rightChild; //右子节点的引用
boolean isDelete;//表示节点是否被删除
}


• 2
点赞
• 13
收藏
• 打赏
• 0
评论
07-14
07-03 2万+
11-03 731
04-30 1314
08-04 645
01-02 388
08-30 2518
10-16 767
08-02 156

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。