二叉搜索树之Java实现(2)

if (u.parent == null) {
   root = v;
  } else if (u == u.parent.left) {
   u.parent.left = v;
  } else {
   u.parent.right = v;
  }

if (v != null) {
   v.parent = u.parent;
  }
 }

public Node<T> search(T element) {
  if (element == null) {
   throw new IllegalArgumentException("element can not be null");
  }

Node<T> node = root;
  while (node != null) {
   if (node.value.equals(element)) {
    return node;
   } else if (element.compareTo(node.value) < 0) {
    node = node.left;
   } else {
    node = node.right;
   }
  }

return null;
 }

public Node<T> min(Node<T> rootNode) {
  if (rootNode == null) {
   throw new IllegalArgumentException("node can not be null");
  }

Node<T> node = rootNode;
  while (node.left != null) {
   node = node.left;
  }

return node;
 }

public Node<T> min() {
  if (root != null) {
   return min(root);
  } else {
   return null;
  }
 }

public Node<T> max(Node<T> rootNode) {
  if (rootNode == null) {
   throw new IllegalArgumentException("node can not be null");
  }

Node<T> node = rootNode;
  while (node.right != null) {
   node = node.right;
  }

return node;
 }

public Node<T> max() {
  if (root != null) {
   return max(root);
  } else {
   return null;
  }
 }

public Node<T> successor(Node<T> node) {
  if (node == null) {
   throw new IllegalArgumentException("node can not be null");
  }

if (node.right != null) {
   return min(node.right);
  }

Node<T> processNode = node;
  Node<T> parent = processNode.parent;
  while (parent != null && processNode == parent.right) {
   processNode = parent;
   parent = processNode.parent;
  }

return parent;
 }

public Node<T> predecesssor(Node<T> node) {
  if (node == null) {
   throw new IllegalArgumentException("node can not be null");
  }

if (node.left != null) {
   return max(node.left);
  }

Node<T> processNode = node;
  Node<T> parent = processNode.parent;
  while (parent != null && processNode == parent.left) {
   processNode = parent;
   parent = processNode.parent;
  }

return parent;
 }

public void print() {
  print(root);
 }

public void print(Node<T> node) {
  if (node == null) {
   return;
  }

print(node.left);
  System.out.print("  " + node.value.toString() + "  ");
  print(node.right);
 }

public static class Node<T extends Comparable<T>> {
  private Node<T> parent;
  private Node<T> left;
  private Node<T> right;

private T value;

public Node(Node<T> parent, T value) {
   this.parent = parent;
   this.value = value;
  }

public Node<T> getParent() {
   return parent;
  }

public void setParent(Node<T> parent) {
   this.parent = parent;
  }

public Node<T> getLeft() {
   return left;
  }

public void setLeft(Node<T> left) {
   this.left = left;
  }

public Node<T> getRight() {
   return right;
  }

public void setRight(Node<T> right) {
   this.right = right;
  }

public T getValue() {
   return value;
  }

public void setValue(T value) {
   this.value = value;
  }
 }

public static void main(String[] args) {
  BinaryTree<String> tree = new BinaryTree<String>();

tree.insert("Hello");
  tree.insert("World");
  tree.insert("Money");

tree.print();
  System.out.println();

Node<String> moneyNode = tree.search("Money");
  tree.print(moneyNode);
  System.out.println();

tree.insert("Like");
  tree.print(moneyNode);
  System.out.println();

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/2b23e261d8fc1aea130fcf5d49e9fd0d.html