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

C++實(shí)現(xiàn)二叉樹(shù)基本操作詳解

 更新時(shí)間:2017年12月06日 11:30:34   作者:tttjp  
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)二叉樹(shù)基本操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

樹(shù)是一種重要的非線性數(shù)據(jù)結(jié)構(gòu),二叉樹(shù)是樹(shù)型結(jié)構(gòu)的一種重要類型。本學(xué)年論文介紹了二叉樹(shù)的定義,二叉樹(shù)的存儲(chǔ)結(jié)構(gòu),二叉樹(shù)的相關(guān)術(shù)語(yǔ),以此引入二叉樹(shù)這一概念,為展開(kāi)二叉樹(shù)的基本操作做好理論鋪墊。二叉樹(shù)的基本操作主要包含以下幾個(gè)模塊:二叉樹(shù)的遍歷方法,計(jì)算二叉樹(shù)的結(jié)點(diǎn)個(gè)數(shù),計(jì)算二叉樹(shù)的葉子結(jié)點(diǎn)個(gè)數(shù),二叉樹(shù)深度的求解等內(nèi)容。

前序遍歷(遞歸&非遞歸)

  • 訪問(wèn)根節(jié)點(diǎn)
  • 前序訪問(wèn)左子樹(shù)
  • 前序訪問(wèn)右子樹(shù)
//前序非遞歸
  void PrevOrder()
  {
    stack<Node*> s;
    Node *cur = _root;

    while (cur || !s.empty())
    {
      while (cur)
      {
        cout << cur->_data << " ";
        s.push(cur);
        cur = cur->_left;
      }
      //此時(shí)當(dāng)前節(jié)點(diǎn)的左子樹(shù)已遍歷完畢
      Node *tmp = s.top();
      s.pop();
      cur = tmp->_right;
    }
    cout << endl;
  }

  //前序遞歸
  void PrevOrderR()
  {
    _PrevOrder(_root);

    cout << endl;
  }

  void _PrevOrder(Node *root)
  {
    if (root == NULL) //必須有遞歸出口!!!
      return;

    cout << root->_data << " ";
    _PrevOrder(root->_left);
    _PrevOrder(root->_right);
  }

中序遍歷(遞歸&非遞歸)

  • 中序訪問(wèn)左子樹(shù)
  • 訪問(wèn)根節(jié)點(diǎn)
  • 中序訪問(wèn)右子樹(shù)
//中序非遞歸
  void InOrder()
  {
    stack<Node*> s;
    Node *cur = _root;

    while (cur || !s.empty())
    {
      while (cur)
      {
        s.push(cur);
        cur = cur->_left;
      }
      //此時(shí)當(dāng)前節(jié)點(diǎn)的左子樹(shù)已遍歷完畢
      Node *tmp = s.top();
      cout << tmp->_data << " ";
      s.pop();
      cur = tmp->_right;
    }
    cout << endl;
  }

  //中序遞歸
  void InOrderR()
  {
    _InOrder(_root);

    cout << endl;
  }

  void _InOrder(Node *root)
  {
    if (root == NULL)
      return;

    _InOrder(root->_left);
    cout << root->_data << " ";
    _InOrder(root->_right);
  }

后序遍歷(遞歸&非遞歸)

  //后序非遞歸
  //后序遍歷可能會(huì)出現(xiàn)死循環(huán),所以要記錄下前一個(gè)訪問(wèn)的節(jié)點(diǎn)
  void PostOrder()
  {
    stack<Node*> s;
    Node *cur = _root;
    Node *prev = NULL;

    while (cur || !s.empty())
    {
      while (cur)
      {
        s.push(cur);
        cur = cur->_left;
      }
      Node *tmp = s.top();
      if (tmp->_right && tmp->_right != prev)
      {
        cur = tmp->_right;
      }
      else
      {
        cout << tmp->_data << " ";
        prev = tmp;
        s.pop();
      }
    }
    cout << endl;
  }

  //后序遞歸
  void PostOrderR()
  {
    _PostOrder(_root);

    cout << endl;
  }

  void _PostOrder(Node *root)
  {
    if (root == NULL)
      return;

    _PostOrder(root->_left);
    _PostOrder(root->_right);
    cout << root->_data << " ";
  }

層序遍歷

從根節(jié)點(diǎn)開(kāi)始,依次訪問(wèn)每層結(jié)點(diǎn)。
利用隊(duì)列先進(jìn)先出的特性,把每層結(jié)點(diǎn)從左至右依次放入隊(duì)列。

 void LevelOrder() //利用隊(duì)列!??!
  {
    queue<Node*> q;
    Node *front = NULL;

    //1.push根節(jié)點(diǎn)
    if (_root)  
    {
      q.push(_root);
    }
    //2.遍歷當(dāng)前節(jié)點(diǎn),push當(dāng)前節(jié)點(diǎn)的左右孩子,pop當(dāng)前節(jié)點(diǎn)
    //3.遍歷當(dāng)前節(jié)點(diǎn)的左孩子,再遍歷右孩子,循環(huán)直至隊(duì)列為空
    while (!q.empty())
    {

      front = q.front();
      cout << front->_data << " ";

      if (front->_left)
        q.push(front->_left);
      if (front->_right)
        q.push(front->_right);

      q.pop();
    }

    cout << endl;
  }

求二叉樹(shù)的高度

  size_t Depth()
  {
    return _Depth(_root);
  }

  size_t _Depth(Node *root)
  {
    if (root == NULL)
      return 0;
    else if (root->_left == NULL && root->_right == NULL)
      return 1;
    else
    {
      size_t leftDepth = _Depth(root->_left) + 1;
      size_t rightDepth = _Depth(root->_right) + 1;
      return leftDepth > rightDepth ? leftDepth : rightDepth;
    }
  }

求葉子節(jié)點(diǎn)的個(gè)數(shù)

size_t LeafSize()
  {
    return _LeafSize(_root);
  }

  size_t _LeafSize(Node *root)
  {
    if (root == NULL)
      return 0;
    else if (root->_left == NULL && root->_right == NULL)
      return 1;
    else
      return _LeafSize(root->_left) + _LeafSize(root->_right);
  }

求二叉樹(shù)第k層的節(jié)點(diǎn)個(gè)數(shù)

size_t GetKLevel(int k)
  {
    return _GetKLevel(_root, k);
  }

  size_t _GetKLevel(Node *root, int k)
  {
    if (root == NULL)
      return 0;
    else if (k == 1)
      return 1;
    else
      return _GetKLevel(root->_left, k - 1) + _GetKLevel(root->_right, k - 1);
  }

完整代碼如下:

template<class T>
struct BinaryTreeNode
{
  T _data;
  BinaryTreeNode *_left;
  BinaryTreeNode *_right;

  BinaryTreeNode(const T& d)
    :_data(d)
    , _left(NULL)
    , _right(NULL)
  {}
};

template<class T>
class BinaryTree
{
public:
  typedef BinaryTreeNode<T> Node;

  BinaryTree()
    :_root(NULL)
  {}

  BinaryTree(T *arr, size_t n, const T& invalid)
  {
    size_t index = 0;
    _root = _CreateBinaryTree(arr, n, invalid, index);
  }

  BinaryTree(const BinaryTree<T>& t)
    :_root(NULL)
  {
    _root = _CopyTree(t._root);
  }

  BinaryTree<T>& operator=(const BinaryTree<T>& t)
  {
    if (this != t)
    {
      Node *tmp = new Node(t._root);
      if (tmp != NULL)
      {
        delete _root;
        _root = tmp;
      }
    }
    return *this;
  }

  ~BinaryTree()
  {
    _DestroyTree(_root);
    cout << endl;
  }

  //前序非遞歸
  void PrevOrder()
  {
    stack<Node*> s;
    Node *cur = _root;

    while (cur || !s.empty())
    {
      while (cur)
      {
        cout << cur->_data << " ";
        s.push(cur);
        cur = cur->_left;
      }
      //此時(shí)當(dāng)前節(jié)點(diǎn)的左子樹(shù)已遍歷完畢
      Node *tmp = s.top();
      s.pop();
      cur = tmp->_right;
    }
    cout << endl;
  }

  //前序遞歸
  void PrevOrderR()
  {
    _PrevOrder(_root);

    cout << endl;
  }

  //中序非遞歸
  void InOrder()
  {
    stack<Node*> s;
    Node *cur = _root;

    while (cur || !s.empty())
    {
      while (cur)
      {
        s.push(cur);
        cur = cur->_left;
      }
      //此時(shí)當(dāng)前節(jié)點(diǎn)的左子樹(shù)已遍歷完畢
      Node *tmp = s.top();
      cout << tmp->_data << " ";
      s.pop();
      cur = tmp->_right;
    }
    cout << endl;
  }

  //中序遞歸
  void InOrderR()
  {
    _InOrder(_root);

    cout << endl;
  }

  //后序非遞歸
  //后序遍歷可能會(huì)出現(xiàn)死循環(huán),所以要記錄下前一個(gè)訪問(wèn)的節(jié)點(diǎn)
  void PostOrder()
  {
    stack<Node*> s;
    Node *cur = _root;
    Node *prev = NULL;

    while (cur || !s.empty())
    {
      while (cur)
      {
        s.push(cur);
        cur = cur->_left;
      }
      Node *tmp = s.top();
      if (tmp->_right && tmp->_right != prev)
      {
        cur = tmp->_right;
      }
      else
      {
        cout << tmp->_data << " ";
        prev = tmp;
        s.pop();
      }
    }
    cout << endl;
  }

  //后序遞歸
  void PostOrderR()
  {
    _PostOrder(_root);

    cout << endl;
  }

  void LevelOrder() //利用隊(duì)列?。?!
  {
    queue<Node*> q;
    Node *front = NULL;

    //1.push根節(jié)點(diǎn)
    if (_root)  
    {
      q.push(_root);
    }
    //2.遍歷當(dāng)前節(jié)點(diǎn),push當(dāng)前節(jié)點(diǎn)的左右孩子,pop當(dāng)前節(jié)點(diǎn)
    //3.遍歷當(dāng)前節(jié)點(diǎn)的左孩子,再遍歷右孩子,循環(huán)直至隊(duì)列為空
    while (!q.empty())
    {

      front = q.front();
      cout << front->_data << " ";

      if (front->_left)
        q.push(front->_left);
      if (front->_right)
        q.push(front->_right);

      q.pop();
    }

    cout << endl;
  }

  size_t Size()
  {
    return _Size(_root);
  }

  size_t LeafSize()
  {
    return _LeafSize(_root);
  }

  size_t GetKLevel(int k)
  {
    return _GetKLevel(_root, k);
  }

  size_t Depth()
  {
    return _Depth(_root);
  }

  Node* Find(const T& d)
  {
    return _Find(_root, d);
  }

protected:
  Node* _CreateBinaryTree(T *arr, size_t n, const T& invalid, size_t& index)
  {
    Node *root = NULL;
    if (index < n && arr[index] != invalid)
    {
      root = new Node(arr[index]);
      index++;
      root->_left = _CreateBinaryTree(arr, n, invalid, index);
      index++;
      root->_right = _CreateBinaryTree(arr, n, invalid, index);
    }
    return root;
  }

  Node* _CopyTree(Node *root)
  {
    Node *newRoot = NULL;

    if (root)
    {
      newRoot = new Node(root->_data);
      newRoot->_left = _CopyTree(root->_left);
      newRoot->_right = _CopyTree(root->_right);
    }

    return newRoot;
  }

  void _DestroyTree(Node *root)
  {
    if (root)
    {
      _Destroy(root->_left);
      _Destroy(root->_right);
      delete root;
    }
  }

  void _PrevOrder(Node *root)
  {
    if (root == NULL) //必須有遞歸出口?。?!
      return;

    cout << root->_data << " ";
    _PrevOrder(root->_left);
    _PrevOrder(root->_right);
  }

  void _InOrder(Node *root)
  {
    if (root == NULL)
      return;

    _InOrder(root->_left);
    cout << root->_data << " ";
    _InOrder(root->_right);
  }

  void _PostOrder(Node *root)
  {
    if (root == NULL)
      return;

    _PostOrder(root->_left);
    _PostOrder(root->_right);
    cout << root->_data << " ";
  }

  size_t _Size(Node *root)
  {
    if (root == NULL)
      return 0;
    else
      return _Size(root->_left) + _Size(root->_right) + 1;
  }

  size_t _LeafSize(Node *root)
  {
    if (root == NULL)
      return 0;
    else if (root->_left == NULL && root->_right == NULL)
      return 1;
    else
      return _LeafSize(root->_left) + _LeafSize(root->_right);
  }

  size_t _GetKLevel(Node *root, int k)
  {
    if (root == NULL)
      return 0;
    else if (k == 1)
      return 1;
    else
      return _GetKLevel(root->_left, k - 1) + _GetKLevel(root->_right, k - 1);
  }

  size_t _Depth(Node *root)
  {
    if (root == NULL)
      return 0;
    else if (root->_left == NULL && root->_right == NULL)
      return 1;
    else
    {
      size_t leftDepth = _Depth(root->_left) + 1;
      size_t rightDepth = _Depth(root->_right) + 1;
      return leftDepth > rightDepth ? leftDepth : rightDepth;
    }
  }

  Node* _Find(Node *root, const T& d)
  {
    if (root == NULL)
      return NULL;
    else if (root->_data == d)
      return root;
    else if (Node *ret = _Find(root->_left, d))
      return ret;
    else
      _Find(root->_right, d);
  }

protected:
  Node *_root;
};

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C++模擬實(shí)現(xiàn)stack和Queue的操作示例

    C++模擬實(shí)現(xiàn)stack和Queue的操作示例

    這篇文章主要介紹了C++模擬實(shí)現(xiàn)stack和Queue的操作示例,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-06-06
  • opencv車道線檢測(cè)的實(shí)現(xiàn)方法

    opencv車道線檢測(cè)的實(shí)現(xiàn)方法

    這篇文章主要介紹了opencv車道線檢測(cè)的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • 實(shí)現(xiàn)C語(yǔ)言常用字符串庫(kù)函數(shù)

    實(shí)現(xiàn)C語(yǔ)言常用字符串庫(kù)函數(shù)

    這篇文章主要為大家介紹了如何實(shí)現(xiàn)C語(yǔ)言常用字符串庫(kù)函數(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-11-11
  • C++控制臺(tái)實(shí)現(xiàn)貪吃蛇游戲

    C++控制臺(tái)實(shí)現(xiàn)貪吃蛇游戲

    這篇文章主要為大家詳細(xì)介紹了C++控制臺(tái)實(shí)現(xiàn)貪吃蛇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • 深入解析C++中的構(gòu)造函數(shù)和析構(gòu)函數(shù)

    深入解析C++中的構(gòu)造函數(shù)和析構(gòu)函數(shù)

    析構(gòu)函數(shù):在撤銷對(duì)象占用的內(nèi)存之前,進(jìn)行一些操作的函數(shù)。析構(gòu)函數(shù)不能被重載,只能有一個(gè)
    2013-09-09
  • 基于C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的掃雷小游戲

    基于C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的掃雷小游戲

    這篇文章主要為大家詳細(xì)介紹了基于C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • c++使用正則表達(dá)式提取關(guān)鍵字的方法

    c++使用正則表達(dá)式提取關(guān)鍵字的方法

    這篇文章給大家介紹了c++使用正則表達(dá)式提取關(guān)鍵字的方法,相對(duì)來(lái)說(shuō)比較簡(jiǎn)單,同時(shí)給大家提到了c++通過(guò)正則表達(dá)式提取匹配到的字符串的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-08-08
  • C++ 中函數(shù)重載、覆蓋與隱藏詳解

    C++ 中函數(shù)重載、覆蓋與隱藏詳解

    這篇文章主要介紹了C++ 中函數(shù)重載、覆蓋與隱藏詳解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • C++對(duì)cin輸入字符的判斷及分段函數(shù)處理方法示例

    C++對(duì)cin輸入字符的判斷及分段函數(shù)處理方法示例

    這篇文章主要介紹了C++對(duì)cin輸入字符的判斷及分段函數(shù)處理方法,結(jié)合實(shí)例形式分析了C++輸入判斷及處理相關(guān)操作技巧,需要的朋友可以參考下
    2017-09-09
  • C/C++實(shí)現(xiàn)投骰子游戲

    C/C++實(shí)現(xiàn)投骰子游戲

    這篇文章主要為大家詳細(xì)介紹了C/C++實(shí)現(xiàn)投骰子游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11

最新評(píng)論

揭西县| 江阴市| 内丘县| 晋州市| 广东省| 南涧| 扬中市| 库车县| 罗山县| 南昌县| 黔江区| 湟中县| 梓潼县| 阿勒泰市| 沙坪坝区| 济宁市| 龙陵县| 榆中县| 宜章县| 兴和县| 大田县| 米林县| 西乡县| 大悟县| 宣汉县| 那曲县| 拉孜县| 萨迦县| 龙州县| 南木林县| 德昌县| 江津市| 麻江县| 全州县| 玉林市| 定边县| 广东省| 巢湖市| 德安县| 修水县| 五原县|