数据结构上机3.5.4

3.5.4

二叉树的高度

#include <cstdio>
#include <iostream>
#define max(x, y) (x) > (y) ? (x) : (y)
using namespace std;

template<typename T>
struct node{
    node<T> * l;
    node<T> * r;
    T data;
    node(){
        l = nullptr, r = nullptr;
    }
    node(const T d){
        data = d;
        l = nullptr, r = nullptr;
    }
    node & operator= (const node & n){
        data = n.data;
        l = n.l, r = n.r;
    }
};

template<typename T>
class bt{
    private:
        node<T> * root;
        int size;
        void insert(node<T> * n, const T & data){
            if (data == n->data) return;
            if (data < n->data){
                if (n->l != nullptr){
                    insert(n->l, data);
                }
                else{
                    n->l = new node<T>(data);
                    return;
                }
            }
            else{
                if (n->r != nullptr){
                    insert(n->r, data);
                }
                else{
                    n->r = new node<T>(data);
                    return;
                }
            }
        }
        void print(ostream & os, node<T> * n, int depth){
            for (int i = 0; i < depth << 2; ++i) os << ' ';
            os << n->data << endl;
            if (n->l != nullptr) print(os, n->l, depth + 1);
            if (n->r != nullptr) print(os, n->r, depth + 1);
        }
        int count(node<T> * n){
            if (n->l == nullptr && n->r == nullptr){
                return 1;
            }
            else{
                int ans = 0;
                if (n->l) ans += count(n->l);
                if (n->r) ans += count(n->r);
                return ans;
            }
        }
        int height(node<T> * n, int depth){
            if (n->l == nullptr && n->r == nullptr){
                return depth;
            }
            int ans = 0;
            if (n->l) ans = max(height(n->l, depth + 1), ans);
            if (n->r) ans = max(height(n->r, depth + 1), ans);
            return ans;
        }
    public:
        bt(){
            root = nullptr;
            size = 0;
        }
        friend ostream & operator<< (ostream & os, bt<T> & t){
            if (!t.size) return os;
            t.print(os, t.root, 0);
            return os;
        }
        void insert(T data){
            ++size;
            if (root == nullptr) root = new node<T>(data);
            else insert(root, data);
        }
        int count(){
            return count(root);
        }
        int height(){
            return height(root, 0) + 1;
        }
};

typedef unsigned long long ull;
ull k1, k2, mod;

ull xorshift128add(){
    ull k3 = k1, k4 = k2;
    k1 = k4;
    k3 = k3 << 17 ^ k3;
    k2 = k3 >> 23 + k4 << 10;
    return k2 + k4;
}

int main(){
    bt<int> t;
    ios::sync_with_stdio(false);
    int T;
    cin >> T;
    cin >> k1 >> k2 >> mod;
    cout << t << endl;
    for (int i = 0; i < T; ++i){
        int temp = (int)(xorshift128add() % mod);
        cout << "inserting " << temp << endl;
        t.insert(temp);
    }
    cout << t << endl;
    cout << "HEIGHT: " << t.height() << endl;
    return 0;
}

/*
20
123456789 987654321 23

*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值