(New page: Category:ECE264 Category:programming Category:C ==Important function of Binary Tree== typedef struct TreeNode_t { int valude; struct TreeNode_t *left, *right; }TreeNod...) |
|||
Line 3: | Line 3: | ||
[[Category:C]] | [[Category:C]] | ||
− | ==Important function of Binary Tree== | + | ==[[ECE264]]: Important function of Binary Tree== |
typedef struct TreeNode_t { | typedef struct TreeNode_t { | ||
Line 77: | Line 77: | ||
} | } | ||
---- | ---- | ||
− | [[ | + | [[2011_Spring_ECE_264_Lu|Back to ECE264, Spring 2011, Prof. Lu]] |
Latest revision as of 05:44, 11 July 2012
ECE264: Important 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; }