1 / 22

CISC220 Fall 2009 James Atlas

CISC220 Fall 2009 James Atlas. Lecture 13: Trees. Skip Lists. Project 1. AI Graphics Networking Bio-informatics Natural Language Processing. Objectives for Today. Understand Trees/Terminology Use basic traversals on trees Understand binary search trees

meda
Download Presentation

CISC220 Fall 2009 James Atlas

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. CISC220Fall 2009James Atlas Lecture 13: Trees

  2. Skip Lists

  3. Project 1 • AI • Graphics • Networking • Bio-informatics • Natural Language Processing

  4. Objectives for Today • Understand Trees/Terminology • Use basic traversals on trees • Understand binary search trees • Construct and use binary search trees • Reading - K+W Chap 8

  5. Trees • Nonlinear data structure

  6. Tree Terminology • root, leaf • parent, child, sibling • subtree • external, internal node • ancestor, descendant • depth, height

  7. Binary Trees • Each node has 0, 1, or 2 children

  8. Binary Tree Application: Huffman Tree

  9. Tree Traversal • process of visiting each node • 4 different standard traversals: • preorder • inorder • postorder • level-order (also called breadth-first) • Interactive example: • http://nova.umuc.edu/~jarc/idsv/lesson1.html

  10. Traversal Exercise Find the: • preorder • inorder • postorder • level-order

  11. Exercise Answers • Preorder traversal sequence: F, B, A, D, C, E, G, I, H (root, left, right) • Inorder traversal sequence: A, B, C, D, E, F, G, H, I (left, root, right) • Postorder traversal sequence: A, C, E, D, B, H, I, G, F (left, right, root) • Level-order traversal sequence: F, B, G, A, D, I, C, E, H

  12. Binary Trees • Each node has 0, 1, or 2 children

  13. Binary Search Trees Binary search trees • Elements in left subtree < element in subtree root • Elements in right subtree > element in subtree root • Both the left and right subtrees are binary search trees

  14. Binary Search Tree (Example) • Is this a binary search tree?

  15. Binary Search Tree (Example)

  16. Searching a BST Search algorithm: • if the tree is empty, return NULL • if target equals to root node, return that data • if target < root node, return search(left subtree) • otherwise return search(right subtree)

  17. find(7) find(12) find(0) find(14)

  18. Inserting to a Binary Search Tree if root is NULL replace empty tree with new data leaf; else if item < root->data return insert(left subtree, item) else return insert(right subtree, item) • http://www.cs.jhu.edu/~goodrich/dsa/trees/btree.html

  19. Inserting to a Binary Search Tree

  20. Removing from a Binary Search Tree • Item not present: do nothing • Item present in leaf: remove leaf (change to null) • Item in non-leaf with one child: Replace current node with that child • Item in non-leaf with two children? • Find largest item in the left subtree • Recursively remove it • Use it as the parent of the two subtrees • (Could use smallest item in right subtree)

  21. Removing from a Binary Search Tree

  22. Order of operations?

More Related