二叉查找树书中实现

来源:互联网 发布:北电网络被谁收购 编辑:程序博客网 时间:2024/06/06 06:35

  

在二叉排序树插入结点的算法

向一个二叉排序树b中插入一个结点s的算法,过程为:

1.     b是空树,则将s所指结点作为根结点插入,否则:

2.     s->data小于b的根结点的数据域之值,则把s所指结点插入到左子树中,否则:

3.     s所指结点插入到右子树中。

 

public void addElement (T element)

   {

      BinaryTreeNode<T> temp = new BinaryTreeNode<T> (element);

      Comparable<T> comparableElement = (Comparable<T>)element;

 

      if (isEmpty())

         root = temp;

      else

      {

         BinaryTreeNode<T> current = root;

         boolean added = false;

 

         while (!added)

         {

            if (comparableElement.compareTo(current.element) < 0)

            {

               if (current.left == null)

               {

                  current.left = temp;

                  added = true;

               }

               else

                  current = current.left;

            }

            else

            {

               if (current.right == null)

               {

                  current.right = temp;

                  added = true;

               }

               else

                  current = current.right;

            }

         }

      }

      count++;

   }

 

在二叉排序树删除结点的算法

在二叉排序树删去一个结点,分三种情况讨论:

1.    如果被删除的结点没有孩子,则replacement返回null

2.    如果被删除的节点只有一个孩子,则replacement返回这个孩子

3.    如果被删除的节点有两个孩子,则replacement会返回中序后继者

  public T removeElement (T targetElement)

                          throws ElementNotFoundException

   {

      T result = null;

 

      if (!isEmpty())

      {

         if (((Comparable)targetElement).equals(root.element))

         {

            result =  root.element;

            root = replacement (root);

            count--;

         }

         else

         {

            BinaryTreeNode<T> current, parent = root;

            boolean found = false;

 

            if (((Comparable)targetElement).compareTo(root.element) < 0)

               current = root.left;

            else

               current = root.right;

 

            while (current != null && !found)

            {

               if (targetElement.equals(current.element))

               {

                  found = true;

                  count--;

                  result =  current.element;

         

                  if (current == parent.left)

                  {

                     parent.left = replacement (current);

                  }

                  else

                  {

                     parent.right = replacement (current);

                  }

               }

               else

               {

                  parent = current;

        

                  if (((Comparable)targetElement).compareTo(current.element) < 0)

                     current = current.left;

                  else

                     current = current.right;

               }

            } //while

           

            if (!found)

               throw new ElementNotFoundException("binary search tree");

         }

      } // end outer if

 

      return result;

   }

 

 

protected BinaryTreeNode<T> replacement (BinaryTreeNode<T> node)

   {

      BinaryTreeNode<T> result = null;

 

      if ((node.left == null)&&(node.right==null))

         result = null;

     

      else if ((node.left != null)&&(node.right==null))

         result = node.left;

     

      else if ((node.left == null)&&(node.right != null))

         result = node.right;

     

      else

      {

         BinaryTreeNode<T> current = node.right;

         BinaryTreeNode<T> parent = node;

        

         while (current.left != null)

         {

            parent = current;

            current = current.left;

         }

        

         if (node.right == current)

            current.left = node.left;

        

         else

         {

            parent.left = current.right;

            current.right = node.right;

            current.left = node.left;

         }

        

         result = current;

      }

     

      return result;

   }

 

 

查找指定元素节点

 

public T find(T targetElement) throws ElementNotFoundException

   {

      BinaryTreeNode<T> current = findAgain( targetElement, root );

     

      if( current == null )

         throw new ElementNotFoundException("binary tree");

     

      return (current.element);

   }

 

 

 

   private BinaryTreeNode<T> findAgain(T targetElement,

                                       BinaryTreeNode<T> next)

   {

      if (next == null)

         return null;

     

      if (next.element.equals(targetElement))

         return next;

     

      BinaryTreeNode<T> temp = findAgain(targetElement, next.left);

     

      if (temp == null)

         temp = findAgain(targetElement, next.right);

     

      return temp;

   }

原创粉丝点击