二叉排序树的查找、插入、删除(C#实现)

博客中代码都经过运行并且没有bug

    //用二叉链表结构构造二叉排序树
    class TreeNode//树的结点
    {
      public int key;
      public string treeNodeName;
      public TreeNode lChild, rChild;
    }

    class BiTree//二叉树
    {
      public TreeNode T;//树的头结点
    }

    //构建一颗二叉排序树
    private static BiTree InitBiTree()
    {
      TreeNode tn1 = new TreeNode();
      TreeNode tn2 = new TreeNode();
      TreeNode tn3 = new TreeNode();
      TreeNode tn4 = new TreeNode();

      tn1.key = 50;
      tn1.treeNodeName = "tn1";
      tn1.lChild = tn2;
      tn1.rChild = tn3;

      tn2.key = 30;
      tn2.treeNodeName = "tn2";
      tn2.lChild = null;
      tn2.rChild = tn4;

      tn3.key = 80;
      tn3.treeNodeName = "tn3";
      tn3.lChild = null;
      tn3.rChild = null;

      tn4.key = 40;
      tn4.treeNodeName = "tn4";
      tn4.lChild = null;
      tn4.rChild = null;

      BiTree bitree = new BiTree();
      bitree.T = tn1;

      return bitree;
    }


    //二叉排序树查找
    //T 二叉树的根 key 查找的结点的值 f 当前二叉树的父结点 p 查找到的结点(或没查找到的情况下指针最后指向的结点)
    private static bool SearchBST(TreeNode T, int key, TreeNode f, out TreeNode p)
    {
      if (T == null)//查找不成功
      {
        p = f;
        return false;
      }
      else if (key == T.key)//查找成功
      {
        p = T;
        return true;
      }
      else if (key < T.key)
      {
        return  SearchBST(T.lChild, key, T, out p);
      }
      else
      {
        return SearchBST(T.rChild, key, T, out p);
      }
    }

    //二叉排序树插入
    //T 二叉树的根 key 查找的结点的值 f 当前二叉树的父结点 p 查找到的结点(或没查找到的情况下指针最后指向的结点)
    private static bool InsertBST(BiTree biTree, TreeNode e)
    {
      TreeNode p;
      if (!SearchBST(biTree.T, e.key, null, out p))
      {
        TreeNode s;
        s = e;
        s.lChild = null;
        s.rChild = null;
        s.treeNodeName = "insertNode";
        if (p == null)
        {
          biTree.T = s;//插入为新的根节点
        }
        else if (e.key < p.key)
        {
          p.lChild = s;
        }
        else
        {
          p.rChild = s;
        }
        return true;
      }
      else
      {
        return false;
      }
    }


    //删除节点
    //isLeftChild T是dp结点的左子树 -1 T是dp结点的左子树 1 T没有父结点,即dp为空 0
    private static bool DeleteBST(TreeNode T, int key,TreeNode dp,int isLeftChild)
    {
      TreeNode p;
      if (!SearchBST(T, key, null, out p))
      {
        return false;//没有查找到对应的结点
      }
      else
      {
        if (key == T.key)
        {
          Delete(T, dp, isLeftChild);
        }
        else if (key < T.key)
        {
          DeleteBST(T.lChild, key, T,-1);
        }
        else
        {
          DeleteBST(T.rChild, key, T,1);
        }
        return true;
      }
    }

    //删除对应结点,并使表结构发生变化使其仍然为二叉排序树
    private static void Delete(TreeNode T, TreeNode dp, int isLeftChild)
    {
      if (T.lChild == null)
      {
        if (isLeftChild==-1)
        {
          dp.lChild = T.rChild;
        }
        else
        {
          dp.rChild = T.rChild;
        }
      }
      else if (T.rChild == null)
      {
        if (isLeftChild==-1)
        {
          dp.lChild = T.lChild;
        }
        else
        {
          dp.rChild = T.lChild;
        }
      }
      else
      {
        TreeNode pre,mov;
        pre = T;
        mov = T.lChild;
        while (mov.rChild != null)
        {
          pre = mov;
          mov = mov.rChild;
        }
        T.key = mov.key;
        T.treeNodeName = mov.treeNodeName;

        if (pre.lChild!=null && pre.lChild.key == mov.key)
        {
          T.lChild = mov.lChild;
        }
        else
        {
          pre.rChild = mov.lChild;
        }
      }
    }


    static void Main(string[] args)
    {
      BiTree biTree = InitBiTree();
      TreeNode p;

      //查找二叉排序树
      //bool isExist = SearchBST(biTree.T,31,null,out p);

      //Console.WriteLine("是否查找到了:"+isExist);
      //if (isExist)
      //  Console.WriteLine("查找到的结点为:"+p.treeNodeName);

      //插入二叉排序树

      //TreeNode e = new TreeNode();
      //e.key = 30;
      //e.lChild = null;
      //e.rChild = null;
      //e.treeNodeName = "insertNode";
      //bool isInsert = InsertBST(biTree,e);
      //Console.WriteLine("是否插入成功:" + isInsert);


      //删除二叉排序树
      bool isDelete = DeleteBST(biTree.T, 50, null, 0);
      Console.WriteLine("是否删除成功:"+ isDelete);

      Console.ReadKey();
    }

二叉排序树查找的时间复杂度:P(n)=2(n+1)/n*logn+c

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值