cisc220 fall 2009 james atlas lecture 13: trees. skip lists
TRANSCRIPT
![Page 1: CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649ebb5503460f94bc4220/html5/thumbnails/1.jpg)
CISC220Fall 2009
James Atlas
Lecture 13: Trees
![Page 2: CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649ebb5503460f94bc4220/html5/thumbnails/2.jpg)
Skip Lists
![Page 3: CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649ebb5503460f94bc4220/html5/thumbnails/3.jpg)
Project 1
• AI
• Graphics
• Networking
• Bio-informatics
• Natural Language Processing
![Page 4: CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649ebb5503460f94bc4220/html5/thumbnails/4.jpg)
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
![Page 5: CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649ebb5503460f94bc4220/html5/thumbnails/5.jpg)
Trees
• Nonlinear data structure
![Page 6: CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649ebb5503460f94bc4220/html5/thumbnails/6.jpg)
Tree Terminology
• root, leaf
• parent, child, sibling
• subtree
• external, internal node
• ancestor, descendant
• depth, height
![Page 7: CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649ebb5503460f94bc4220/html5/thumbnails/7.jpg)
Binary Trees
• Each node has 0, 1, or 2 children
![Page 8: CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649ebb5503460f94bc4220/html5/thumbnails/8.jpg)
Binary Tree Application: Huffman Tree
![Page 9: CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649ebb5503460f94bc4220/html5/thumbnails/9.jpg)
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
![Page 10: CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649ebb5503460f94bc4220/html5/thumbnails/10.jpg)
Traversal Exercise
Find the:
• preorder
• inorder
• postorder
• level-order
![Page 11: CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649ebb5503460f94bc4220/html5/thumbnails/11.jpg)
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
![Page 12: CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649ebb5503460f94bc4220/html5/thumbnails/12.jpg)
Binary Trees
• Each node has 0, 1, or 2 children
![Page 13: CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649ebb5503460f94bc4220/html5/thumbnails/13.jpg)
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
![Page 14: CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649ebb5503460f94bc4220/html5/thumbnails/14.jpg)
Binary Search Tree (Example)
• Is this a binary search tree?
![Page 15: CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649ebb5503460f94bc4220/html5/thumbnails/15.jpg)
Binary Search Tree (Example)
![Page 16: CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649ebb5503460f94bc4220/html5/thumbnails/16.jpg)
Searching a BST
Search algorithm:1. if the tree is empty, return NULL
2. if target equals to root node, return that data
3. if target < root node, return search(left subtree)
4. otherwise return search(right subtree)
![Page 17: CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649ebb5503460f94bc4220/html5/thumbnails/17.jpg)
find(7)
find(12)
find(0)
find(14)
![Page 18: CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649ebb5503460f94bc4220/html5/thumbnails/18.jpg)
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
![Page 19: CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649ebb5503460f94bc4220/html5/thumbnails/19.jpg)
Inserting to a Binary Search Tree
![Page 20: CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649ebb5503460f94bc4220/html5/thumbnails/20.jpg)
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)
![Page 21: CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649ebb5503460f94bc4220/html5/thumbnails/21.jpg)
Removing from a Binary Search Tree
![Page 22: CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649ebb5503460f94bc4220/html5/thumbnails/22.jpg)
Order of operations?