欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

C#如何實(shí)現(xiàn)二叉排序樹(shù)

這篇文章主要介紹了C#如何實(shí)現(xiàn)二叉排序樹(shù),具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

成都創(chuàng)新互聯(lián)公司-專(zhuān)業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性?xún)r(jià)比高安網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式高安網(wǎng)站制作公司更省心,省錢(qián),快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋高安地區(qū)。費(fèi)用合理售后完善,十年實(shí)體公司更值得信賴(lài)。

二叉排序樹(shù),又稱(chēng)為二叉查找樹(shù)。它或者是一顆空樹(shù),或者是具有下列性質(zhì)的二叉樹(shù):

  • 若它的左子樹(shù)不為空。則左子樹(shù)上所有的結(jié)點(diǎn)的值均小于跟的結(jié)點(diǎn)值

  • 若它的右子樹(shù)部位空,則右子樹(shù)的所有結(jié)點(diǎn)值均大于它的根結(jié)點(diǎn)的值

  • 它的左右子樹(shù)也分別是二叉排序樹(shù)

1,排序方便
2,查找方便
3,便于插入和刪除

C#如何實(shí)現(xiàn)二叉排序樹(shù)

C#鏈?zhǔn)酱鎯?chǔ)二叉排序樹(shù),實(shí)現(xiàn)簡(jiǎn)單的排序,以及查找,具體代碼如下:

namespace _2_1_3二叉排序樹(shù)
{
  /// <summary>
  /// 結(jié)點(diǎn)類(lèi)
  /// </summary>
  class BSNode
  {
    //結(jié)點(diǎn)
    public BSNode LeftChild { get; set; }
    public BSNode RightChild { get; set; }
    public BSNode Parent { get; set; }
    public int Data { get; set; }
    // 構(gòu)造方法
    public BSNode(){}
    public BSNode(int item)
    {
      this.Data = item;
    }
  }
}
using System;
namespace _2_1_3二叉排序樹(shù)
{
  /// <summary>
  /// 二叉排序樹(shù)
  /// </summary>
  class BSTree
  {
    BSNode root = null;
    /// <summary>
    /// 添加數(shù)據(jù)
    /// </summary>
    public void Add(int item)
    {
      //創(chuàng)建新結(jié)點(diǎn)
      BSNode newNode = new BSNode(item);
      if (root == null)     //若為空,則創(chuàng)建為根結(jié)點(diǎn)
      {
        root = newNode;
      }
      else
      {
        BSNode temp = root;
        while (true)
        {
          if (item >= temp.Data) //放在temp結(jié)點(diǎn)的右邊
          {
            if (temp.RightChild == null)
            {
              temp.RightChild = newNode;
              newNode.Parent = temp;
              break;
            }
            else
            {
              temp = temp.RightChild;
            }
          }
          else          //放在temp結(jié)點(diǎn)的左邊
          {
            if (temp.LeftChild == null)
            {
              temp.LeftChild = newNode;
              newNode.Parent = temp;
              break;
            }
            else
            {
              temp = temp.LeftChild;
            }
          }
        }
      }
    }
    /// <summary>
    /// 中序遍歷二叉樹(shù)
    /// </summary>
    public void MiddleBianli()
    {
      MiddleBianli(root);
    }
    //遞歸方式中序遍歷樹(shù)
    private void MiddleBianli(BSNode node)
    {
      if (node == null) return;
      MiddleBianli(node.LeftChild);
      Console.Write(node.Data + " ");
      MiddleBianli(node.RightChild);
    }
    /// <summary>
    ///查找方法-1
    /// </summary>
    public bool Find1(int item)
    {
      return Find(item, root);
    }
    private bool Find(int item, BSNode node)
    {
      if (node == null) { return false; }
      if (node.Data == item)
      {
        return true;
      }
      else
      {
        //利用二叉排序樹(shù)的便利
        if (item > node.Data)
        {
          return Find(item, node.RightChild);
        }
        else
        {
          return Find(item, node.LeftChild);
        }
      }
    }
    /// <summary>
    /// 查找方法-2
    /// </summary>
    /// <param name="item"></param>
    /// <returns></returns>
    public bool Find2(int item)
    {
      BSNode temp = root;
      while (true)
      {
        if (temp == null) return false;
        if (temp.Data == item) return true;
        if (item > temp.Data)
        {
          temp = temp.RightChild;
        }
        else
        {
          temp = temp.LeftChild;
        }
      }
    }
    public bool Delete(int item)
    {
      BSNode temp = root;
      while (true)
      {
        if (temp == null) return false;
        if (temp.Data == item)
        {
          Delete(temp);
          return true;
        }
        if (item > temp.Data)
        {
          temp = temp.RightChild;
        }
        else
        {
          temp = temp.LeftChild;
        }
      }
    }
    public void Delete(BSNode node)
    {
      //葉子結(jié)點(diǎn),即無(wú)子樹(shù)情況
      if (node.LeftChild == null && node.RightChild == null)
      {
        if (node.Parent == null)
        {
          root = null;
        }
        else if (node.Parent.LeftChild == node)
        {
          node.Parent.LeftChild = null;
        }
        else if (node.Parent.RightChild == node)
        {
          node.Parent.RightChild = null;
        }
        return;
      }
      //只有右子樹(shù)的情況
      if (node.LeftChild == null && node.RightChild != null)
      {
        node.Data = node.RightChild.Data;
        node.RightChild = null;
        return;
      }
      //只有左子樹(shù)的情況
      if (node.LeftChild != null && node.RightChild == null)
      {
        node.Data = node.LeftChild.Data;
        node.LeftChild = null;
        return;
      }
      //刪除的結(jié)點(diǎn)有左,右子樹(shù)
      BSNode temp = node.RightChild;
      while (true)
      {
        if (temp.LeftChild != null)
        {
          temp = temp.LeftChild;
        }
        else
        {
          break;
        }
      }
      node.Data = temp.Data;
      Delete(temp);
    }
  }
}
using System;
namespace _2_1_3二叉排序樹(shù)
{
  /// <summary>
  /// 測(cè)試類(lèi)
  /// </summary>
  class Program
  {
    static void Main(string[] args)
    {
      BSTree tree = new BSTree();
      int[] data = {62,58,28,47,73,99,35,51,93,37 };

      foreach (int item in data)
      {
        tree.Add(item);
      }
      Console.Write("中序遍歷的結(jié)果:");
      tree.MiddleBianli();
      Console.WriteLine();
      Console.WriteLine("Find-1方法查找62是否存在:" + tree.Find1(62));
      Console.WriteLine("Find-2方法查找62是否存在:" + tree.Find2(62));
      Console.WriteLine("Find-1方法查找63是否存在:" + tree.Find1(63)); 
      Console.WriteLine("Find-2方法查找63是否存在:" + tree.Find2(63));
      Console.WriteLine("刪除根結(jié)點(diǎn)后的結(jié)果:");
      tree.Delete(62);
      tree.MiddleBianli();
      Console.ReadKey();
    }
  }
}

C#如何實(shí)現(xiàn)二叉排序樹(shù)

C#是什么

C#是一個(gè)簡(jiǎn)單、通用、面向?qū)ο蟮木幊陶Z(yǔ)言,它由微軟Microsoft開(kāi)發(fā),繼承了C和C++強(qiáng)大功能,并且去掉了一些它們的復(fù)雜特性,C#綜合了VB簡(jiǎn)單的可視化操作和C++的高運(yùn)行效率,以其強(qiáng)大的操作能力、優(yōu)雅的語(yǔ)法風(fēng)格、創(chuàng)新的語(yǔ)言特性和便捷的面向組件編程從而成為.NET開(kāi)發(fā)的首選語(yǔ)言,但它不適用于編寫(xiě)時(shí)間急迫或性能非常高的代碼,因?yàn)镃#缺乏性能極高的應(yīng)用程序所需要的關(guān)鍵功能。

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“C#如何實(shí)現(xiàn)二叉排序樹(shù)”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

當(dāng)前標(biāo)題:C#如何實(shí)現(xiàn)二叉排序樹(shù)
文章源于:http://aaarwkj.com/article6/phddig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、響應(yīng)式網(wǎng)站、網(wǎng)站收錄網(wǎng)站設(shè)計(jì)公司、做網(wǎng)站商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)
国产伦国产一区二区三区在线观看| 国产l精品国产亚洲区久久 | 国产国语久久91老女人| 中出亚洲精品日韩在线视频 | 国产高清毛片区1区二区三区| 日韩中文字幕在线乱码| 亚洲欧美丝袜清纯另类| 精品人妻一区三区蜜桃| 啄木乌法国一区二区三区| 色综合色综合蘑菇在线| 刚出嫁新婚少妇很紧很爽| 日本人妻内射一区二区| 亚洲一区日韩精品电影| 国产超码片内射在线| 人妻少妇中文字幕一区| 日韩三级av黄片在线| 成人高清在线观看91| 亚洲欧美午夜不卡视频| 丁香六月五月色婷婷网| 91日本精品一区二区| 欧美亚洲伊人久久综合| 国产成人精品久久一区二区三区 | 久久精品熟女亚洲av韩国| 99精品亚洲一区二区| 欧美日韩中文字幕精品视频| 香蕉视频在线观看亚洲精品| 在线免费观看午夜视频| 日本加勒比不卡在线视频| 日本经典三级在线视频| 日日夜夜天天操天天干| 青青草国产精品一区二区| 中文字幕av免费专区| 日韩精品成人一区二区三区免费| 九九在线视频免费观看精品视频| 日韩色图在线观看视频| 精品人妻少妇av一区二区| 日韩精品视频播放一区| 亚洲男人天堂中文字幕| 日韩欧美国产精品自拍| 97久久精品人妻一区二区三区| 不卡一区二区国产精品|