我試圖在編程c中實現二叉樹。它只取值但不顯示任何預期的結果。需要幫助來實現二叉樹。在這裏我添加了我的代碼,這個代碼正在嘗試很久。編程中的二叉樹實現代碼中的問題c
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*left;
struct node*right;
};
int i,parent;
struct node*root=NULL;
struct node*newnode,*temp[];
void display(struct node*);
void display(struct node *root)
{
if(root != NULL){
printf("%d\t",root->data);
display(root->left);
display(root->right);
}
}
void add(int num)
{
for(i=0;i<num;i++)
{
newnode=(struct node*)malloc(sizeof(struct node));
newnode->left=newnode->right=NULL;
newnode->data=i+1;
if(i==0)root=newnode;continue;
parent=(i-1)/2;
if(i%2==0)temp[parent]->right=newnode;
else temp[parent]->left=newnode;
}
}
int main()
{
int num;
printf("enter a binary number:");
scanf("%d",&num);
display(root);
}
爲什麼它應該顯示任何東西,如果你永遠不會調用'add'? – Leeor
什麼是要求,一棵平衡的樹,一個排序的樹,還有其他東西? – danh
danh一棵平衡的樹 –