Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Notes on Trees - Introduction to Programming II | CS 112, Study notes of Computer Science

Material Type: Notes; Class: Intro to Computer Science II; Subject: Computer Science; University: University of San Francisco (CA); Term: Fall 2005;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-ktn
koofers-user-ktn 🇺🇸

10 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to Programming II
Trees
Chris Brooks
Department of Computer Science
University of San Francisco
Department of Computer Science University of San Francisco p. 1/??
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Notes on Trees - Introduction to Programming II | CS 112 and more Study notes Computer Science in PDF only on Docsity!

Introduction to Programming II^ Trees^ Chris BrooksDepartment of Computer ScienceUniversity of San Francisco

Department of Computer Science — University of San Francisco – p. 1/

22-2: Trees^ •^ Previously, we’ve talked about how to store objects in a linearsequence.^ ◦^ Arrays^ ◦^ ArrayLists^ ◦^ LinkedLists^ •^ These are nice when we care about keeping everything inorder.^ •^ Finding particular elements can take a while, though.

Department of Computer Science — University of San Francisco – p. 2/

22-4: 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. 4/

22-5: Implementing a tree^ •^ A struct representing a tree containing strings:^ typedef^ struct^ treeNode

*treeptr; typedef^ struct^ treeNode

{ char^ *data;treeptr^ left;treeptr^ right; } treeNode; • We need to declare a type called ’treeptr’, because otherwisethe C compiler doesn’t know what a treeNode is until thedefinition is processed.

Department of Computer Science — University of San Francisco – p. 5/

22-7: Binary Search Trees^ •^ Binary Trees^ •^ For each node n, (value stored at node n)

^ (value stored in left subtree) • For each node n, (value stored at node n)

<^ (value stored in right subtree)

Department of Computer Science — University of San Francisco – p. 7/

22-8: Adding methods to BST void^ insert(treeptr^ root,

char^ {*newdata) treeNode^ *newNode;if^ (strcmp(root->data,

newdata)^ <=^ 0)^ { if (root->left == NULL)^ { newNode = makeNode(newdata);root->left = newNode;return; } else { return insert(root->left,^ newdata); } }^ else^ {^ if^ (root->right^ ==^

NULL)^ { newNode = makeNode(newdata);root->right = newNode;return; } else { return insert(root->right,^ newdata); } } }

Department of Computer Science — University of San Francisco – p. 8/

22-10: Finding an Element in a BST^ •^ First, the Base Case – when is it easy to determine if anelement is stored in a Binary Search Tree?^ ◦^ If the tree is empty, then the element can’t be there^ ◦^ If the element is stored at the root, then the element is there

Department of Computer Science — University of San Francisco – p. 10/

22-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. 11/

22-13: 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 of theproblem. 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, use the rightsubtree.

Department of Computer Science — University of San Francisco – p. 13/

22-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 of theproblem. 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, use the rightsubtree. • How do we use the solution to the subproblem to solve theoriginal problem?

Department of Computer Science — University of San Francisco – p. 14/

22-16: 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. 16/

22-17: Exercise^ •^ Implement the find(treeNode *root, char *object) method.^ •^ Write a main() that inserts names into the tree and tests find().^ •^ Count how many recursive calls it takes to find something.^ •^ How is this related to the number of elements in the tree?

Department of Computer Science — University of San Francisco – p. 17/

22-19: Counting nodes^ •^ So how would we count the number of nodes in a tree?

Department of Computer Science — University of San Francisco – p. 19/

22-20: Counting nodes^ •^ If the tree is null, return 0.^ •^ Otherwise, return 1 + the number of nodes in the left subtree +the number of nodes in the right subtree.^ •^ Exercise: add a countNodes(treeNode *root) function to ourtree data structure.

Department of Computer Science — University of San Francisco – p. 20/