挑战程序设计竞赛(算法和数据结构)——9.4二叉搜索树删除的JAVA实现
Posted 小乖乖的臭坏坏
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了挑战程序设计竞赛(算法和数据结构)——9.4二叉搜索树删除的JAVA实现相关的知识,希望对你有一定的参考价值。
题目:
这次没有参照讲解的方法,直接自己尝试,代码如下:
import java.util.LinkedList;
import java.util.Scanner;
public class BinaryTreeToInsert
//定义树的节点型类
public static class Node
int key;
Node parent, left, right;
Node(Node parent, Node left, Node right, int key)
this.parent = parent;
this.left = left;
this.right = right;
this.key = key;
public static void main(String[] args)
//建树
LinkedList<Node> tree = new LinkedList<Node>();
//数据输入及树的初始化与建立
Scanner cin = new Scanner(System.in);
System.out.println("请输入命令数:");
int n = cin.nextInt();
for (int i = 0; i < n; i++)
Scanner cin_string = new Scanner(System.in);
String s = cin_string.nextLine();
String[] string_arr = s.split(" ");//如果不是print那很显然是另有操作,需要分割
// String s = cin.nextLine();//nextLine()函数获取的是一整行的内容其中也包括了(\\n)也就是换行符,如果循环执行会出现nextLine()方法没有执行直接跳过的情况
if (string_arr[0].equals("print"))
printTreeWithInOrder(tree, tree.element());//用中序遍历树
System.out.println();
printTreeWithPreOrder(tree, tree.element());//用前序遍历树
System.out.println();
else if(string_arr[0].equals("insert"))
int num = Integer.valueOf(string_arr[1]);
Node new_node = new Node(null, null, null, num);
insertNode(tree, new_node);//插入新的节点
else if(string_arr[0].equals("find"))
int num = Integer.valueOf(string_arr[1]);
Node new_node = findNode(tree.element(), num);
if(new_node==null)
System.out.println("no");
else
System.out.println("yes");
else if(string_arr[0].equals("delete"))
int num = Integer.valueOf(string_arr[1]);
Node new_node = new Node(null, null, null, num);
deleteNode(tree, new_node);//插入新的节点
public static void insertNode(LinkedList<Node> T, Node z)
Node y = new Node(null, null, null, -1);//x的父节点
Node x;//获取T的根节点
if(T.isEmpty())x = new Node(null, null, null, -1);
elsex = T.element();
while(x!=null)//只要x不是根节点的父节点
y = x;//设置父节点
if(z.key<x.key)
x = x.left;//移动到左子节点
else
x = x.right;//移动到右子节点
z.parent = y;
if(y==null)
T.add(z);
else if(z.key<y.key)
T.add(z);
y.left = z;
else
T.add(z);
y.right = z;
public static Node findNode(Node x, int k)
while(x!=null && k!= x.key)
if(k<x.key)x = x.left;
elsex = x.right;
return x;
public static void deleteNode(LinkedList<Node> tree, Node a)
Node x = findNode(tree.element(), a.key);
if(x == tree.element())//x为根节点
if(x.left==null && x.right == null)//没有子节点
tree.remove();
else if(x.left!=null && x.right ==null)//有一个左子节点
x.left.parent = new Node(null, null, null, -1);
tree.remove();
else if(x.left==null && x.right !=null)//有一个右子节点
x.right.parent = new Node(null, null, null, -1);
tree.remove();
else//有两个子节点
Node temp = x.right;
if(temp.left != null)//如果还有左子节点的话
while (temp.left != null)
temp = temp.left;
if(temp.right != null)
temp.right.parent = temp.parent;
temp.parent.left = temp.right;
elsetemp.parent.left = null;
temp.parent = new Node(null, null, null, -1);
temp.left = x.left;
x.left.parent = temp;
temp.right = x.right;
x.right.parent = temp;
tree.addFirst(temp);
else if(temp.left == null)
temp.left = x.left;
x.left.parent = temp;
tree.addFirst(temp);
else //x不为根节点
if(x.left==null && x.right==null)//没有子节点
if(x.parent.left==x)x.parent.left=null;
else if(x.parent.right==x)x.parent.right=null;
else if(x.left!=null && x.right==null)//有一个左子节点
if(x.parent.left==x)
x.parent.left = x.left;
x.left.parent = x.parent;
else if(x.parent.right==x)
x.parent.right = x.left;
x.left.parent = x.parent;
else if(x.right!=null && x.left==null)//有一个右子节点
if(x.parent.left == x)
x.parent.left = x.right;
x.right.parent = x.parent;
else if(x.parent.right == x)
x.parent.right = x.right;
x.right.parent = x.parent;
else//有两个子节点
Node temp = x.right;
if(temp.left!=null)
while (temp.left != null)
temp = temp.left;
if(temp.right!=null)
temp.right.parent = temp.parent;
temp.parent.left = temp.right;
elsetemp.parent.left = null;
temp.parent = x.parent;
if(x.parent.left==x)x.parent.left = temp;
if(x.parent.right==x)x.parent.right = temp;
temp.left = x.left;
x.left.parent = temp;
temp.right = x.right;
x.right.parent = temp;
if(temp.left == null)
temp.parent = x.parent;
if(x.parent.left==x)x.parent.left = temp;
if(x.parent.right==x)x.parent.right = temp;
temp.left = x.left;
x.left.parent = x;
public static void printTreeWithInOrder(LinkedList<Node> T, Node N/*具体的某一个节点*/)
if (N==null)//判决为空的条件
return;
printTreeWithInOrder(T, N.left);
System.out.print(N.key + " ");
printTreeWithInOrder(T, N.right);
public static void printTreeWithPreOrder(LinkedList<Node> T, Node N/*具体的某一个节点*/)
if (N==null)//判决为空的条件
return;
System.out.print(N.key + " ");
printTreeWithPreOrder(T, N.left);
printTreeWithPreOrder(T, N.right);
输入输出如下:
请输入命令数:
18
insert 8
insert 2
insert 3
insert 7
insert 22
insert 1
find 1
>>>yes
find 2
>>>yes
find 3
>>>yes
find 4
>>>no
find 5
>>>no
find 6
>>>no
find 7
>>>yes
find 8
>>>yes
print
>>>1 2 3 7 8 22
>>>8 2 1 3 7 22
delete 3
delete 7
print
>>>1 2 8 22
>>>8 2 1 22
以上是关于挑战程序设计竞赛(算法和数据结构)——9.4二叉搜索树删除的JAVA实现的主要内容,如果未能解决你的问题,请参考以下文章
挑战程序设计竞赛(算法和数据结构)——9.2二叉搜索树插入的JAVA实现
挑战程序设计竞赛(算法和数据结构)——9.3二叉搜索树搜索的JAVA实现
挑战程序设计竞赛(算法和数据结构)——8.3二叉树的表达的JAVA实现
挑战程序设计竞赛(算法和数据结构)——8.4二叉树的遍历的JAVA实现