#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *left;
struct node *right;
} Node;
typedef struct tree
{
Node *root;
}Tree;
void insert(Tree *tree, int value){
Node *node = malloc(sizeof(Node));
node ->data = value;
node ->left = NULL;
node ->right = NULL;
if (tree -> root == NULL){
tree -> root = node;
}else{
Node *temp = tree ->root;
while (temp != NULL)
{
if (value < temp->data){
if(temp->left == NULL){
temp->left = node;
return;
}else{
temp = temp -> left;
}
}else{
if(temp->right == NULL){
temp->right = node;
return;
}else{
temp = temp -> right;
}
}
}
}
}
void myprint(Node *node){
if (node != NULL){
myprint(node->left);
printf("%d\n", node->data);
myprint(node->right);
}
}
int get_height(Node *node){
if (node == NULL){
return 0;
}else{
int left = get_height(node->left);
int right = get_height(node->right);
int max = left;
if (right >max){
max = right;
}
return max + 1;
}
}
int main(){
int arr[7] = {6,5,4,7,8,9,0};
Tree tree;
tree.root =NULL;
int i;
for (size_t i = 0; i < 7; i++)
{
insert(&tree, arr[i]);
}
myprint(tree.root);
return 0;
}
普通二叉树
最新推荐文章于 2022-12-19 16:31:01 发布