- 二叉排序树(BST)的构造,节点插入,节点查找,节点删除(java)
高度最小BST(同样数据,顺序可能不一样)
package ccnu.offer.tree;
import java.util.ArrayList;
import java.util.Arrays;
public class Demo04 {
public static void main(String[] args) {
int[] datas = new int[]{53, 17, 78, 9, 45, 65, 94, 23, 81, 88};
Node root = null;
root = removeBSTNode(createBST(root, datas), 78);
inOrder(root);
}
public static Node createBST(Node root, int[] datas){
root = null;
int index = 0;
while(index < datas.length){
root = insertBST(root, datas[index]);
index++;
}
return root;
}
public static Node insertBST(Node root, int data){
if(root == null){
root = new Node(data);
}
else if(data <= root.data){
root.lchild = insertBST(root.lchild, data);
}else{
root.rchild = insertBST(root.rchild, data);
}
return root;
}
public static Node searchBST(Node root, int data){
Node p = null;
while(root != null && root.data != data){
p = root;
if(data < root.data){
root = root.lchild;
}else{
root = root.rchild;
}
}
return p;
}
public static Node getChild(Node root, Node p, int data){
if(p == null){
return root;
}
if(p.lchild == null && p.rchild == null){
return null;
}
if(p.lchild != null && p.lchild.data == data){
return p.lchild;
}else{
return p.rchild;
}
}
public static Node searchBST1(Node root, int data){
if(root == null){
return null;
}else if(root.data == data){
return root;
}else if(data < root.data){
root = searchBST1(root.lchild, data);
}else{
root = searchBST1(root.rchild, data);
}
return root;
}
public static Node removeBSTNode(Node root, int data) {
if (root == null) {
return null;
}
Node p = searchBST(root, data);
Node node = null;
node = getChild(root, p, data);
if (node == null) {
return root;
}
if (node.lchild == null && node.rchild == null) {
if (node == root) {
root = null;
return root;
}
if (p.lchild != null && p.lchild.data == node.data) {
p.lchild = null;
} else {
p.rchild = null;
}
node = null;
} else if (node.lchild != null && node.rchild == null) {
if (node == root) {
root = null;
return node.lchild;
}
if (p.lchild != null && p.lchild.data == node.data) {
p.lchild = node.lchild;
} else {
p.rchild = node.lchild;
}
} else if (node.lchild == null && node.rchild != null) {
if (node == root) {
root = null;
return node.rchild;
}
if (p.lchild != null && p.lchild.data == node.data) {
p.lchild = node.rchild;
} else {
p.rchild = node.rchild;
}
} else {
Node nextNode = node.rchild;
while (nextNode.lchild != null) {
nextNode = nextNode.lchild;
}
removeBSTNode(node, nextNode.data);
nextNode.lchild = node.lchild;
nextNode.rchild = node.rchild;
if (node == root) {
return nextNode;
}else{
if (p.lchild != null && p.lchild.data == node.data) {
p.lchild = nextNode;
} else {
p.rchild = nextNode;
}
}
}
return root;
}
public static void inOrder(Node root){
if(root != null){
inOrder(root.lchild);
System.out.print(root.data + " ");
inOrder(root.rchild);
}
}
private static class Node extends Object{
private Node lchild = null;
private Node rchild = null;
private int data;
public Node(int data){
this.data = data;
}
}
public static ArrayList<Integer> getMedians(int[] datas, int begin, int end) {
Arrays.sort(datas);
ArrayList<Integer> arr = new ArrayList<Integer>();
if (begin > end) {
return arr;
} else if (begin == end) {
arr.add(datas[begin]);
} else {
int medium = (begin + end) / 2;
arr.add(datas[medium]);
arr.addAll(getMedians(datas, begin, medium - 1));
arr.addAll(getMedians(datas, medium + 1, end));
}
return arr;
}
public static int getDepth(Node root){
int ldepth = 0;
int rdepth = 0;
if(root == null){
return 0;
}
ldepth = getDepth(root.lchild);
rdepth = getDepth(root.rchild);
return (ldepth > rdepth ? ldepth : rdepth) + 1;
}
}