



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















Department of Computer Science โ University of San Francisco โ p.1/
We can use recursion to easily create interestinggraphical effects. For example, recursively tiling a surface.
Department of Computer Science โ University of San Francisco โ p.2/
We can also use recursion to draw fractals Example: Koch snowflake Rule: Each line segment is replaced by a โwedgeโ withsides that are the same length as the replaced piece. As we increase the depth, it begins to look like asnowflake.
Department of Computer Science โ University of San Francisco โ p.4/
Change the color scheme of the applet. Change the default max and min values Change the initial triangle to have its โpointโ downward.
Department of Computer Science โ University of San Francisco โ p.5/
Parent / Child Leaf node Root node Edge (between nodes) Path Ancestor / Descendant Depth of a node^ n^ Length of path from root to
n Height of a tree^ (Depth of deepest node) + 1
Department of Computer Science โ University of San Francisco โ p.7/
Treenode class: public^ class^ TreeNode^ {^ private^ Comparable^ data;private^ TreeNode^ left;private^ TreeNode^ right;public^ Object^ getData()
{return^ data;^ } }
Department of Computer Science โ University of San Francisco โ p.8/
public^ TreeNode^ insert(TreeNode
tree,^ Comparable^ elem)
{ if^ (tree^ ==^ null)^ {^ return^ new^ TreeNode(elem);if^ (elem.compareTo(tree.getData())
<^ 0)^ { tree.setLeft(insert(tree.left(),
elem)); return^ tree; } else^ { tree.setRight(insert(tree.right(),
elem)); return^ tree; } }
Department of Computer Science โ University of San Francisco โ p.10/
First, the Base Case โ when is it easy to determine if anelement is stored in a Binary Search Tree?
Department of Computer Science โ University of San Francisco โ p.11/
<^ the element stored at the root, use the left subtree. Otherwise, usethe right subtree.
Department of Computer Science โ University of San Francisco โ p.16/
<^ the element stored at the root, use the left subtree. Otherwise, usethe right subtree. How do we use the solution to the subproblem to solve theoriginal problem?
Department of Computer Science โ University of San Francisco โ p.17/
To find an element^ e
in a Binary Search Tree
If^ T^ is empty, then^ e^
is not in^ T If the root of^ T^ contains
e, then^ e^ is in^ T If^ e <^ the element stored in the root of
Look for^ e^ in the left subtree of
Otherwise^ Look for^ e^ in the right subtree of
T^ Department of Computer Science โ University of San Francisco โ p.19/
Copy the TreeNode class, including the insert method. Insert some numbers. implement the find() method. How many recursive calls does it take to find something? How is this related to the number of elements in the tree?
Department of Computer Science โ University of San Francisco โ p.20/