#include<stdio.h>
#include<stdlib.h>
typedef struct note{
int data;
struct note *left,*right;
} BTnode;
BTnode *CreateTree(int Data[],int n)
{
BTnode *root,*current,*parent,*insert;
//創建根節點
root=(BTnode *)malloc(sizeof(BTnode));
root->data=https://bbs.csdn.net/topics/Data[0];
root->left=root->right=NULL;
current=root;
for(int loop=1;loop<n;++loop)
{ insert=(BTnode *)malloc(sizeof(BTnode));
insert->data=https://bbs.csdn.net/topics/Data[loop];
insert->left=insert->right=NULL;
current=root;
while(current){
parent=current;
if(current->data>insert->data)
current=current->left;
else current=current->right;
if(parent->data>insert->data)
parent->left=insert;
else
parent->right=insert;
}
}
return root;
}
void Forder(BTnode *root)
{ if(root)
{ printf("%5d",root->data);
Forder(root->left);
Forder(root->right);
}
}
void Inorder(BTnode *root)
{ if(root){
Forder(root->left);
printf("%3d",root->data);
Forder(root->right);
}
}
void FDorder(BTnode *root)
{ if(root){
Forder(root->left);
Forder(root->right);
printf("%2d",root->data);
}
}
int main(void)
{ int Data[8]={6,4,3,8,2,9,5,7};
BTnode *root;
//創建二叉樹
root=CreateTree(Data,8);
Inorder(root);
printf("\n\n");
Forder(root);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/115933.html
標籤:基礎類
