数据结构栈、队列算法

[cpp] view plain copy

#include <malloc.h>  

#include <stdio.h>  

#include <iostream>  

#include <stack>  

#include <algorithm>  

//两个栈实现队列  

using namespace std;  

template<class T>   

class  queue{  

    stack<T>s1, s2;  

public: queue(){}  

    int size(){return s1.size()+s2.size()}  

    bool empty(){};  

    void push(T value){ s1.push(value); }  

    T front(){  

    //先判空  

    if (size()==0)  

    {  

        return NULL;  

    }  

    if (!s2.empty())  

    {  

        return s2.top();  

    }  

    else  

    {  

        while (!s1.empty())  

        {  

            s2.push(s1.top());  

            s1.pop();  

        }  

        return s2.top();  

    }  

    }  

    void pop()  

    {  

        //先判空  

        if (size() == 0)  

        {  

            return ;  

        }  

        if (s2.empty())  

        {  

            while (!s1.empty())  

            {  

                s2.push(s1.top());  

                s1.pop();  

            }  

        }  

        s2.pop();     

    }  

};  

struct binary  

{  

    int value;  

    binary* l;  

    binary* r;  

};  

  

  

//递归法先中后遍历二叉树  

void preRec(binary* T)  

{  

    if (T!=NULL)  

    {  

        std::cout << T->value;  

        preRec(T->l);  

        preRec(T->r);  

    }  

}  

  

//非递归前序法  

void preNoRec(binary* T)  

{  

    stack<binary* > s;  

    binary* p = T;  

    while (p||!s.empty())  

    {  

        if (p!=NULL)  

        {  

            cout << p->value;  

            s.push(p);  

            p = p->l;  

        }  

        else  

        {  

            p = s.top();  

            s.pop();  

            p = p->r;  

        }  

    }  

}  

  

//非递归中序  

void inNoRec(binary* T)  

{  

    stack<binary* > s;  

    binary* p = T;  

    while (p || !s.empty())  

    {  

        if (p != NULL)//一直走到最左端,不打印  

        {  

            s.push(p);  

            p = p->l;  

        }  

        else  

        {  

            p = s.top();  

            cout << p->value;  

            s.pop();  

            p = p->r;  

        }  

    }  

}  

  

//非递归后序  

void postNoRec(binary* T)  

{  

    binary* p = T;  

    binary* r = NULL;  

    stack<binary* > s;  

    while (p||!s.empty())  

    {  

        if (p!=NULL)  

        {  

            s.push(p);  

            p = p->l;  

        }  

        else  

        {  

            p = s.top();  

            if (p->r&&p->r!=r)  

            {  

                p = p->r;  

                s.push(p);  

                p = p->l;  

            }  

            else  

            {  

                p = s.top();  

                s.pop();  

                cout << p->value;  

                r = p;  

                p = NULL;  

            }  

        }  

    }  

}  

  

//层次遍历  

void levelOrder(binary* T)  

{  

    queue<binary*> s;  

    binary* p=T;  

    s.push(p);  

    while (!s.empty())  

    {  

        p = s.front;  

        cout << p->value;  

        s.pop();  

        if (p->l)  

        {  

            s.push(p->l);  

        }  

        if (p->r)  

        {  

            s.push(p->r);  

        }  

    }  

}  

  

//二叉树是否相等  

bool isSame(binary* T1, binary* T2)  

{  

    if (T1==NULL&&T2==NULL)  

    {  

        return true;  

    }  

    if ((T1==NULL&&T2!=NULL) ||(T1!=NULL&&T2==NULL))  

    {  

        return false;  

    }  

    if (T1->value==T2->value)  

    {  

        return(isSame(T1->l, T2->l) && (T1->r, T2->r)) ||  

              (isSame(T1->l, T2->r) && (T1->r, T2->l));  

    }  

    else return false;  

}  

  

//二叉树深度  

int binaryDepth(binary* T)  

{  

    if (T==NULL)  

    {  

        return 0;  

    }  

    else  

    {  

        int l = binaryDepth(T->l) + 1;  

        int r = binaryDepth(T->r) + 1;  

        return l > r ? l : r;  

    }  

}  

  

//求二叉树相距最远的两个节点之间的距离  

  

struct r   

{  

    int m;  

    int n;  

};  

  

r fun(binary* T)  

{  

    if (T!=NULL)  

    {  

        r empty = {0,-1};  

        return empty;  

    }  

    r lhs = fun(T->l);  

    r rhs = fun(T->r);  

    r r1;  

    r1.n = max(lhs.n, rhs.n + 1);  

    r1.m = max(max(lhs.m,rhs.m),lhs.n+rhs.n+2);  

    return r1;  

}  

  

//二叉搜索树的判断  

//1.递归中序遍历,然后判断是否递增数列  

//2.如下,非常简洁的方法  

bool isBST(binary* T)  

{  

    int min = -(1 << 31);  

    int max = 1 << 31 - 1;  

    BST(T, min, max);  

}  

bool BST(binary* T,int Min,int Max)  

{  

    if (T == NULL)  

    {  

        return true;  

    }  

    if (T->value>Min&&T->value<Max)  

    {  

        return BST(T->l, Min, T->value) && BST(T->r, T->value, Max);  

    }  

    else   

        return false;  

}  

  

//平衡二叉树判断  

bool isBalanced1(binary* T)  

{  

    if (T==NULL)  

    {  

        return true;  

    }  

    int left = binaryDepth(T->l);  

    int right = binaryDepth(T->r);  

    int diff = left - right;  

    if (diff>1||diff<-1)  

    {  

        return false;  

    }  

    return isBalanced1(T->l) && isBalanced1(T->r);  

}  

  

bool isBalanced2(binary* T,int* depth)  

{  

    if (T==NULL)  

    {  

        *depth = 0;  

        return true;  

    }  

    int left, int right;  

    if (isBalanced2(T->l, &left) && isBalanced2(T->r, &right))  

    {  

        int diff = left - right;  

        if (diff <= 1 && diff >= -1)  

        {  

            *depth = 1 + (left > right ? left : right);  

            return true;  

        }  

        else  

            return false;  

    }  

    else  

        return false;  

}  

  

int MaxDepth(binary* T)  

{  

    if (T==NULL)  

    {  

        return 0;  

    }  

    return max(MaxDepth(T->l), MaxDepth(T->r)) + 1;  

}  

  

int MinDepth(binary* T)  

{  

    if (T == NULL)  

    {  

        return 0;  

    }  

    return min(MinDepth(T->l), MinDepth(T->r)) + 1;  

}  

bool isBalanced3(binary* T)  

{  

    if (MaxDepth(T) - MinDepth(T)<=1)  

    {  

        return true;  

    }  

    else   

        return false;  

}  

  

//大根堆  

//1.大根堆的初始化,l表示数组从a[0]-a[l];  

void buildMaxHeap(int a[], int l)  

{  

    void AdjustDown(int a[], int i, int l);  

    for (int i = l / 2; i > 0;i--)  

    {  

        AdjustDown(a, i, l);  

    }  

}  

  

void AdjustDown(int a[], int k, int l)  

{  

    //k为父节点,ii+1为子节点  

    a[0] = a[k];  

    for (int i = 2 * k; i <= l;i*=2)  

    {  

        //取子节点中较大者  

        if ((i < l) && a[i] < a[i + 1])  

            i++;  

        if (a[0]>=a[i])  

        {  

            break;  

        }  

        else  

        {  

            //子节点上移  

            a[k] = a[i];  

            k = i;  

        }  

    }  

    //被赛选节点值放入最终位置  

    a[k] = a[0];  

}  

  

//k是需要调整节点的下标位置  

void AdjustUp(int a[], int k)  

{  

    a[0] = a[k];  

    for (int i = k / 2; i > 0;i--)  

    {  

        if (a[0]<a[i])  

        {  

            break;  

        }  

        else  

        {  

            a[k] = a[i];  

            k = i;  

        }  

    }  

    a[k] = a[0];  

}  

  

//插入  

void maxHeapInsert(int a[], int t, int l )  

{  

    a[l+1] = t;  

    AdjustUp(a,l+1);  

}  

  

  

//删除最大元素  

void maxHeapDelete(int a[], int l)  

{  

    //破坏堆结构,在进行向下调整操作  

    a[1] = a[l];  

    a[l] = 0;//即为删除  

    AdjustDown(a, 1,l-1);  

}  


猜你喜欢

转载自blog.csdn.net/jingeche/article/details/77891670