C++.编程题.平衡二叉树

#include <iostream>
#include <stdlib.h>
using namespace std;

typedef struct node{
        int data;
        struct node* left;
        struct node* right;
} Node;

typedef struct{
        Node* root;
} Tree;

void insert(Tree* tree, int value){
        Node* node = (Node*)malloc(sizeof(Node));
        node -> data  = value;
        node -> left  = NULL;
        node -> right = NULL;

        if(tree -> root == NULL){
                tree -> root = node;
        }
        else {
                Node* tmp = tree -> root;
                while (tmp != NULL) {
                        if(value < tmp -> data){
                                if(tmp -> left == NULL){
                                        tmp -> left = node;
                                        return;
                                }
                                else {
                                        tmp = tmp -> left;
                                }
                        }
                        else{
                                if(tmp -> right == NULL){
                                        tmp -> right = node;
                                        return;
                                }
                                else {
                                        tmp = tmp -> right;
                                }
                        }
                }
        }
}

int get_height(Node* node) {
        if(node == NULL) {
                return 0;
        }
        else {
                int left_h  = get_height(node -> left);
                int right_h = get_height(node -> right);
                int d = left_h;
                if(right_h > d);
                        d = right_h;
        }
        return d + 1;

}

int get_maximum(Node* node) {
        if (node == NULL) {
                return -1;
        }
        else{
                int m1 = get_maximum(node -> left);
                int m2 = get_maximum(node -> right);
                int m3 = node -> data;
                int max = m1;
                if(m2 > max) { max = m2; }
                if(m3 > max) { max = m3; }
                return max;
        }
}

        }
        if(node != NULL){
                cout << node -> data << " ";
                preorder(node -> left);
                preorder(node -> right);
        }
}

void inorder(Node* node) {
        if (node != NULL){
                inorder(node -> left);
                cout << node -> data << " ";
                inorder(node -> right);
 }
}

void postorder(Node* node) {
        if (node != NULL){
                postorder(node -> left);
                postorder(node -> right);
                cout << node -> data << " ";
        }
}

int main() {
        int arr[7] = {6, 3, 8, 2, 5, 1, 7};
        Tree tree;
        tree.root = NULL;

        int i;
        for(i=0; i<7; i++){
                insert(&tree, arr[i]);
        }
        //      inorder(tree.root);
         int h = get_height(tree.root);
        cout << h << " ";
}

                  

方法:先创建树的数据结构,根据二叉树右大左小原则将数组的数插入树中,根据指针递归可以查出树高,前中后遍历的区别就是把输出放在前中后中,用递归遍历

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值