1 / 24

Understanding Trees: Structure, Traversal, and Implementations for Efficient Data Handling

Explore the importance of trees in data structures, learn about ordered trees, tree traversals, and implementations like Binary Search Trees (BSTs) with detailed examples and algorithms presented.

rschroeder
Download Presentation

Understanding Trees: Structure, Traversal, and Implementations for Efficient Data Handling

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

  2. Why a tree? • Faster than linear data structures • More natural fit for some kinds of data • Examples?

  3. Example Tree root Sami’s Home Page Teaching Research Activities CS101 CS211 Papers Presentations

  4. Terminology • Root • Parent • Child • Sibling • External node • Internal node • Subtree • Ancestor • Descendant

  5. Example Tree root Sami’s Home Page Teaching Research Activities CS101 CS211 Papers Presentations External nodes Internal nodes Subtree – left subtree of research? Ancestor – papers ancestor of activities? Descendant – papers descendant of home? Root? Parent – papers, activities Children – cs101, research Sibling - teaching

  6. Ordered Trees • Linear relationship between child nodes • Binary tree – max two children per node • Left child, right child root Rollins Truman Davidson Taft Zuniga Brown Ralson

  7. Another Ordered Binary Tree root Brown Truman Taft Zuniga Ralson Davidson Rollins

  8. Tree Traversal • Pre-order traversal • Visit node, traverse left subtree, traverse right subtree • Post-order traversal • Traverse left subtree, traverse right subtree, visit node

  9. Example • Pre-order • Post-order root Rollins Truman Davidson Taft Zuniga Brown Ralson

  10. Example • Pre – Rollins, Davidson, Brown, Ralson, Truman, Taft, Zuniga • Post – Brown, Ralson, Davidson, Taft, Zuniga, Truman, Rollins root Rollins Trimmer Do Tilkidjieva Yucius Bendersky Reardon

  11. Another Example • Pre – Brown, Truman, Taft, Ralson, Davidson, Rollins, Zuniga • Post – Davidson, Rollins, Ralson, Taft, Zuniga, Truman, Brown root Brown Truman Taft Zuniga Ralson Davidson Rollins

  12. In-order Traversal • Traverse left subtree, visit node, traverse right subtree • Brown, Davidson, Ralson, Rollins, Taft, Truman, Zuniga root Rollins Truman Davidson Taft Zuniga Brown Ralson

  13. Another Example • In-order – Brown, Davidson, Ralson, Rollins, Taft, Truman, Zuniga root Brown Truman Taft Zuniga Ralson Davidson Rollins

  14. Implementation – TreeNode • Data members? • Functions? Name = Rollins

  15. Implementation – Tree root Rollins • Data Members • Functions • Pre/post/in-order print Smith Brown

  16. Implementation – Pre-order void preOrderPrint(TreeNode* curnode) { o.print(); if(curnode->getLeftChild() != NULL) preOrderPrint(curnode->getLeftChild()); if(curnode->getRightChild() != NULL) preOrderPrint(curnode->getRightChild()); } Tree* t = …; t->preOrderPrint(t->getRoot());

  17. BSTs • Elements in left subtree nodes are before (are less than) element in current node • Element in current node is before (less than) elements in right subtree

  18. find Operation • Algorithm for finding element in BST root Brown Truman Taft Zuniga Ralson Davidson Rollins

  19. find Algorithm if current node is null return not found else if target is in current node return found else if target is before current node return find(left child) else return find(right child)

  20. find Complexity • Worst case • Best case • Average case

  21. insert Operation • Algorithm for inserting element in BST root Brown Truman Taft Zuniga Ralson Davidson Rollins

  22. insert Algorithm if new_elt is before current and current left child is null insert as left child else if new_elt is after current and current right child is null insert as right child else if new_elt is before current insert in left subtree else insert in right subtree

  23. remove Operation • Algorithm for removing element in BST root Brown Truman Taft Zuniga Ralson Davidson Rollins

  24. remove Algorithm elt = find node to remove if elt left subtree is null replace elt with right subtree else if elt right subtree is null replace with left subtree else find successor of elt (go right once and then left until you hit null) replace elt with successor call remove on successor

More Related