1、基本介绍
-
二叉树的遍历
-
二叉树的查找
-
二叉树的删除
2、应用实例(二叉树的遍历查找删除)
package tree;
public class BinaryTreeDemo {
public static void main(String[] args) {
// 先构建一棵空二叉树
BinaryTree tree = new BinaryTree();
TreeNode root = new TreeNode(1, "1号苹果");
TreeNode node2 = new TreeNode(2, "2号苹果");
TreeNode node3 = new TreeNode(3, "3号苹果");
TreeNode node4 = new TreeNode(4, "4号苹果");
root.left = node2;
root.right = node3;
node3.right = node4;
// 创建二叉树
tree.root = root;
// 遍历二叉树
tree.preOrderTraverse();
tree.inOrderTraverse();
tree.postOrderTraverse();
// 查找指定树节点
TreeNode temp1 = tree.preOrderSearch(3);
TreeNode temp2 = tree.inOrderSearch(2);
TreeNode temp3 = tree.postOrderSearch(4);
if (temp1 != null) {
System.out.println("查找成功! => 该结点的name="+temp1.name);
} else {
System.out.println("查找失败!");
}
if (temp2 != null) {
System.out.println("查找成功! => 该结点的name="+temp2.name);
} else {
System.out.println("查找失败!");
}
if (temp3 != null) {
System.out.println("查找成功! => 该结点的name="+temp3.name);
} else {
System.out.println("查找失败!");
}
System.out.println("------------------------------");
// 删除节点
tree.deleteNode(2);
tree.deleteNode(3);
tree.deleteNode(1);
// 遍历二叉树
tree.preOrderTraverse();
tree.inOrderTraverse();
tree.postOrderTraverse();
}
}
// 二叉树
class BinaryTree {
public TreeNode root;
/*
规定:
如果删除的节点是叶子节点,则删除该节点
如果删除的节点是非叶子节点,则删除该子树
*/
public void deleteNode(int no) {
if (this.root != null) {
if (this.root.no == no) { // 判断待删除节点是否是根节点
this.root = null;
return;
} else {
this.root.deleteNode(no);
}
} else {
System.out.println("此树为空树!");
}
}
// 前序查找
public TreeNode preOrderSearch(int no) {
if (root != null) {
return this.root.preOrderSearch(no);
} else {
return null;
}
}
// 前序查找
public TreeNode inOrderSearch(int no) {
if (root != null) {
return this.root.inOrderSearch(no);
} else {
return null;
}
}
// 前序查找
public TreeNode postOrderSearch(int no) {
if (root != null) {
return this.root.postOrderSearch(no);
} else {
return null;
}
}
// 前序遍历
public void preOrderTraverse() {
if (this.root != null) {
System.out.print("前序遍历:");
this.root.preOrderTraverse();
System.out.println();
} else {
System.out.println("此树为空树!");
}
}
// 中序遍历
public void inOrderTraverse() {
if (this.root != null) {
System.out.print("中序遍历:");
this.root.inOrderTraverse();
System.out.println();
} else {
System.out.println("此树为空树!");
}
}
// 后序遍历
public void postOrderTraverse() {
if (this.root != null) {
System.out.print("后序遍历:");
this.root.postOrderTraverse();
System.out.println();
} else {
System.out.println("此树为空树!");
}
}
}
// 树节点
class TreeNode {
public int no;
public String name;
public TreeNode left;
public TreeNode right;
public TreeNode(int no, String name) {
this.no = no;
this.name = name;
this.left = null;
this.right = null;
}
public void deleteNode(int no) {
// 因为二叉树是单向的,所以是判断当前节点的子节点是否是需要删除节点,而不是判断当前节点是不是删除节点
// 如果当前节点的左子节点不为空,并且该子节点就是删除节点,就将this.left = null
if (this.left != null && this.left.no == no) {
this.left = null;
return;
}
// 如果当前节点的右子节点不为空,并且该子节点就是删除节点,就将this.right = null
if (this.right != null && this.right.no == no) {
this.right = null;
return;
}
// 如果上述都没有删除节点,则向左子树进行递归删除
if (this.left != null) {
this.left.deleteNode(no);
}
// 若左子树未找到待删除节点,就向右子树进行递归删除
if (this.right != null) {
this.right.deleteNode(no);
}
}
// 前序查找
public TreeNode preOrderSearch(int no) {
if (this.no == no) {
return this;
}
TreeNode treeNode = null;
if (this.left != null) {
treeNode = this.left.preOrderSearch(no);
}
if (this.right != null){
treeNode = this.right.preOrderSearch(no);
}
return treeNode;
}
// 中序查找
public TreeNode inOrderSearch(int no) {
TreeNode treeNode = null;
if (this.left != null) {
treeNode = this.left.inOrderSearch(no);
}
if (treeNode != null) {
return treeNode;
}
if (this.no == no) {
return this;
}
if (this.right != null){
treeNode = this.right.inOrderSearch(no);
}
return treeNode;
}
// 后序查找
public TreeNode postOrderSearch(int no) {
TreeNode treeNode = null;
if (this.left != null) {
treeNode = this.left.postOrderSearch(no);
}
if (treeNode != null) {
return treeNode;
}
if (this.right != null){
treeNode = this.right.postOrderSearch(no);
}
if (this.no == no) {
return this;
}
return treeNode;
}
// 前序遍历
public void preOrderTraverse() {
// 输出根节点
System.out.print(this.no + " ");
// 左子树若不为空,遍历左子树
if (this.left != null) {
this.left.preOrderTraverse();
}
// 右子树若不为空,遍历右子树
if (this.right != null) {
this.right.preOrderTraverse();
}
}
// 中序遍历
public void inOrderTraverse() {
// 左子树若不为空,遍历左子树
if (this.left != null) {
this.left.inOrderTraverse();
}
// 输出根节点
System.out.print(this.no + " ");
// 右子树若不为空,遍历右子树
if (this.right != null) {
this.right.inOrderTraverse();
}
}
// 后序遍历
public void postOrderTraverse() {
// 左子树若不为空,遍历左子树
if (this.left != null) {
this.left.postOrderTraverse();
}
// 右子树若不为空,遍历右子树
if (this.right != null) {
this.right.postOrderTraverse();
}
// 输出根节点
System.out.print(this.no + " ");
}
}