1 / 34

Administration

Lecture12: Basic Data Structure with Binary Tree 12/10/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C. Administration. Assignment #11 is on the webpage. Outline. Binary Tree Write large programs with multiple files. A Binary Tree.

irenereid
Download Presentation

Administration

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture12: Basic Data Structure with Binary Tree12/10/2012Slides modified from Yin Lou, Cornell CS2022: Introduction to C

  2. Administration • Assignment #11 is on the webpage.

  3. Outline • Binary Tree • Write large programs with multiple files

  4. A Binary Tree • A tree is a data structure that stores data, like linked list. • Binary tree supports fast add-delete-search. • Each node in the tree leads to two further nodes. struct node { int val; struct node *left; struct node *right; }; new_node 10 left value right

  5. A Binary Tree: fast search (also add & delete) root 10 left value right 30 8 left value right left value right 40 20 5 left value right left value right left value right

  6. Binary Tree Data Structure • A tree node includes: • The data • Pointers to two children nodes (left and right) • 2 pointers == binary • Left node pointer points to a node with data less than the current node • All nodes to the left contain data less • Right node pointer points to a node with data greater than the current node • All nodes to the right contain data greater • A leaf node is a node with no children • A root node is the top node

  7. Add a node (20) - now add 15 • Simply add a leaf to the tree. • Add 20 10 10 8 30 8 30 40 20 5 40 5

  8. Add a node (15) – now add 9 10 8 30 5 20 40 15

  9. Add a node (9) – now add 25 10 8 30 9 5 20 40 15

  10. Add a node (25) 10 8 30 9 5 20 40 15 25

  11. Binary Tree Implementation in C struct node { int value; struct node *left; struct node *right; }; struct node *root = NULL; root

  12. Create a node struct node { int value; struct node *left; struct node *right; }; new_node = malloc(sizeof(struct node)); new_node->value = 10; new_node->left = NULL; new_node->right = NULL; new_node 10 left value right

  13. Insert a node (n=5) root struct node *new_node; new_node = malloc(sizeof(struct node)); new_node->value = n; new_node->left = NULL; new_node->right = NULL; if (root->value > n) root->right = new_node; 10 left value right root 10 left value right 5 left value right

  14. root Insert a node (n=20) 10 if (root->value > n) root->right = new_node; else if (root->value < n) root->left = new_node; left value right 5 left value right root 10 left value right 5 20 left value right left value right

  15. Insert a node 10 struct node *add_node(struct node *tree, int n) { if (tree == NULL) { struct node *new_node; new_node = malloc(sizeof(struct node)); new_node->value = n; new_node->left = NULL; new_node->right = NULL; tree = new_node; } else if (tree->value > n) tree->left = add_node(tree->left, n); else if (tree->value == n) printf("Error: %d already exists\n", n); else tree->right = add_node(tree->right, n); return tree; } 8 30 9 5 20 40 15 25

  16. Tree operations (like Linked List) Beside add_node(), other possible list operations: struct node *search_node(struct node *tree, int n); void print_tree(struct node *tree); struct node* delete_node(struct node *tree, int n); int sum_tree(struct node *tree); …

  17. struct node *search_node(struct node *tree, int n); 10 if (tree == NULL) return NULL; else if (tree->value > n) return search_node(tree->left, n); else if (tree->value == n) return tree; else return search_node(tree->right, n); 8 30 9 5 20 40 15 25

  18. In-Class Exercise 12.1 Copy and paste the skeleton code from the exercise. Implement the print_tree() function that prints values from small to large. void print_tree(struct node* tree);

  19. In-Class Exercise 12.2 Now reorder two lines of code in your print_tree() function to print values from large to small. void print_tree(struct node* tree);

  20. Delete a node • Find the deleted node • The node has no child • Has one child • Has two children root 10 left value right 30 8 left value right left value right 40 20 5 left value right left value right left value right

  21. Delete a node (no child: 5) root 10 left value right 30 8 left value right left value right 40 20 5 left value right left value right left value right

  22. Delete a node (one child: 8) root 10 left value right 30 8 left value right left value right 40 20 5 left value right left value right left value right

  23. Delete a node (two children: 10) root 10 left value right 30 8 left value right left value right 40 20 5 left value right left value right left value right

  24. Delete a node (two children: 10) root replaced with which node? 10 left value right 30 8 left value right left value right 40 20 5 left value right left value right left value right

  25. Delete a node (two children: 10) root 20 10 left value right 30 8 left value right left value right 40 20 5 left value right left value right left value right

  26. struct node *delete_node(struct node *tree, int n) /* Find the deleted node */ if (tree == NULL); else if (tree->value > n) tree->left = delete_node(tree->left, n); else if (tree->value < n) tree->right = delete_node(tree->right, n); else if (tree->value == n) { /* three cases: * (1) the node has no child * (2) the node has one child * (3) the node has two children */

  27. struct node *delete_node(struct node *tree, int n) /* three cases: * (1) the node has no child * (2) the node has one child * (3) the node has two children */ struct node *temp; if (tree->left == NULL) // one right child or none { temp = tree->right; free(tree); tree = temp; } else if (temp->right == NULL) // one left child or none { temp = tree->left; free(tree); tree = temp; }

  28. struct node *delete_node(struct node *tree, int n) /* * (3) the node has two children */ else // two children { /* find the smallest_node whose value > n * replace this smallest_node's value with tree's value * delete this smallest_node */ temp = tree->right; while (temp->left != NULL) temp = temp->left; tree->value = temp->value; tree->right = delete_node(tree->right, tree->value); } } return tree;

  29. In-Class Exercise 12.3 Implement the following function that returns the depth of a tree (the maximum number of nodes between the root and any leaf node in the tree). Note that this number also counts the root and the leaf node. int get_depth(struct node* tree)

  30. Write a large multi-file program • Small C programs can be put in one file. • Large C programs ought to be split into multiple files. One file implements one related group of functions. • Two types of files • Header files “xxx.h” • Source files “xxx.c”

  31. Header File • Place common material to be shared in a header file. • Shared function declarations (not function definitions) • Shared struct, enumeration, union, etc. • Shared MACROs (#define …) • External variables • Can be included as needed. Example: “mymath.h” int fact(int n); int power(int power, int exp); extern int global_var1; extern int global_var2;

  32. Source file: contain function definitions power.c #include "mymath.h" int global_var1; int power(int base, int exp) { int result = 1; int i; for (i = 1; i <= exp; ++i) { result *= base; } return result; } fact.c #include "mymath.h" int global_var2; int fact(int n) { if (n == 0) { return 1; } else { return n * fact(n - 1); } }

  33. Example main.c #include <stdio.h> #include "mymath.h" void main() { printf("%d\n", power(5, 3)); printf("%d\n", fact(5)); }

  34. In-Class Exercise 12.4 Create a multi-file program: - Move the tree functions (add_node, delete_node, etc.) into a file “tree.c” - Create a new header file “tree.h”. - The “main.c” file should have the main() function.

More Related