1 / 17

Trees in C

Trees in C. CSE 2541 Rong Shi. Tree definition. Recursively defined data structure Tree (in general) Empty Data + a specific number of subtrees Binary tree Empty Data + left subtree + right subtree. C Binary Tree node.

morty
Download Presentation

Trees in C

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. Trees in C CSE 2541 Rong Shi

  2. Tree definition • Recursively defined data structure • Tree (in general) • Empty • Data + a specific number of subtrees • Binary tree • Empty • Data + left subtree + right subtree

  3. C Binary Tree node structbtnode{ int data; structbtnode *left; structbtnode*right; } ;

  4. What is this? structtreenode{ int data; structtreenode *mynode; } ; Equivalent to a linked list

  5. Creating a tree structbtnode *root; structbtnode *mynode= (structbtnode *) malloc (sizeof(structbtnode)); root = mynode; // use root to access the tree, like head for a linked list

  6. Visual example of a binary tree Each value corresponds to a node in the tree root is a structbtnode pointer that points at the node containing the 9

  7. Tree traversal (preorder) PreOrderPrint(structbtnode*anode) { printf(“%i”, anode->data); PreOrderPrint(anode->left); PreOrderPrint(anode->right); } Any problems with this function?

  8. Tree traversal (preorder corrected) PreOrderPrint(structbtnode*anode) { if(anode == NULL) return; printf(“%i ”, anode->data); PreOrderPrint(anode->left); PreOrderPrint(anode->right); } Output of PreOrderPrint(root) is: (po9pt9po6pt6… see blackboard) 9 6 2 7 15 12 25

  9. Tree traversal (inorder) InOrderPrint(structbtnode*anode) { if(anode == NULL) return; InOrderPrint(anode->left); printf(“%i ”, anode->data); InOrderPrint(anode->right); } Output of InOrderPrint(root) is: 2 6 7 9 12 15 25

  10. Tree traversal (postorder) PostOrderPrint(structbtnode*anode) { if(anode == NULL) return; PostOrderPrint(anode->left); PostOrderPrint(anode->right); printf(“%i ”, anode->data); } Output of PostOrderPrint(root) is: 2 7 6 12 25 15 9

  11. Tree termination • NULL pointers NULL are not btnodes, but the value of their parent’s left and right pointers • NULL data -1 are btnodes, whose left and right btnodes are uninitialized Assumption: -1 is never valid data in the tree

  12. Creating nodes structnode * NewNode(int data) { struct node *mynode= (struct node *) malloc (sizeof(struct node)); mynode->data = data;mynode->left = NULL;mynode->right = NULL; return(node); }

  13. Deallocating binary trees • Three things to do • Free current node • Recursively free left subtree • Recursively free right subtree • What is the order? void delete_tree(structbtnode*leaf) { if( leaf != NULL ) { delete_tree(leaf->left); delete_tree(leaf->right); free( leaf ); } }

  14. Trees and arrays Map current node, left child, and right child to array positions Order in the array by level in the tree Figure from http://scientopia.org/blogs/goodmath/2008/04/29/implementing-compact-binary-heaps/

  15. Trees and arrays Map current node, left child, and right child to array positions t[i] , t[2i+1] , t[2i+2] Order in the array by level in the tree Figure from http://scientopia.org/blogs/goodmath/2008/04/29/implementing-compact-binary-heaps/

  16. Additional Terminology • Depth: number of edges from the root to the node (node ‘7’ has depth 2, etc) • Height: number of edges from the node to the deepest leaf • Height of Tree: height of the root (tree-height = 2)

  17. Reference • Wiki http://en.wikipedia.org/wiki/Binary_search_tree http://en.wikipedia.org/wiki/Tree_(data_structure) Slides from CMU http://www.cs.cmu.edu/~adamchik/15-121/lectures/Trees/trees.html

More Related