(Removing all content from page) |
(Inportant function of Binary Tree) |
||
Line 1: | Line 1: | ||
+ | //Inportant function of Binary Tree | ||
+ | |||
+ | typedef struct TreeNode_t { | ||
+ | int valude; | ||
+ | struct TreeNode_t *left, *right; | ||
+ | |||
+ | |||
+ | |||
+ | }TreeNode; | ||
+ | |||
+ | |||
+ | |||
+ | TreeNode *TreeNode_create(int value) | ||
+ | { | ||
+ | TreeNode *node = malloc(sizeof(TreeNode)); | ||
+ | Node->value=value; | ||
+ | node->left=NULL; | ||
+ | node->right=NULL; | ||
+ | return node; | ||
+ | } | ||
+ | |||
+ | |||
+ | TreeNode *Tree_insert(TreeNode *node, int value) | ||
+ | { | ||
+ | if (node==NULL) | ||
+ | return TreeNode_create(value); | ||
+ | if(node->value>=value) | ||
+ | node->left=Tree_insert(node->left,value); | ||
+ | else | ||
+ | node->right=Tree_insert(node->right,value); | ||
+ | return node; | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | void Tree_inorder(TreeNode *node) | ||
+ | { | ||
+ | if (node==NULL) | ||
+ | return ; | ||
+ | Tree_inorder(node->left); | ||
+ | printf("%d\n",node->value); | ||
+ | Tree_inorder(node->right); | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | int main(int argc, char *argc[]) | ||
+ | { | ||
+ | |||
+ | if(argc!=3) | ||
+ | { | ||
+ | return EXIT_FAILURE; | ||
+ | } | ||
+ | FILE *f=fopen(argv[1],"r"); | ||
+ | if (f==NULL) | ||
+ | { | ||
+ | return EXIT_FAILURE; | ||
+ | } | ||
+ | |||
+ | int num; | ||
+ | TreeNode *root=NULL; | ||
+ | while(fscanf(f,"%d",&sum)==1) | ||
+ | { | ||
+ | root = Tree_insert(root,num); | ||
+ | |||
+ | } | ||
+ | fclose(f); | ||
+ | Tree_inorder(root); | ||
+ | |||
+ | return EXIT_FAILURE; | ||
+ | } |
Revision as of 04:02, 25 April 2012
//Inportant function of Binary Tree
typedef struct TreeNode_t {
int valude; struct TreeNode_t *left, *right;
}TreeNode;
TreeNode *TreeNode_create(int value) {
TreeNode *node = malloc(sizeof(TreeNode)); Node->value=value; node->left=NULL; node->right=NULL; return node;
}
TreeNode *Tree_insert(TreeNode *node, int value)
{
if (node==NULL) return TreeNode_create(value); if(node->value>=value) node->left=Tree_insert(node->left,value); else node->right=Tree_insert(node->right,value); return node;
}
void Tree_inorder(TreeNode *node) {
if (node==NULL) return ; Tree_inorder(node->left); printf("%d\n",node->value); Tree_inorder(node->right);
}
int main(int argc, char *argc[]) {
if(argc!=3) { return EXIT_FAILURE; } FILE *f=fopen(argv[1],"r"); if (f==NULL) { return EXIT_FAILURE; }
int num; TreeNode *root=NULL; while(fscanf(f,"%d",&sum)==1) { root = Tree_insert(root,num);
}
fclose(f); Tree_inorder(root);
return EXIT_FAILURE; }