二叉树(java版)

这篇博客介绍了如何在Java中创建二叉树的节点类`TreeNode`,包括节点的数据域、左右子节点的引用,并提供了构造函数进行初始化。
摘要由CSDN通过智能技术生成

二叉树节点类:

package com.node;
public class TreeNode {
public int data;
public TreeNode leftChild;
public TreeNode rightChild;
public TreeNode(int data) {
this.data = data;
this.leftChild = null;
this.rightChild = null;
}

}

测试类:


package com.node;

public class TreeTest {

public static TreeNode root;

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array = { 35, 17, 39, 9, 28, 65, 56, 87 };
root = new TreeNode(array[0]); // 创建二叉树
for (int i = 1; i < array.length; i++) {
insert(root, array[i]); // 向二叉树中插入数据
}
/*
* 前序遍历或称先跟遍历 即遍历顺序为:先根节点-左子树-右子树
*/
// System.out.println("先根遍历:");
// preOrder(root);
/*
* 中序遍历或称中跟遍历 即遍历顺序为:先左子树-根节点-右子树
*/


System.out.println();
System.out.println("中根遍历:");
inOrder(root);

delete(65);
System.out.println("\n删除后:");
inOrder(root);
/*
* 后序遍历或称后跟遍历 即遍历顺序为:先左子树-右子树-根节点
*/
// System.out.println();
// System.out.println("后根遍历:");
// postOrder(root);
}
// 向二叉树中插入数据
public static void insert(TreeNode root, int data) {
if (data > root.data) { // 如果插入的节点大于跟节点
if (root.rightChild == null) { // 如果右子树为空,就插入,如果不为空就再创建一个节点
root.rightChild = new TreeNode(data); // 就把插入的节点放在右边
} else {
insert(root.rightChild, data);
}
} else { // 如果插入的节点小于根节点
if (root.leftChild == null) { // 如果左子树为空,就插入,如果不为空就再创建一个节点
root.leftChild = new TreeNode(data); // 就把插入的节点放在左边边
} else {
insert(root.leftChild, data);
}
}
}


public static void preOrder(TreeNode root) { // 先根遍历
if (root != null) {
System.out.print(root.data + "-");
preOrder(root.leftChild);
preOrder(root.rightChild);
}
}


public static void inOrder(TreeNode root) { // 中根遍历


if (root != null) {
inOrder(root.leftChild);
System.out.print(root.data + "-");
inOrder(root.rightChild);
}
}


public static void postOrder(TreeNode root) { // 后根遍历


if (root != null) {
postOrder(root.leftChild);
postOrder(root.rightChild);
System.out.print(root.data + "-");
}
}
  //删除节点
public static void delete(int value) {
TreeNode current = root;
TreeNode parent = root;
boolean isLeftChild = true;


while (current.data != value) {
parent = current;
if (current.data > value) {
current = current.leftChild;
isLeftChild = true;
} else {
current = current.rightChild;
isLeftChild = false;
}
if (current == null) {
System.out.println("没有找到该节点");
return;
}


}
          //删除叶枝节点
if (current.leftChild == null && current.rightChild == null) {
if (isLeftChild)
parent.leftChild = null;
else
parent.rightChild = null;

else if (current.rightChild == null) {
if (current == root) {
root = current.leftChild;
} else if (isLeftChild) {
parent.leftChild = current.leftChild;
} else {
parent.rightChild = current.leftChild;
}
} else if (current.leftChild == null) {
if (current == root) {
root = current.rightChild;
} else if (isLeftChild) {
parent.leftChild = current.rightChild;
} else {
parent.rightChild = current.rightChild;
}
} else {
TreeNode successor = getSuccessor(current);
if (current == root) {
root = successor;
} else if (isLeftChild) {
parent.leftChild = successor;
} else {
parent.rightChild = successor;
}
successor.leftChild = current.leftChild;
}


}


public static TreeNode getSuccessor(TreeNode delNode) {
TreeNode successor = delNode;
TreeNode successorParent = delNode;
TreeNode 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;
}


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值