more binary search trees , project 2
DESCRIPTION
More Binary Search Trees , Project 2. Bryce Boe 2013/08/ 06 CS24, Summer 2013 C. Outline. Lab 5 Solution Tree Traversals More Binary Search Trees Project 2. Thursday Recap. What is the depth of G?. 3. What is the height of B?. 1. What is the height of the tree?. 3. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/1.jpg)
More Binary Search Trees, Project 2
Bryce Boe2013/08/06
CS24, Summer 2013 C
![Page 2: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/2.jpg)
Outline
• Lab 5 Solution• Tree Traversals• More Binary Search Trees• Project 2
![Page 3: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/3.jpg)
THURSDAY RECAP
![Page 4: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/4.jpg)
What is the depth of G?
3
![Page 5: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/5.jpg)
What is the height of B?
1
![Page 6: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/6.jpg)
What is the height of the tree?
3
![Page 7: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/7.jpg)
Recursion question
• How many activation records are created when calling Fibonacci(0)?
• Fibonacci(1)?• Fibonacci(2)?• Fibonacci(3)?• Fibonacci(4)?• Fibonacci(5)?
11359
15
![Page 8: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/8.jpg)
Lab 5 Solution
• Going over in class• Make private request for bst.cpp if you need a
100% working solution
![Page 9: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/9.jpg)
Depth-First Tree Traversals
• Can be done iteratively (with a stack) or recursively• Pre-order– Process the node, recurse on left subtree, recurse on
right subtree• In-order– Recurse on the left subtree, process the node, recurse on
the right subtree• Post-order– Recurse on the left subtree, recurse on the right subtree, process the node
![Page 10: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/10.jpg)
Pre-Order TraversalA -> B - > D -> E -> C -> F -> G -> H
![Page 11: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/11.jpg)
In-Order TraversalD -> B -> E -> A -> C -> G -> F -> H
![Page 12: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/12.jpg)
Post-Order TraversalD -> E -> B -> G -> H -> F -> C -> A
![Page 13: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/13.jpg)
Breadth-first Traversal
• Cannot be done recursively• Done iteratively with the help of a queue
![Page 14: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/14.jpg)
Breadth-first (queue lhs before rhs)A -> B -> C -> D -> E -> F -> G -> H
![Page 15: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/15.jpg)
Question
• Which of the traversals will output a sorted version of a binary search tree?
![Page 16: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/16.jpg)
MORE BINARY SEARCH TREES
![Page 17: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/17.jpg)
Binary Search Trees
• Recall• A binary search tree is a tree with the
property that the value of all descendants of a node’s left subtree are smaller, and the value of all descendants of a node’s right subtree are larger
![Page 18: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/18.jpg)
BST Example
![Page 19: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/19.jpg)
BST Operations
• insert(item) – done in Lab 5– Add an item to the BST
• remove(item) – to complete in project 2– Remove an item from the BST
• contains(item) – done in Lab 5– Test whether or not the item is in the tree
![Page 20: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/20.jpg)
BST Remove
• If the node has no children simply remove it• If the node has a single child, update its parent
pointer to point to its child and remove the node
![Page 21: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/21.jpg)
Removing a node with two children
• Replace the value of the node with the largest value in its left-subtree (right-most descendant on the left hand side)
• Then repeat the remove procedure to remove the node whose value was used in the replacement
![Page 22: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/22.jpg)
Removing a node with two children
![Page 23: More Binary Search Trees , Project 2](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815ec9550346895dcd4dc8/html5/thumbnails/23.jpg)
PROJECT 2: VIRTUAL DIRECTORY TREE