• C++二叉树笔试题


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

  • 相关阅读:
    神经网络之 --- 2012_ Alexnet
    Array.obj : error LNK2001: unresolved external symbol "void __cdecl Test_ultiply(void)" (?Test_ultiply@@YAXXZ)
    学习opencv出现配置错误(一)
    port和interface的区别
    Vivado当中的ooc与global模式
    ADC采样率,符号率
    MATLAB&Simulink的重复方式
    傅里叶变换的对称性质
    AXI总结一
    晶振相关(一)
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/7096010.html
Copyright © 2020-2023  润新知