最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實(shí)現(xiàn)二叉排序樹(shù)代碼實(shí)例

 更新時(shí)間:2018年10月25日 12:01:39   作者:Czhenya  
今天小編就為大家分享一篇關(guān)于C#實(shí)現(xiàn)二叉排序樹(shù)代碼實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(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#鏈?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();
    }
  }
}

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

相關(guān)文章

  • 使用linq to xml修改app.config示例(linq讀取xml)

    使用linq to xml修改app.config示例(linq讀取xml)

    這篇文章主要介紹了使用linq to xml修改app.config示例,需要的朋友可以參考下
    2014-02-02
  • 一文搞懂c# await,async執(zhí)行流

    一文搞懂c# await,async執(zhí)行流

    這篇文章主要介紹了c# await,async執(zhí)行流的相關(guān)知識(shí),文章講解的非常細(xì)致,代碼幫助理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • WPF自定義TreeView控件樣式實(shí)現(xiàn)QQ聯(lián)系人列表效果

    WPF自定義TreeView控件樣式實(shí)現(xiàn)QQ聯(lián)系人列表效果

    TreeView控件在項(xiàng)目中使用比較頻繁,下面這篇文章主要給大家介紹了關(guān)于WPF自定義TreeView控件樣式實(shí)現(xiàn)QQ聯(lián)系人列表效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2018-04-04
  • C#中使用UDP通信的示例

    C#中使用UDP通信的示例

    這篇文章主要介紹了C#中使用UDP通信的示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2020-11-11
  • 詳解如何使用BenchmarkDotNet對(duì).NET代碼進(jìn)行性能基準(zhǔn)測(cè)試

    詳解如何使用BenchmarkDotNet對(duì).NET代碼進(jìn)行性能基準(zhǔn)測(cè)試

    BenchmarkDotNet是一個(gè)基于.NET開(kāi)源、功能全面、易于使用的性能基準(zhǔn)測(cè)試框架,這篇文章就來(lái)和小編一起學(xué)習(xí)一下如何使用BenchmarkDotNet對(duì).NET代碼進(jìn)行性能基準(zhǔn)測(cè)試吧
    2024-12-12
  • C#使用NPOI庫(kù)讀寫(xiě)Excel文件

    C#使用NPOI庫(kù)讀寫(xiě)Excel文件

    這篇文章主要為大家詳細(xì)介紹了C#使用NPOI庫(kù)讀寫(xiě)Excel文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C# 結(jié)合 Javascript 測(cè)試獲取天氣信息

    C# 結(jié)合 Javascript 測(cè)試獲取天氣信息

    本文將介紹如何使用 C# 并結(jié)合 JavaScript 獲取天氣信息,獲取的數(shù)據(jù)來(lái)源于360瀏覽器首頁(yè)數(shù)據(jù),對(duì)C# 獲取天氣信息示例代碼感興趣的朋友一起看看吧
    2024-08-08
  • C#操作txt文件,進(jìn)行清空添加操作的小例子

    C#操作txt文件,進(jìn)行清空添加操作的小例子

    這篇文章介紹了C#操作txt文件,進(jìn)行清空添加操作的小例子,有需要的朋友可以參考一下
    2013-09-09
  • c#將字節(jié)數(shù)組轉(zhuǎn)成易讀的字符串的實(shí)現(xiàn)

    c#將字節(jié)數(shù)組轉(zhuǎn)成易讀的字符串的實(shí)現(xiàn)

    這篇文章主要介紹了c#將字節(jié)數(shù)組轉(zhuǎn)成易讀的字符串的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • C# Winform 分頁(yè)功能的實(shí)現(xiàn)

    C# Winform 分頁(yè)功能的實(shí)現(xiàn)

    本文主要介紹了C# Winform 分頁(yè)功能的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06

最新評(píng)論

水城县| 陇南市| 尖扎县| 大冶市| 科技| 望都县| 康定县| 新化县| 武宁县| 巨鹿县| 京山县| 尖扎县| 淳安县| 凌海市| 秦安县| 灵璧县| 襄樊市| 喀什市| 金山区| 乐平市| 周宁县| 崇仁县| 阳江市| 视频| 丰原市| 黄冈市| 黔南| 安岳县| 昌乐县| 林口县| 建湖县| 兴海县| 洛阳市| 蚌埠市| 遵义市| 绥滨县| 塘沽区| 华安县| 蒙自县| 成都市| 河西区|