简单二叉树的实现

一个简单二叉树的实现,来自http://zhouyunan2010.iteye.com/blog/1255299

package searcher.performance;

import java.util.Iterator;
import java.util.NoSuchElementException;


public class BinarySortTree<E> {

 
 private Entry<E> root = null;

 
 private int size = 0;

 public BinarySortTree() {

 }

 public int size() {
  return size;
 }

 public E getRoot() {
  return root == null ? null : root.element;
 }

 
 private boolean searchBST(Entry<E> t, Object element, Entry<E> f, Entry<E> p) {
  if (t == null) {
   p = f;
   return false;
  }
  Comparable<? super E> e = (Comparable<? super E>) element;
  int cmp = e.compareTo(t.element);
  if (cmp < 0) {
   return searchBST(t.left, element, t, p);
  } else if (cmp > 0) {
   return searchBST(t.right, element, t, p);
  } else {
   p = t;
   return true;
  }
 }

 
 private boolean searchBST(Object element, Entry[] p) {
  Comparable<? super E> e = (Comparable<? super E>) element;
  Entry<E> parent = root;
  Entry<E> pp = null; // 保存parent父节点
  while (parent != null) {
   int cmp = e.compareTo(parent.element);
   pp = parent;
   if (cmp < 0) {
    parent = parent.left;
   } else if (cmp > 0) {
    parent = parent.right;
   } else {
    p[0] = parent;
    return true;
   }
  }
  p[0] = pp;
  return false;
 }

 
 public boolean add(E element) {
  Entry<E> t = root;
  if (t == null) { // 如果根节点为空
   root = new Entry<E>(element, null);
   size = 1;
   return false;
  }
  Comparable<? super E> e = (Comparable<? super E>) element;
  Entry[] p = new Entry[1];
  if (!searchBST(element, p)) { // 查找失败,插入元素
   Entry<E> s = new Entry<E>(element, p[0]);
   System.out.println("------------->" + p[0].element + element);
   int cmp = e.compareTo((E) p[0].element);
   System.out.println("--->" + cmp + "element" + element + "e" + e
     + "p[0].element" + p[0].element);
   if (cmp < 0) {
    p[0].left = s;
   }
   if (cmp > 0) {
    p[0].right = s;
   }
   size++;
   return true;
  }
  return false;
 }

 
 public boolean remove(Object o) {
  Entry[] p = new Entry[1];
  if (searchBST(o, p)) { // 查找成功,删除元素
   deleteEntry(p[0]);
   return true;
  }
  return false;
 }

 private void deleteEntry(Entry<E> p) {
  size--;
  // 如果p左右子树都不为空,找到其直接后继,替换p
  if (p.left != null && p.right != null) {
   Entry<E> s = successor(p);
   p.element = s.element;
   p = s;
  }
  Entry<E> replacement = (p.left != null ? p.left : p.right);

  if (replacement != null) { // 如果其左右子树有其一不为空
   replacement.parent = p.parent;
   if (p.parent == null) // 如果p为root节点
    root = replacement;
   else if (p == p.parent.left) // 如果p是左孩子
    p.parent.left = replacement;
   else
    // 如果p是右孩子
    p.parent.right = replacement;

   p.left = p.right = p.parent = null; // p的指针清空

  } else if (p.parent == null) { // 如果全树只有一个节点
   root = null;
  } else { // 左右子树都为空,p为叶子节点
   if (p.parent != null) {
    if (p == p.parent.left)
     p.parent.left = null;
    else if (p == p.parent.right)
     p.parent.right = null;
    p.parent = null;
   }
  }

 }

 
 static <E> Entry<E> successor(Entry<E> t) {
  if (t == null)
   return null;
  else if (t.right != null) { // 往右,然后向左直到尽头
   Entry<E> p = t.right;
   while (p.left != null)
    p = p.left;
   return p;
  } else { // right为空,如果t是p的左子树,则p为t的直接后继
   Entry<E> p = t.parent;
   Entry<E> ch = t;
   while (p != null && ch == p.right) { // 如果t是p的右子树,则继续向上搜索其直接后继
    ch = p;
    p = p.parent;
   }
   return p;
  }
 }

 public Iterator<E> itrator() {
  return new BinarySortIterator();
 }

 // 返回中序遍历此树的迭代器
 private class BinarySortIterator implements Iterator<E> {
  Entry<E> next;
  Entry<E> lastReturned;

  public BinarySortIterator() {
   Entry<E> s = root;
   if (s != null) {
    while (s.left != null) { // 找到中序遍历的第一个元素
     s = s.left;
    }
   }
   next = s; // 赋给next
  }

  @Override
  public boolean hasNext() {
   return next != null;
  }

  @Override
  public E next() {
   Entry<E> e = next;
   if (e == null)
    throw new NoSuchElementException();
   next = successor(e);
   lastReturned = e;
   return e.element;
  }

  @Override
  public void remove() {
   if (lastReturned == null)
    throw new IllegalStateException();
   // deleted entries are replaced by their successors
   if (lastReturned.left != null && lastReturned.right != null)
    next = lastReturned;
   deleteEntry(lastReturned);
   lastReturned = null;
  }
 }

 
 static class Entry<E> {
  E element;
  Entry<E> parent;
  Entry<E> left;
  Entry<E> right;

  public Entry(E element, Entry<E> parent) {
   this.element = element;
   this.parent = parent;
  }

  public Entry() {
  }
 }

 // just for test
 public static void main(String[] args) {
  BinarySortTree<Integer> tree = new BinarySortTree<Integer>();
  tree.add(45);
  tree.add(24);
  tree.add(53);
  tree.add(45);
  tree.add(12);
  tree.add(90);

  System.out.println(tree.remove(400));
  System.out.println(tree.remove(45));
  System.out.println("root=" + tree.getRoot());
  Iterator<Integer> it = tree.itrator();
  while (it.hasNext()) {
   System.out.println(it.next());
  }
  System.out.println(tree.size());
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值