資料結構之二叉排序樹或者二叉尋找樹(java版)__資料結構

來源:互聯網
上載者:User
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;}}


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.