面试经典二叉树算法

来源:互联网 发布:sql日期截取年月日 编辑:程序博客网 时间:2024/05/22 08:20
#include <iostream>#include <cstdlib>#include <cassert>#include <stack>#include <cmath>#include <queue>#include <list>using namespace std;//面试经典二叉树算法typedef char ElemType;#define END '#'template <typename T>const T & Max(const T& a,const T & b){    return a>b?a:b;}typedef struct BtNode{    //BtNode *parent;    BtNode *leftchild;    BtNode *rigthchild;    ElemType data;}BtNode , *BinaryTree;BtNode * _Buynode(){    BtNode *s = (BtNode *)malloc(sizeof(BtNode));    if(NULL == s) exit(1);    memset(s,0,sizeof(BtNode));    return s;}void _Freenode(BtNode *p){    free(p);}//利用出栈的顺序进行遍历struct StkNode{    BtNode * Btnode;    int PopNum;public:    StkNode(BtNode * ptr = NULL)        :Btnode(ptr),PopNum(0)    {}};//中序void IsTree_struct_pop_num(BtNode *ptr){    if(ptr == NULL)    {        return ;    }    stack<StkNode> st;    st.push(StkNode(ptr));    while (!st.empty())    {        StkNode p = st.top();        st.pop();        p.PopNum++;        if( p.PopNum == 2 )        {            cout<<p.Btnode->data<< " ";            if( p.Btnode->rigthchild != NULL )            {                st.push(StkNode(p.Btnode->rigthchild));            }        } else {            st.push(StkNode(p));            if(p.Btnode->leftchild != NULL) {                st.push(StkNode(p.Btnode->leftchild));            }        }    }}//后序void PassTree_struct_pop_num(BtNode *ptr){    if(ptr == NULL)    {        return ;    }    stack<StkNode> st;    st.push(StkNode(ptr));    while (!st.empty())    {        StkNode pt = st.top();        st.pop();        pt.PopNum++;        if(pt.PopNum == 3) //第三次出栈,打印        {            cout<<pt.Btnode->data<<" ";        }        else        {            st.push(ptr);            if(pt.PopNum == 1 && pt.Btnode->leftchild != NULL)            {                st.push(StkNode(pt.Btnode->leftchild));            }            else if(pt.PopNum == 2 && pt.Btnode->rigthchild != NULL)            {                st.push(StkNode(pt.Btnode->rigthchild));            }        }    }}//创建二叉树BtNode* Create_Tree(const char * &str){    BtNode * s = NULL;    if(str == NULL)    {        return NULL;    }    if(*str != END)    {        s = _Buynode();        s->data = *str;        s->leftchild = Create_Tree(++str);        s->rigthchild = Create_Tree(++str);    }    return s;}//前序递归void PreTree(BtNode *ptr){    if(ptr == NULL)    {        return ;    }    else    {        cout<<ptr->data<<" ";        PreTree(ptr->leftchild);        PreTree(ptr->rigthchild);    }}//前序非递归void PreOrder2(BtNode *ptr){    if(ptr == NULL)    {        return;    }    stack<BtNode *>st;    st.push(ptr);    while (!st.empty())    {        ptr = st.top();        st.pop();        cout <<ptr->data<<" ";        if(ptr->rigthchild != NULL) //right        {            st.push(ptr->rigthchild);        }        if(ptr->leftchild  != NULL)//left        {            st.push(ptr->leftchild);        }           }}//前序非递归void PreOrder1(BtNode *ptr){    if(ptr == NULL)    {        return;    }    stack<BtNode*> st;    while (ptr != NULL || !st.empty())    {        while (ptr != NULL)        {            cout<<ptr->data<<" ";            st.push(ptr);            ptr = ptr->leftchild;        }        ptr = st.top();        st.pop();        ptr = ptr->rigthchild;    }}//中序非递归void IsOrder(BtNode * ptr){    if(ptr == NULL)    {        return;    }    stack<BtNode *> st;    while(ptr != NULL || !st.empty())    {        while(ptr != NULL)        {            st.push(ptr);            ptr = ptr->leftchild;        }        ptr = st.top();        st.pop();        cout<<ptr->data<<" ";        ptr = ptr->rigthchild;    }}//后续非递归void PassOrder(BtNode *ptr){    if(ptr == NULL)    {        return;    }    stack<BtNode*> st;    BtNode *flag = NULL;    while (ptr != NULL || !st.empty())    {        while (ptr != NULL)        {            st.push(ptr);            ptr = ptr->leftchild;        }        ptr = st.top();        st.pop();        if(ptr->rigthchild == NULL || flag == ptr->rigthchild)        {            cout<<ptr->data<<" ";            flag = ptr;            ptr  = NULL; //如果已经访问了的数据节点,则此节点将会失去生命周期,没有意义        }        else        {            st.push(ptr); //他的右子树还没有访问            ptr = ptr->rigthchild;        }    }    cout<<endl;}//树高int Hight_Tree(BtNode *ptr){    if(ptr == NULL)    {        return 0;    }    if(ptr->leftchild == NULL && ptr->rigthchild == NULL)    {        return 1;    }    int lefthight  = Hight_Tree(ptr->leftchild);    int righthight = Hight_Tree(ptr->rigthchild);    return Max(lefthight,righthight)+1;}//节点个数int Size_Tree(BtNode *ptr){    if(ptr == NULL)    {        return 0;    }    return Size_Tree(ptr->leftchild)+Size_Tree(ptr->rigthchild)+1;}//叶节点个数int leave_Tree(BtNode *ptr){    if(ptr == NULL)    {        return 0;    }    if(ptr->leftchild == NULL && ptr->rigthchild == NULL)    {        return 1;    }    return leave_Tree(ptr->rigthchild)+leave_Tree(ptr->leftchild)+1;}//完全二叉树bool Is_Comp_Binary_Tree(BtNode *ptr){    if(ptr == NULL)    {        return true;    }    queue<BtNode *> qu;    qu.push(ptr);    bool result = true;    bool flag = false;    while (!qu.empty())    {        ptr = qu.front();        qu.pop();        if(flag)        {            if(ptr->leftchild != NULL || ptr->rigthchild !=NULL)            {                result = false;                break;            }        }        if(ptr->leftchild != NULL && ptr->rigthchild == NULL)        {            qu.push(ptr->leftchild);            flag = true;        }        if(ptr->leftchild == NULL && ptr->rigthchild == NULL)        {            flag = true;        }        if(ptr->leftchild == NULL && ptr->rigthchild != NULL)        {            result = false;            break;        }        if(ptr->leftchild != NULL && ptr->rigthchild !=NULL)        {            qu.push(ptr->leftchild);            qu.push(ptr->rigthchild);        }    }    return result;}//满二叉树bool Is_Full_Binary_Tree(BtNode *ptr){    if(ptr == NULL)    {        return true;    }    int higth = Hight_Tree(ptr);    int sizecount  = Size_Tree(ptr);    int leave = leave_Tree(ptr);    if((sizecount ==  pow(2,higth)-1) && leave == pow(2,higth-1))    {        return true;    }    else    {        return false;    }   }//树节点int GetNumTree(BtNode *ptr){    if(ptr == NULL)    {        return 0;    }    return GetNumTree(ptr->leftchild)+GetNumTree(ptr->rigthchild)+1;}//打印倒数第k个void PrintCompBTree(BtNode *ptr, int k)//倒数第k个节点{    assert(ptr != NULL);    int len = GetNumTree(ptr);    if(ptr == NULL ||k < 0 || k > len)    {        return;    }    int k1 = len-k;    int i = 0;    queue<BtNode*> queue;    queue.push(ptr);    while (!queue.empty())    {        ptr = queue.front();        queue.pop();        i++;        if( i == k1)        {            cout<<"倒数第"<<k<<"个节点:"<<ptr->data<<endl;            break;        }        if(ptr->leftchild != NULL)        {            queue.push(ptr->leftchild);        }        if(ptr->rigthchild != NULL)        {            queue.push(ptr->rigthchild);        }    }}int GetBinaryTreeLevel(BtNode *ptr, int k){    if(ptr == NULL || k < 0)    {        return 0;    }    if(k == 1)    {        return 1;    }    int leftcount  = GetBinaryTreeLevel(ptr->leftchild,k-1);    int rightcount = GetBinaryTreeLevel(ptr->rigthchild,k-1);    return  leftcount+rightcount;}//平衡二叉树bool IsBalance_BinaryTree(BtNode *ptr,int &hight){    if(ptr == NULL)    {        hight = 0;        return false;    }    if(ptr->leftchild == NULL && ptr->rigthchild != NULL)    {        return false;    }    if(ptr->rigthchild != NULL && ptr->leftchild == NULL)    {        return false;    }    int lefthight  = 0;    int righthight = 0;    bool boolleft  = IsBalance_BinaryTree(ptr->leftchild,lefthight);    bool boolright = IsBalance_BinaryTree(ptr->rigthchild,righthight);    return boolright && boolleft && (abs(lefthight - righthight) <=1);}//查找节点bool FindNode_Tree(BtNode *ptr,BtNode *child){    if(ptr == NULL || child == NULL)    {        return false;    }    if(ptr == child)    {        return true;    }    bool flag = FindNode_Tree(ptr->leftchild ,child);    if(!flag)    {        flag = FindNode_Tree(ptr->rigthchild,child);    }    return flag;}//寻找相同祖父BtNode * FindComParent(BtNode *ptr, BtNode *child1,BtNode *child2){    if(ptr == NULL || child1 == NULL || child2 == NULL)    {        return NULL;    }    if(FindNode_Tree(ptr->leftchild,child1))   // 左右  ----     {        if(FindNode_Tree(ptr->rigthchild,child2))        {            return ptr;        }        else        {            return FindComParent(ptr->leftchild,child1,child2);        }    }    if(FindNode_Tree(ptr->rigthchild,child1))//not else //may be no this child in ptr    {        if(FindNode_Tree(ptr->leftchild,child2)) //右左        {            return ptr;        }        else        {            return FindComParent(ptr->rigthchild,child1,child2);        }    }    return NULL;}//2bool Get_Child_Tree_Path(BtNode *ptr,BtNode *child,list<BtNode*>& path) {    if(ptr == NULL || child == NULL )    {        return false;    }    if(ptr == child)    {        path.push_back(ptr);        return true;    }    path.push_back(ptr);    bool found = false;    found = Get_Child_Tree_Path(ptr->leftchild,child,path);    if(!found)    {        found = Get_Child_Tree_Path(ptr->rigthchild,child,path);    }    if(!found)    {        path.pop_back();    }    return found;}BtNode * FindComParent2(BtNode *ptr, BtNode *child1,BtNode *child2){    if(NULL == ptr || NULL == child1 || NULL == child2)    {        return NULL;    }    list<BtNode *>path1;    list<BtNode *>path2;    bool boolchild1 = Get_Child_Tree_Path(ptr,child1,path1);    bool boolchild2 = Get_Child_Tree_Path(ptr,child2,path2);    BtNode * last = NULL;    if(boolchild1 && boolchild2)    {        list<BtNode *>::iterator it1 = path1.begin();        list<BtNode *>::iterator it2 = path2.begin();        while(it1 != path1.end() && it2 != path2.end())        {            if(*it1 ==  *it2)            {                last = *it1;            }            else            {                break;            }            ++it1;            ++it2;        }    }    else    {        last = NULL;    }    return last;}//转换成双链表BtNode * BinaryTreeToList(BtNode *ptr){    if(ptr == NULL )    {        return NULL;    }    BtNode *head = NULL;    stack<BtNode*> st;    while (ptr != NULL || !st.empty() )    {        while (ptr!=NULL)        {            st.push(ptr);            ptr = ptr->leftchild;        }        BtNode *p = st.top();        ptr = p;        st.pop();        if(NULL ==  head)        {            head = p;            head->rigthchild = NULL;            head->leftchild  = NULL;        }        else        {            BtNode *pre = head;            while (pre->rigthchild != NULL)            {                pre = pre->rigthchild;            }            pre->rigthchild = p;            p->leftchild    = pre;            p->rigthchild   = NULL;        }        ptr = ptr->rigthchild;          }    return head;}void BinaryTreeToList3(BtNode * &head,BtNode *&last){    if(head == NULL) return;    BinaryTreeToList3(head->leftchild,last);    head->leftchild = last;    if(last != NULL)    {        last->rigthchild = head;    }    last = head;    BinaryTreeToList3(head->rigthchild,last);}void BinaryTreeToList2(BtNode * ptr,BtNode *&first,BtNode *&last){    BtNode *pfirstleft;    BtNode *plastleft;    BtNode *pfirstright;    BtNode *plastright;    if(ptr == NULL)    {        first = NULL;        last  = NULL;        return ;    }    if(ptr->leftchild == NULL)    {        first = ptr;    }    else    {        BinaryTreeToList2(ptr->leftchild,pfirstleft,plastleft);        first = pfirstleft;        //把根节点放置到在左子树中查找到的最后一个节点        ptr->leftchild        = plastleft;        plastleft->rigthchild = ptr;    }    if(ptr->rigthchild == NULL)    {        last = ptr;    }    else    {        BinaryTreeToList2(ptr->rigthchild,pfirstright,plastright);        last                   = plastright;        ptr->rigthchild        = pfirstleft;        pfirstleft->rigthchild = ptr;    }}int main(){    //const char* p = "ABC##DE##F##G#H##";     //BtNode * root = Create_Tree(p);    //PreTree(root);    //cout<<endl;    //PreOrder2(root);    //cout<<endl;    //IsOrder(root);    //PassTree_struct_pop_num(root);    //int hight = 0;    //if(IsBalance_BinaryTree(root,hight))    //{    //cout<<"root is IsBalance_BinaryTree"<<endl;    //}    const char *str1 = "ABC##D##EF###";    const char *str2 = "ABCG###D##EF###";    BtNode *root = Create_Tree(str1);    IsOrder(root);    BtNode *p;    BtNode *q = NULL;    BinaryTreeToList3(root,q);    //BinaryTreeToList2(root,p,q);    for(p = root;p!=NULL;p = p->rigthchild)    {        cout<<p->data<<" ";    }    cout<<endl;    //cout<<p->data<<" "<<p->rigthchild->data<<" "<<p->rigthchild->rigthchild->data<<endl;    //for(;p!=q;p = p->rigthchild)    //{    //  cout<<p->data<<" ";    //}    //BtNode *root2 = Create_Tree(str2);    ////PreTree(root);    //    //cout<<endl;    //cout<<GetNumTree(root)<<endl;    //BtNode * p = BinaryTreeToList(root);      //cout<<endl;    //while (p!=NULL)    //{    //  cout<<p->data<<" ";    //  p = p->rigthchild;    //}    //PassOrder(root);    //if(Is_Comp_Binary_Tree(root))    //{    //  cout<<"Ok Is_Comp_Binary_Tree"<<endl;    //}    //cout<<GetBinaryTreeLevel(root2,4)<<endl;    /*PreTree(root2);    cout<<endl;    PassOrder(root2);    if(Is_Comp_Binary_Tree(root2))    {        cout<<"Ok Is_Comp_Binary_Tree"<<endl;    }*/    return 0;}//如果有什么错误,希望不吝赐教
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 房东不想续租了怎么办 唯一住房卖掉后户口怎么办 杭州唯一住房卖掉后户口怎么办 唯一一套住房卖掉户口怎么办 房租押金条丢了怎么办 二建证书跟毕业证丢失怎么办 住宅房到70年怎么办 护照号变了机票怎么办 苹果se指纹坏了怎么办 月经推迟怎么办才能快点来 车载支架不粘了怎么办 otpc平板电脑无法开机怎么办 手表表轴掉了怎么办 鸡肉放冰箱臭了怎么办 鸡胸肉熟了腥怎么办 梦幻西游手游手机号换了怎么办 ppt做一半卡住了怎么办 吃了发霉的蚝油怎么办 蛋皮干燥起皮怎么办 wps卡顿资料没保存怎么办 手指受伤肉掉了怎么办 手机被wifi禁了怎么办 母乳一边是咸的怎么办 tcl电视蓝频了怎么办 长虹电视蓝频了怎么办 电视突然蓝频了怎么办 海信电视蓝频了怎么办 连网电视蓝频了怎么办 英雄联盟画面卡顿怎么办 长残了怎么办原来很帅 被吓到了怎么办没精神 宝宝吓着怎么办最有效 4个月婴儿易惊吓怎么办 心里有问题的人怎么办 减肥的时候想吃东西怎么办 大联盟ping很高怎么办 酷派手机弹广告怎么办 孕早期肚子紧绷怎么办 怀孕2个月同房了怎么办 怀孕前三月同房了怎么办 人流前三天同房了怎么办