C ++ binary tree summary (including source code) a

#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;
}

 

Published 43 original articles · won praise 44 · views 6606

Guess you like

Origin blog.csdn.net/qq_41582910/article/details/104742256