a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

C#實現二叉排序樹代碼實例

2019-10-29 19:58:13
字體:
來源:轉載
供稿:網友

二叉排序樹,又稱為二叉查找樹。它或者是一顆空樹,或者是具有下列性質的二叉樹:

  • 若它的左子樹不為空。則左子樹上所有的結點的值均小于跟的結點值
  • 若它的右子樹部位空,則右子樹的所有結點值均大于它的根結點的值
  • 它的左右子樹也分別是二叉排序樹

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

C#,二叉排序樹,代碼

C#鏈式存儲二叉排序樹,實現簡單的排序,以及查找,具體代碼如下:

namespace _2_1_3二叉排序樹{  /// <summary>  /// 結點類  /// </summary>  class BSNode  {    //結點    public BSNode LeftChild { get; set; }    public BSNode RightChild { get; set; }    public BSNode Parent { get; set; }    public int Data { get; set; }    // 構造方法    public BSNode(){}    public BSNode(int item)    {      this.Data = item;    }  }}using System;namespace _2_1_3二叉排序樹{  /// <summary>  /// 二叉排序樹  /// </summary>  class BSTree  {    BSNode root = null;    /// <summary>    /// 添加數據    /// </summary>    public void Add(int item)    {      //創建新結點      BSNode newNode = new BSNode(item);      if (root == null)     //若為空,則創建為根結點      {        root = newNode;      }      else      {        BSNode temp = root;        while (true)        {          if (item >= temp.Data) //放在temp結點的右邊          {            if (temp.RightChild == null)            {              temp.RightChild = newNode;              newNode.Parent = temp;              break;            }            else            {              temp = temp.RightChild;            }          }          else          //放在temp結點的左邊          {            if (temp.LeftChild == null)            {              temp.LeftChild = newNode;              newNode.Parent = temp;              break;            }            else            {              temp = temp.LeftChild;            }          }        }      }    }    /// <summary>    /// 中序遍歷二叉樹    /// </summary>    public void MiddleBianli()    {      MiddleBianli(root);    }    //遞歸方式中序遍歷樹    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      {        //利用二叉排序樹的便利        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)    {      //葉子結點,即無子樹情況      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;      }      //只有右子樹的情況      if (node.LeftChild == null && node.RightChild != null)      {        node.Data = node.RightChild.Data;        node.RightChild = null;        return;      }      //只有左子樹的情況      if (node.LeftChild != null && node.RightChild == null)      {        node.Data = node.LeftChild.Data;        node.LeftChild = null;        return;      }      //刪除的結點有左,右子樹      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二叉排序樹{  /// <summary>  /// 測試類  /// </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("中序遍歷的結果:");      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("刪除根結點后的結果:");      tree.Delete(62);      tree.MiddleBianli();      Console.ReadKey();    }  }}

C#,二叉排序樹,代碼

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄色电影在线免费看 | 国产一区精品 | 欧美激情综合色综合啪啪五月 | 日本免费高清视频 | 色中色在线视频 | 精品国产乱码久久久久久久 | 久久久久中文 | 成人深夜视频 | 视频一二区| 亚洲第一区在线 | 亚洲国产精品久久久久久女王 | 精品一区二区电影 | 一区二区三区四区国产 | 久久久久久亚洲精品 | 亚洲欧美中文日韩在线v日本 | 99国内精品久久久久久久 | 九色在线视频 | 日韩一区在线播放 | 免费一区二区三区 | 五月婷婷导航 | 好看的一级毛片 | 久久99精品久久久久久秒播放器 | 日韩中文在线播放 | 欧美精品99 | 日韩一区二区三区在线 | 日韩欧美国产精品一区二区三区 | 国产成人精品亚洲男人的天堂 | 99久久国产综合精品女不卡 | 91久色 | 亚洲欧洲一区二区 | 黄色网址免费在线播放 | 99亚洲视频 | eeuss影院一区二区三区 | 蜜桃视频网站在线观看 | 久久久水蜜桃 | 国产精品毛片一区二区在线看 | 99视频免费在线观看 | 天天草夜夜 | 精品久久国产 | 亚洲一区二区三区在线播放 | 黄a一级|