C++二叉樹總結(包含源碼)一

#include<iostream>
#include<string>
#include<list>
#include<cmath>
#include<hash_map>
using namespace std;

class Node{
    public:
        Node(int data){
            this->value = data;
        }
        int value;
        Node* left;
        Node* right;
};

typedef list<Node*> Linklist;//創建一個名稱爲Linklist的單鏈表類
/*
總結單鏈表在C++中的操作
Linklist Linkint;
Linklist::iterator it;//定義一個迭代器
Linkint.seze();//返回大小
Linkint.push_back()||push_back(Linkint, num);//從後面插入
Linkint.push_front()||push_front(Linkint, num);//從前面插入數據
Linkint.pop_front();//彈出數據同上兩種方法
Linkint.pop_back();//彈出數據,同上兩種方法
Linkint.back();//返回鏈表最後一個數據
Linkint.begin();//返回鏈表首部的迭代器
clear(Linkint);//刪除鏈表的所有元素
Linklist.end();//返回鏈表的末尾的迭代器
Linklist.empty();//判斷鏈表是否爲空
erase();//刪除一個元素
其它參考https://blog.csdn.net/Cypress1010/article/details/53669403?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task
*/

//遞歸實現將所有的元素中序遍歷加入鏈表
void process(Node* node, Linklist* inOrderlist){
    if (node == NULL){
        return;
    }

    process(node->left, inOrderlist);
    inOrderlist->push_back(node);
    process(node->right, inOrderlist);
}

//判斷一顆二叉樹是否爲搜索二叉樹
bool isBST(Node* head){
    if(head == NULL){
        return true;
    }

    Linklist* inOrderList = new Linklist;//申請一個鏈表
    process(head, inOrderList);
    int pre = 0;
    Linklist::iterator it;//迭代器
    for (it = inOrderList->begin(); it != inOrderList->end(); it++)
    {
        if (pre >= (*it)->value)
        {
            return false;
        }
        pre = (*it)->value;   
    }

    return true;
}

//計算一顆二叉樹的最大寬度
int getMaxWidth(Node* head){
    if (head == NULL)
    {
        return 0;
    }

    int maxWidth = 0;//最大寬度
    int curWidth = 0;//當前層的寬度
    int curLevel = 0;//當前層數

    hash_map<Node*, int> levelMap;//聲明一個哈希表<節點, 當前層數>
    typedef pair<Node*, int> pair_Node;//聲明向hash_map中插入的數據格式
    hash_map<Node, int>::const_iterator levelMap_AcIter;//迭代器
    levelMap.insert(pair_Node(head, 1));
    Linklist* quene;

    quene->push_front(head);//頭節點放入鏈表中
    Node* node = NULL;
    Node* left = NULL;
    Node* right = NULL;

    while (!quene->empty())
    {
        node = quene->front();//獲取首部節點
        quene->pop_front();//彈出鏈表首元素
        left = node->left;
        right = node->right;

        if (left != NULL)
        {
            levelMap.insert(pair_Node(left, levelMap[node]+1));
            quene->push_front(left);
        }

        if (right == NULL)
        {
            levelMap.insert(pair_Node(right, levelMap[node]+1));
            quene->push_front(right);
        }

        if (levelMap[node] > curLevel)
        {
            curWidth = 0;
            curLevel = levelMap[node];
        }

        else
        {
            curWidth ++;
        }

        maxWidth = maxWidth > curWidth ? maxWidth : curWidth;
    }
    
    return maxWidth;
    
}

//完全二叉樹
bool isCBT(Node* head)
{
    if(head == NULL)
    {
        return true;
    }

    Linklist quene;//聲明一個節點位Node的鏈表
    bool leaf = false;
    Node* l = NULL;
    Node* r = NULL;
    quene.push_front(head);

    while (!quene.empty())
    {
        head = quene.front();
        quene.pop_front();
        l = head->left;
        r = head->right;

        if ((leaf && (l != NULL || r != NULL || (l == NULL && r == NULL))))
        {
            return false;
        }
        if (l != NULL)
        {
            quene.push_front(l);
        }
        if (r != NULL)
        {
            quene.push_front(r);
        }
        else
        {
            leaf = true;
        }
    }
    
    return true;
}

//判斷一顆二叉樹是不是平衡二叉樹
class ReturnType{
    public:
        bool isBalanced;
        int height;

        ReturnType(bool isB, int h)
        {
            isBalanced = isB;
            height = h;
        }
};

ReturnType* Process(Node* x)
{
    if (x == NULL)
    {
        return new ReturnType(true, 0);
    }
    ReturnType* leftData = Process(x->left);
    ReturnType* rightData = Process(x->right);//p判斷左右子樹是不是平衡二叉樹

    int height = leftData->height > rightData->height ? leftData->height : rightData->height;
    bool isBalanced = leftData->isBalanced && rightData->isBalanced && abs(leftData->height - rightData->height) < 2;

    return new ReturnType(isBalanced, height);
    
}

bool isBalanced(Node* head)
{
    return Process(head)->isBalanced;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章