(New page: 264 exam outcome 1.file 2.structure 3.dynamic structure (linked list) binary search tree typedef struct treenode { struct treenode *left; struct treenode *right; in...) |
|||
Line 1: | Line 1: | ||
+ | Hanye Xu April 20 Lecture 26 | ||
+ | |||
264 exam | 264 exam | ||
outcome 1.file | outcome 1.file |
Latest revision as of 18:10, 19 April 2012
Hanye Xu April 20 Lecture 26
264 exam outcome 1.file 2.structure 3.dynamic structure (linked list)
binary search tree
typedef struct treenode { struct treenode *left; struct treenode *right; int value; }Node;
int Tree_search(Node *n, int v) { /*return 0 if not found,1 if found*/ if(n == NULL){return 0;} if((n->value)==v){return 1;} if((n->value)>v) { return Tree_search(n->left,v); } return Tree_search(n->right,v); }
void Tree_destroy(Node *n) { if(n == NULL){return;} Tree_destroy(n->left); Tree_destroy(n->right); free(n); }
void Tree_print(Node *n) { if(n==NULL){return;} (1)printf("%d",n->value); 1 2 3 -> preorder 6 2 0 4 9 7 (2)Tree_print(n->left); 2 1 3 -> inorder sorting 0 2 4 6 7 9 (3)Tree_print(n->right); 2 3 1 -> postorder 0 4 2 7 9 6 }
Node *Node_construct(int v) { Node *n; n = malloc(sizeof(Node)); n->value = v; n->left = NULL; n->right = NULL; return n; }
Node *Tree_construct(Node *n, int v) { if(n==NULL){return Node_construct(v);} if((n->value)==v){return n;} if((n->value)>v) { n->left = Node_construct(n->left,v); } else { n->right= Node_construct(n->right,v); } return n; }
insert 6,2,4,0,9,7
Node *n =NULL;
n=Tree_insert(n,6);
n-> 6 (root) / \
GRD
n=Tree_insert(n,2);
n-> 6 / \ 2 GRD
/ \ GRD
n=Tree_insert(n,4)
n-> 6
/ \ 2 GRD / \
GRD 4 / \ GRD
Finally n-> 6
/ \ 2 9 / \ / \ 0 4 7 GRD
parser
int y = 3+2; int t = 3 + "ece";