Java再帰実装二分検索ツリー
リード:本文は2つの部分に分けられる:1.実装コード2.テストコード
実装コードは次のとおりです.
テストコードは次のとおりです.
実装コードは次のとおりです.
private class Node{
public E e;
public Node left, right;
public Node(E e){
this.e = e;
left = null;
right = null;
}
}
private Node root;
private int size;
public BST (){
root = null;
size = 0;
}
public int size(){
return size;
}
public boolean isEmpty(){
return size== 0 ;
}
// e
public void add(E e){
root = add(root, e);
}
// node e,
//
private Node add(Node node, E e) {
if (node == null){
size++;
return new Node(e);
}
if (e.compareTo(node.e) > 0) {
node.right = add(node.right, e);
} else
node.left = add(node.left, e);
return node;
}
// e
public boolean contains(E e){
return contains(root ,e);
}
private boolean contains(Node node, E e){
if (node == null)
return false;
if (e.compareTo(node.e) == 0){
return true;
}
else if (e.compareTo(node.e) > 0)
return contains(node.right, e);
else
return contains(node.left, e);
}
//
public void perOrder(){
perOrder(root);
}
//
private void perOrder(Node node){
if (node != null){
System.out.print(node.e+" ");
perOrder(node.left);
perOrder(node.right);
}
}
//
public void inOrder(){
inOrder(root);
}
private void inOrder(Node node){
if (node == null)
return;
inOrder(node.left);
System.out.print(node.e+" ");
inOrder(node.right);
}
//
public void postOrder(){
postOrder(root);
}
private void postOrder(Node node){
if (node != null){
postOrder(node.left);
postOrder(node.right);
System.out.print(node.e + " ");
}
}
@Override
public String toString(){
StringBuilder res = new StringBuilder();
generateBSTString(root,0,res);
return res.toString();
}
// node , depth
private void generateBSTString(Node node, int depth, StringBuilder res) {
if (node == null){
res.append(generateDepthString(depth)+"null
");
return;
}
res.append(generateDepthString(depth) + node.e + "
");
generateBSTString(node.left, depth+1, res);
generateBSTString(node.right, depth+1, res);
}
private String generateDepthString(int depth) {
StringBuilder res = new StringBuilder();
for (int i = 0; i < depth; i++) {
res.append("--");
}
return res.toString();
}
//
public E minimum(){
if (size == 0)
throw new IllegalArgumentException("BST is empty!");
return minimum(root).e;
}
private Node minimum(Node node){
if (node.left == null)
return node;
return minimum(node.left);
}
//
public E maximum(){
if (size == 0)
throw new IllegalArgumentException("BST is empty!");
return maximum(root).e;
}
private Node maximum(Node node){
if (node.right == null)
return node;
return maximum(node.right);
}
// ,
public E removeMin(){
E ret = minimum();
root = removeMin(root);
return ret;
}
private Node removeMin(Node node){
if (node.left == null){
Node rightNode = node.right;
node.right = null;
size -- ;
return rightNode;
}
node.left = removeMin(node.left);
return node;
}
// ,
public E removeMax(){
E ret = maximum();
root = removeMax(root);
return ret;
}
private Node removeMax(Node node){
if (node.right == null){
Node leftNode = node.left;
node.left = null;
size -- ;
return leftNode;
}
node.right = removeMax(node.right);
return node;
}
// e
public void remove(E e){
root = remove(root, e);
}
private Node remove(Node node, E e){
if (node == null){
return null;
}
if (e.compareTo(node.e) < 0){
node.left = remove(node.left, e);
return node;
}
else if (e.compareTo(node.e) > 0){
node.right = remove(node.right, e);
return node;
}
else { // e == node.e
if (node.left == null){
Node rightNode = node.right;
node.right = null;
size --;
return rightNode;
}
if (node.right == null){
Node leftNode = node.left;
node.left = null;
size --;
return leftNode;
}
Node successor = minimum(node.right);
successor.right = removeMin(node.right);
successor.left = node.left;
node.left = node.right = null;
return successor;
}
}
}
テストコードは次のとおりです.
BST bst = new BST<>();
for (int i = 0; i < 100; i++) {
bst.add(new Random().nextInt(1000));
}
//
bst.perOrder();
System.out.println();
//
bst.inOrder();
System.out.println();
//
bst.postOrder();
System.out.println();
System.out.println(bst.toString());
}