Shiyu Wang Lec28 April 27th
typedef struct treenode {
struct treenode *left; struct treenode *right; int value;
}Node;
Node* root=NULL;
root=tree_insert(root,13);
root=tree_insert(root,7);
root=tree_insert(root,22);
Node*Tree_insert(Node*n,int v) {
if(n==NULL) { return Node_construct(v); } if((n->value)==v) { return n; } if((n->value)>v) { n->left=Tree_insert(n->left,v); } else { n->right=Tree_insert(n->right,v); } return n;
}
=========================================================
Huayi Guo section-1 Professor Niklas lecture 28
- /
- include<stdio.h>
- include<stdlib.h>
- include<string.h>
typedef struct TreeNode_t{
int value; struct TreeNode_t *left, *right;
}TreeNode;
TreeNode *Tree_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(TreNode *node) {
if(node==NULL)return ; Tree_inorder(node->left); printf("%d\n",node->value); Tree_inoder(node->right);
}
void *Tree_preorder(TreNode *node) {
if(node==NULL)return node; printf("%d\n",node->value); Tree_inorder(node->left); Tree_inoder(node->right);
}
TreeNode *Tree_search(TreeNode *node,int value) {
if(node == NULL)return NULL; if(node->value ==)return node; if(node->value < value)return Tree_search(node->left,value); else return Tree_search(node->right,value);
}
/*void *Tree_postorder(TreNode *node) {
if(node==NULL)return node; Tree_inorder(node->left); Tree_inoder(node->right); printf("%d\n",node->value);
}*/
void Tree_destroy(TreNode *node)
{
if(node==NULL)return; Tree_destroy(node->left);
Tree_destroy(node->right); free(node); }
int main(int argc, char *argv[]) {
if(argc != 3){
return EXIT_FAILURE; }
FILE *f = fopen(argv[1],"r");
if(f ==NULL){ return EXIT_FAILURE; }
int num;
while(fscanf(f, "%d",&num) == 1){ root = Tree_insert(root, num); }
fclose(f);
Tree_inorder(root); Tree_destroy(root);
return EXIT_SUCCESS; }