二叉排序树 -- 增删查改
生活随笔
收集整理的這篇文章主要介紹了
二叉排序树 -- 增删查改
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
數(shù)據(jù)結(jié)構(gòu) -- 樹(shù)
1. 二叉排序樹(shù) -- 定義
2. 二叉排序樹(shù) -- 增
3. 二叉排序樹(shù) -- 刪
4. 二叉排序樹(shù) -- 查
5. 二叉排序樹(shù) -- 改
6. 二叉排序樹(shù) -- 實(shí)現(xiàn)
Class :?
package lime.tree;import java.util.Arrays;/*** @Author: Lime* @Description:* @Date:Create in 19:25 2017/10/28*/ public class BSTTree {private Integer value;private BSTTree PChild;private BSTTree LChild;private BSTTree RChild;public BSTTree(Integer value) {this.value = value;}public static void showTree(BSTTree root) {if (null == root) {return;}showTree(root.getLChild());System.out.print(root.getValue() + " ");showTree(root.getRChild());}public static boolean addTree(BSTTree root, Integer value) {if (null == value) {return false;}if (null == root) {root = new BSTTree(value);return true;}if (value == root.getValue()) {return false;} else if (value < root.getValue()) {if (null == root.getLChild()) {root.setLChild(value);root.getLChild().setPChild(root);return true;} else {return addTree(root.getLChild(), value);}} else {if (null == root.getRChild()) {root.setRChild(value);root.getRChild().setPChild(root);return true;} else {return addTree(root.getRChild(), value);}}}public static BSTTree searchTree(BSTTree root, Integer value) {if (null == root || null == value) {return null;}if (value == root.getValue()) {return root;} else if (value < root.getValue()) {return searchTree(root.getLChild(), value);} else {return searchTree(root.getRChild(), value);}}public static void deleteTree(BSTTree root, Integer value) {if (null == root || null == value) {return;}root = searchTree(root, value);if (null == root) {return;}if (null == root.getRChild() && null == root.getLChild()) {if (null == root.getPChild()) { // 1.只有一個(gè)根節(jié)點(diǎn)root = null;} else if (root == root.getPChild().getLChild()) { // 2. 刪除左葉子節(jié)點(diǎn)root.getPChild().setLChild((BSTTree) null);} else { // 3. 刪除右葉子節(jié)點(diǎn)root.getPChild().setRChild((BSTTree) null);}} else if ((null == root.getLChild()) ^ (null == root.getRChild())) {if (null == root.getPChild()) { // 1. 根節(jié)點(diǎn)下只有一個(gè)子樹(shù)root = (null == root.getLChild()) ? root.getRChild() : root.getLChild();} else if (root == root.getPChild().getRChild()) { // 2. root 是父節(jié)點(diǎn)的右子樹(shù),將root的唯一子樹(shù)放到父節(jié)點(diǎn)的右子樹(shù)上root.getPChild().setRChild(null == root.getLChild() ? root.getRChild() : root.getLChild());root.getPChild().getRChild().setPChild(root.getPChild());} else { // 3. root 是父節(jié)點(diǎn)的左子樹(shù),將root的唯一子樹(shù)放到父節(jié)點(diǎn)的左子樹(shù)上root.getPChild().setLChild(null == root.getRChild() ? root.getLChild() : root.getRChild());root.getPChild().getLChild().setPChild(root.getPChild());}} else { // 1. 如果刪除的節(jié)點(diǎn)有兩個(gè)子節(jié)點(diǎn),則尋找root節(jié)點(diǎn)的前驅(qū)節(jié)點(diǎn)的值替換root節(jié)點(diǎn)的值,然后刪除前驅(qū)節(jié)點(diǎn)BSTTree newRoot = searchPrecursor(root.getLChild());Integer newRootValue = newRoot.getValue();deleteTree(root, newRootValue);root.setValue(newRootValue);}}private static BSTTree searchPrecursor(BSTTree root) {if (null == root.getRChild()) {return root;} else {return searchPrecursor(root.getRChild());}}public Integer getValue() {return value;}public void setValue(Integer value) {this.value = value;}public BSTTree getLChild() {return LChild;}public void setLChild(BSTTree LChild) {this.LChild = LChild;}public void setLChild(Integer value) {this.LChild = new BSTTree(value);}public BSTTree getRChild() {return RChild;}public void setRChild(BSTTree RChild) {this.RChild = RChild;}public void setRChild(Integer value) {this.RChild = new BSTTree(value);}public BSTTree getPChild() {return PChild;}public void setPChild(BSTTree PChild) {this.PChild = PChild;}@Overridepublic String toString() {return "BSTTree{" +"value=" + value +'}';}public static void putTree(BSTTree root, int[] angles) {if (null != root) {for (int angle : angles) {addTree(root, angle);}}} }Class :?
package lime.tree;/*** @Author: Lime* @Description:* @Date:Create in 19:25 2017/10/28*/ public class BSTTreeTt {public static void main(String[] args){BSTTree root = new BSTTree(13);int[] angles = {8,17,1,11,15,25,6,22,27,26};BSTTree.putTree(root,angles);showTree(root); // for(int i = 0;i < 10;i++){ // BSTTree.addTree(root,(int)(Math.random() * 100)); // } // showTree(root); // int elf = (int)(Math.random() * 100); // System.out.println("search " + elf + " : " + BSTTree.searchTree(root,elf));BSTTree.deleteTree(root,11);showTree(root); // BSTTree.addTree(root,11);BSTTree.deleteTree(root,1);showTree(root); // BSTTree.addTree(root,1);BSTTree.deleteTree(root,27);showTree(root); // BSTTree.addTree(root,27);BSTTree.deleteTree(root,17);showTree(root); // BSTTree.addTree(root,17);BSTTree.deleteTree(root,13);showTree(root);}public static void showTree(BSTTree root){BSTTree.showTree(root);System.out.println();} }啦啦啦
轉(zhuǎn)載于:https://www.cnblogs.com/ClassNotFoundException/p/7781032.html
總結(jié)
以上是生活随笔為你收集整理的二叉排序树 -- 增删查改的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 每天一个linux命令-用户之间切换
- 下一篇: Deep learning chapte