码迷,mamicode.com
首页 > 编程语言 > 详细

C++二叉树笔试题

时间:2015-07-17 16:07:17      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:二叉树

#include <iostream>
#include <stack>
#include <queue>
using namespace std;

template<typename Type>
struct Node
{
    Node* right;
    Node* left;
    Type data;
    Node(Type tp = Type()) :data(tp),right(NULL),left(NULL){}
};

template<typename Type>
class MT
{
public:
    MT(const char *s,Type tp)
    {
        root = NULL;
        flags = tp;
        Insert(root,s);
    }

    void Insert(Node<Type> *&t,const char *& s )
    {
        if (*s == flags)
        {
            t = NULL;
            return ;
        }
        else
        {
            t = new Node<Type>(*s);
            Insert(t->left,++s);
            Insert(t->right, ++s);
        }
    }

    void A()
    {
        Node<Type> *t = root;
        if (t == NULL)return;
        queue<Node<Type> *> st;
        st.push(t);
        while (st.empty() == false)
        {
            Node<Type> *t = st.front();
            st.pop();
            cout << t->data << "  ";
            if (t->left != NULL)
            {
                st.push(t->left);
            }
            if (t->right != NULL)
            {
                st.push(t->right);
            }
        }
    }

    int B()
    {
        return B(root);
    }
    int C(int x)
    {
        return C(root,x);
    }
    void Printf()
    {
        Printf(root);
    }
    void D()
    {

    }
    bool IsHere(char ch)
    {
        return IsHere(root,ch);
    }

    char Parent(char ch1,char ch2)
    {
        return Parent(root,ch1,ch2);
    }
    bool IsBanlance()
    {
        return IsBanlance(root);
    }
    int  GetLengthMax()
    {
        return GetLengthMax(root);
    }
    int GetLg()
    {
        return GetLg(root);
    }
private:
    //二叉树高度。
    int GetLg(Node<Type> *t)
    {
        if (t == NULL)return 0;
        else
        {
            return GetLg(t->left) > GetLg(t->right) ? GetLg(t->left) + 1 : GetLg(t->right) + 1;
        }
    }

    //判断二叉树是不是平衡二叉树。
    bool IsBanlance(Node<Type> *t)
    {
        if (t == NULL)return true;
        int len = GetLengthMax(root);
        if (len >= 0 && len <= 1)return true;
        else return false;
    }

    //求二叉树中最高高度差。
    int GetLengthMax(Node<Type>* t)
    {
        int count = 0;
        int mincount = 0x7fffffff;
        int maxcount = 0xffffffff;
        Getlow(root, count, mincount);
        count = 0;

        GetHigh(root, count, maxcount);

        return maxcount - mincount;
        return 0;
    }

    int Getlow(Node<Type> *t,int count,int &mincount)
    {
        if (t == NULL)return 0;
        if (root->left == NULL || root->right == NULL)
        {
            mincount = 0;
            return 0;
        }
        if (t == root)
        {
            count += 1;
        }
        if ((t->left == NULL && t->right == NULL))
        {
            mincount = count > mincount ? mincount : count;
            return 0;
        }
        else
        {
            count++;
            Getlow(t->left, count, mincount);
            Getlow(t->right, count, mincount);
        }   
        return 0;
    }
    int GetHigh(Node<Type> *t,int count,int &maxcount)
    {
        if (t == NULL)return 0;                                                                                                  
        else
        {
            count++;
            GetHigh(t->left,count,maxcount);
            GetHigh(t->right, count, maxcount);
            maxcount = count > maxcount ? count : maxcount;
        }
        return 0;   
    }

    //求最低父亲节点(方法一)
    /*
    char Parent(Node<Type> *t, char ch1, char ch2)
    {
        if (t == NULL)return ‘0‘;
        else
        {
            Parent(t->left, ch1, ch2);
            Parent(t->right, ch1, ch2);
            //后续遍历找最近公共父亲节点。
            if (IsHere(t, ch1) == 1 && IsHere(t, ch2) == 1)return t->data;
        }
    }
    */

    //求最低父亲节点(方法二)
    char Parent(Node<Type> *t,char ch1,char ch2)
    {

        if (t != NULL)
        {
            stack<Node<Type> *> st;
            st.push(t);
            while (st.empty() == false)
            {
                if (IsHere(t->left, ch1) == 1 && IsHere(t->left,ch2) == 1)
                {
                    st.push(t->left);
                    t = t->left;
                    continue;
                }
                if (IsHere(t->right, ch1) == 1 && IsHere(t->right, ch2) == 1)
                {
                    st.push(t->right);
                    t = t->right;
                    continue;
                }
                break;
            }
            while (st.empty() == false)
            {
                Node<Type> *p = st.top();
                st.pop();
                cout << p->data << endl;
            }
        }
        return ‘a‘;
    }


    //查看树中是否有该字符。
    bool IsHere(Node<Type> *t,char ch)
    {
        if (t == NULL)return false;
        if (t->data == ch)return true;
        else
        {
            if (IsHere(t->left, ch))return true;
            return IsHere(t->right,ch);
        }
    }

    //查看指定层的节点个数。
    int C(Node<Type> *t,int x)
    {
        if (x<1 || t == NULL)return 0;
        if (x == 1)
        {
            return 1;
        }
        else
        {
            x--;
            return C(t->left, x) + C(t->right, x);
        }
    }

    //查看叶子节点的个数。
    int B(Node<Type>* t)
    {
        if (t == NULL)return 0;
        if (t->right == NULL  && t->left == NULL)
        {
            return 1;
        }
        else
        {
        return  B(t->left)+B(t->right);
        }
    }
        //二叉树的前序遍历。
    void Printf(Node<Type> *t)
    {
        if (t == NULL)
        {
            return;
        }
        else
        {
            cout << t->data << "   ";
            Printf(t->left);
            Printf(t->right);
        }
    }

private:
    Type flags;
    Node<Type> *root;
};

int main()
{
    //char s[] = "abcd####e##";
    //char s[] = "ab##c##";
    //char s[] = "a#b##";
    char s[] = "ab##c#d#e#f##";
    //char s[] = "abcd####e#f#g#h##";

    MT<char> mt(s,‘#‘);

    //mt.A();
    //cout << mt.C(4) << endl;
    //cout <<mt.IsHere(‘e‘) << endl;
    //cout <<mt.Parent(‘c‘,‘d‘)<<endl;
    //mt.D();
    cout << mt.GetLg() << endl;
    //cout << mt.GetLengthMax() << endl;
    //cout<<mt.IsBanlance()<<endl;
    //mt.Printf();
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

C++二叉树笔试题

标签:二叉树

原文地址:http://blog.csdn.net/liuhuiyan_2014/article/details/46928029

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!