Docsity

Recursion in Computer Science: Graphics, Fractals, Trees and Binary Search Trees, Study notes of Computer Science

Partial preview of the text

Download Recursion in Computer Science: Graphics, Fractals, Trees and Binary Search Trees and more Study notes Computer Science in PDF only on Docsity!

Intro to Programming II^ Recursion II^ Chris BrooksDepartment of Computer ScienceUniversity of San Francisco

Department of Computer Science โ€” University of San Francisco โ€“ p.1/

18-0:^ Recursion in graphics

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/

18-2:^ Fractals

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/

18-3:^ Koch Snowflake: exercise

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/

18-5:^ Tree Terminology

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/

18-6:^ Implementing a tree

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/

18-8:^ Adding methods to BST

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/

18-9:^ Finding a node

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/

18-11:^ Finding an Element in a BST Next, the Recursive Case โ€“ how do we make the problemsmaller?^ Department of Computer Science โ€” University of San Francisco โ€“ p.13/

18-12:^ Finding an Element in a BST Next, the Recursive Case โ€“ how do we make the problemsmaller? Both the left and right subtrees are smaller versions ofthe problem. Which one do we use?^ Department of Computer Science โ€” University of San Francisco โ€“ p.14/

18-14:^ Finding an Element in a BST Next, the Recursive Case โ€“ how do we make the problemsmaller? Both the left and right subtrees are smaller versions ofthe problem. Which one do we use? If the element we are trying to find is

<^ the element stored at the root, use the left subtree. Otherwise, usethe right subtree.

Department of Computer Science โ€” University of San Francisco โ€“ p.16/

18-15:^ Finding an Element in a BST Next, the Recursive Case โ€“ how do we make the problemsmaller? Both the left and right subtrees are smaller versions ofthe problem. Which one do we use? If the element we are trying to find is

<^ 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/

18-17:^ Finding an Element in a BST

To find an element^ e

in a Binary Search Tree

T^ :

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

T^ :

Look for^ e^ in the left subtree of

T

Otherwise^ Look for^ e^ in the right subtree of

T^ Department of Computer Science โ€” University of San Francisco โ€“ p.19/

18-18:^ Exercise

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/