package com.test;import java.util.Scanner;public class TestBintree {public static void main(String[] args) {Scanner scan = new Scanner(System.in);int x = scan.nextInt();TreeNode head = new TreeNode(x, 1, null); //輸入一個整數 ,構造第一個結點while (scan.hasNext()) {int n = scan.nextInt();if (n == -1) {System.out.println("二叉排序樹建立完成");break;} else {create(head, n);}}preOrder(head);delete(head, 10);System.out.println();preOrder(head);}public static void create(TreeNode head, int n) { // 建立二叉尋找樹(二叉排序樹)int id = head.getId();if(n == id) {System.out.println("建立的值已經存在");return;}if (n < id) {if (head.getLchild() == null) {head.setLchild(new TreeNode(n, 2 * head.getNum(), head));return;} else {create(head.getLchild(), n);}} else {if (head.getRchild() == null) {head.setRchild(new TreeNode(n, 2 * head.getNum() + 1, head));return;} else {create(head.getRchild(), n);}}}public static void preOrder(TreeNode head) { // 中序遍曆二叉樹if (head == null) {return;} else {preOrder(head.getLchild());System.out.print(head.getId() + ":" + head.getNum() + " ");preOrder(head.getRchild());}}public static void delete(TreeNode head, int id) { // 刪除某個節點while (true) { //尋找要刪除的結點所在的位置if (id == head.getId()) {break;}if (id < head.getId()) {head = head.getLchild();if (head == null) {System.out.println("所要刪除的結點不存在");return;}} else {head = head.getRchild();if (head == null) {System.out.println("所要刪除的結點不存在");return;}}}TreeNode lchild = head.getLchild();TreeNode rchild = head.getRchild();TreeNode father = head.getFather();int num = head.getNum();if (num % 2 == 0) { //要刪除的結點是左孩子if (lchild == null && rchild == null) { //要刪除的結點是葉子結點father.setLchild(null);head = null;} else {if (lchild != null && rchild == null) { //要刪除的結點是只有左孩子lchild.setNum(num);father.setLchild(lchild);head = null;} else {if (lchild == null && rchild != null) { //要刪除的結點只有右孩子rchild.setNum(num);father.setLchild(rchild);head = null;} else {head.setId(lchild.getId()); //要刪除的結點既有左孩子,也有右孩子head.setLchild(lchild.getLchild());if (lchild.getRchild() != null) {if (rchild.getLchild() != null) {while (rchild.getLchild() != null) {rchild = rchild.getLchild();}}lchild.getRchild().setNum(num + 1);rchild.setLchild(lchild.getRchild());}lchild.setRchild(null);lchild.setLchild(null);lchild = null;}}}} else { //要刪除的結點是右孩子if (lchild == null && rchild == null) { //要刪除的結點是葉子結點father.setRchild(null);head = null;} else {if (lchild != null && rchild == null) { //要刪除的結點是只有左孩子lchild.setNum(num);father.setRchild(lchild);head = null;} else {if (lchild == null && rchild != null) { //要刪除的結點只有右孩子rchild.setNum(num);father.setRchild(rchild);head = null;} else { //要刪除的結點既有左孩子,也有右孩子head.setId(rchild.getId());head.setRchild(rchild.getRchild());if (rchild.getLchild() != null) {if (lchild.getRchild() != null) {while (lchild.getRchild() != null) {lchild = lchild.getRchild();}}rchild.getLchild().setNum(num - 1);lchild.setRchild(rchild.getLchild());}rchild.setRchild(null);rchild.setLchild(null);rchild = null;}}}}}public static boolean find(TreeNode head, int id) { //尋找某個結點,存在返回true,否則返回falsewhile (true) {if (id == head.getId()) {return true;}if (id < head.getId()) {head = head.getLchild();if (head == null) {return false;}} else {head = head.getRchild();if (head == null) {return false;}}}}}class TreeNode { //結點的結構private int id;private int num;private TreeNode lchild;private TreeNode rchild;private TreeNode father;public TreeNode(int id, int num, TreeNode father) {this.id = id;this.num = num;this.father = father;}public TreeNode getFather() {return father;}public void setFather(TreeNode father) {this.father = father;}public int getNum() {return num;}public void setNum(int num) {this.num = num;}public int getId() {return id;}public void setId(int id) {this.id = id;}public TreeNode getLchild() {return lchild;}public void setLchild(TreeNode lchild) {this.lchild = lchild;}public TreeNode getRchild() {return rchild;}public void setRchild(TreeNode rchild) {this.rchild = rchild;}}