程序人生  
我们站在同一起跑线上,让我们共同努力,共同奋进,愿您的人生因程序而美好!
日历
<2025年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789
统计
  • 随笔 - 0
  • 文章 - 38
  • 评论 - 8
  • 引用 - 0

导航

留言簿(2)

文章分类

文章档案

常用Web 站点

搜索

  •  

最新评论

 

package com.eruite.test;

class BinaryNode {
 Comparable element;

 BinaryNode left;

 BinaryNode right;

 BinaryNode(Comparable element) {
  this(element, null, null);
 }

 BinaryNode(Comparable element, BinaryNode left, BinaryNode right) {
  this.element = element;
  this.left = left;
  this.right = right;
 }
}

public class BinarySearchTree {
 private BinaryNode root;

 public BinarySearchTree() {
  root = null;
 }

 public void makeEmpty() {
  root = null;
 }

 public boolean isEmpty() {
  return root == null;
 }

 public Comparable find(Comparable x) {
  return elementAt(find(x, root));
 }

 public Comparable findMin() {
  return elementAt(findMin(root));
 }

 public Comparable findMax() {
  return elementAt(findMax(root));
 }

 public void insert(Comparable x) {
  root = insert(x, root);
 }

 public void remove(Comparable x) {
  root = remove(x, root);
 }

 public void printTree() {
  if (isEmpty())
   System.out.println("Empty Tree");
  else
   printTree(root);
 }

 private Comparable elementAt(BinaryNode t) {
  return t == null ? null : t.element;
 }

 private BinaryNode find(Comparable x, BinaryNode t) {
  if (t == null)
   return null;
  if (x.compareTo(t.element) < 0)
   return find(x, t.left);
  else if (x.compareTo(t.element) > 0)
   return find(x, t.right);
  else
   return t;
 }

 private BinaryNode findMin(BinaryNode t) {
  if (t == null)
   return null;
  else if (t.left == null)
   return t;
  return findMin(t.left);
 }

 private BinaryNode findMax(BinaryNode t) {
  if (t != null) {
   while (t.right != null)
    t = t.right;
  }
  return t;
 }

 private BinaryNode insert(Comparable x, BinaryNode t) {
  if (t == null)
   t = new BinaryNode(x);
  else if (x.compareTo(t.element) < 0)
   t.left = insert(x, t.left);
  else if (x.compareTo(t.element) > 0)
   t.right = insert(x, t.right);
  else
   ;
  return t;
 }

 private BinaryNode remove(Comparable x, BinaryNode t) {
  if (t == null)
   return t;
  if (x.compareTo(t.element) < 0)
   t.left = remove(x, t.left);
  else if (x.compareTo(t.element) > 0)
   t.right = remove(x, t.right);
  else if (t.left != null && t.right != null) {
   t.element = findMin(t.right).element;
   t.right = remove(x, t.right);
  } else
   t = (t.left != null) ? t.left : t.right;
  return t;
 }

 private void printTree(BinaryNode t) {
  if (t != null) {
   printTree(t.left);
   System.out.print(t.element);
   printTree(t.right);
  }
 }

 public static void main(String[] args) {
  BinarySearchTree bst = new BinarySearchTree();
  bst.insert(new Integer(1));
  bst.insert(new Integer(2));
  bst.insert(new Integer(3));
  bst.insert(new Integer(4));
  bst.insert(new Integer(5));
  bst.insert(new Integer(6));
  Comparable c = bst.find(new Integer(6));
  if (c != null)
   System.out.print(c);
  else
   System.out.println("not exist");
 }
}

posted on 2008-03-28 17:55 蔡华林 阅读(364) 评论(0)  编辑  收藏 所属分类: j2se
 
Copyright © 蔡华林 Powered by: 博客园 模板提供:沪江博客