week 7 part 2

46
Week 7 Part 2 Kyle Dewey

Upload: arty

Post on 14-Jan-2016

32 views

Category:

Documents


0 download

DESCRIPTION

Week 7 Part 2. Kyle Dewey. Overview. NULL Recursion Exam #2 Review. NULL. Recall. When a file can’t be opened with fopen , it returns NULL NULL is a special value in C. NULL. Is zero at the binary representation Is a pointer Can be assigned to any pointer type. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Week 7 Part 2

Week 7 Part 2Kyle Dewey

Page 2: Week 7 Part 2

Overview

•NULL

•Recursion

•Exam #2 Review

Page 3: Week 7 Part 2

NULL

Page 4: Week 7 Part 2

Recall...

•When a file can’t be opened with fopen, it returns NULL

•NULL is a special value in C

Page 5: Week 7 Part 2

NULL

•Is zero at the binary representation

•Is a pointer

•Can be assigned to any pointer type

char* string = NULL;int* arr = NULL;

Page 6: Week 7 Part 2

NULL

•Can be used as a sentinel value

•Often used to show that an operation couldn’t be performed

•Return NULL if we can’t open a file, etc.

Page 7: Week 7 Part 2

Example#define LENGTH 4int array[] = { 1, 2, 3, 4 };int* subarray( int start ) { if ( start >= 0 && start < LENGTH ) { return &(array[ start ]); } else { return NULL; }}

Page 8: Week 7 Part 2

Caveats•If we try to dereference a NULL pointer,

the program will crash, usually with a segmentation fault

•This means it tried to access memory that it didn’t have permission to access

char* string = NULL;string[ 2 ] = ‘f’; // crashprintf( “%s\n”, string ); // crash

Page 9: Week 7 Part 2

Caveat

•It can also make code trickier

•Is NULL a possible value?

•In real code, there are places where NULL is impossible but people check anyway (and vice-versa!)

Page 10: Week 7 Part 2

Recursion

Page 11: Week 7 Part 2

Binary Tree•Mathematical concept

•A special kind of graph

Page 12: Week 7 Part 2

Counting

•Basic operation: how many nodes are in the tree?

Page 13: Week 7 Part 2

Depth

•Basic operation: how deep is the tree?

Page 14: Week 7 Part 2

Representation

•Nodes: the circles

•Edges: things that connect the circles

•Nodes in a binary tree have at most two edges connecting to other nodes

•No cycles

Page 15: Week 7 Part 2

Representation

•Each node has two edges

•An edge is either connected or it’s not

Page 16: Week 7 Part 2

Code Representation

•Hint: nodes should be represented as structs

•What would this definition look like?

Page 17: Week 7 Part 2

Code Representation

•Hint: nodes should be represented as structs

•What would this definition look like?

struct Node { struct Node* left; struct Node* right;};

Page 18: Week 7 Part 2

Code Representation

•Represent nodes as struct Nodes

•If there is not a connection, use NULL

struct Node { struct Node* left; struct Node* right;};

Page 19: Week 7 Part 2

Recursion

•A struct Node holds pointers to other struct Nodes

•A struct Node is defined in terms of itself!

Page 20: Week 7 Part 2

Recursion

•In general, this means there is something defined in terms of itself

•Can be data structures (like structs)

•Can be functions (a little later)

•Broken up into recursive cases and base cases

Page 21: Week 7 Part 2

Base Case

•Something not defined in terms of itself

•Act to end the recursion

•Can be multiple base cases

•For a struct Node, this means NULL

Page 22: Week 7 Part 2

Recursive Case

•Case that is defined in terms of itself

•This is a struct Node that connects to another struct Node

Page 23: Week 7 Part 2

Tree as a Whole

•How to represent this?

•Interesting note: there are subtrees

Page 24: Week 7 Part 2

Tree as a Whole

•Can simply use a struct Node without anything else

•This is a very flexible representation

Page 25: Week 7 Part 2

Operations

•So keeping this representation in mind...

Page 26: Week 7 Part 2

Counting

•Basic operation: how many nodes are in the tree?

Page 27: Week 7 Part 2

Base Case

•A tree that is not there (i.e. NULL) has no nodes (i.e. 0 nodes)

Page 28: Week 7 Part 2

Base Case

•A tree that is not there (i.e. NULL) has no nodes (i.e. 0 nodes)

if ( node == NULL ) { return 0;}

Page 29: Week 7 Part 2

Recursive Case•Given:

•The number of nodes on the left

•The number of nodes on the right

•How many nodes are here?

... ...(3 nodes)(2 nodes)

Page 30: Week 7 Part 2

Recursive Case

... ...(3 nodes)(2 nodes)

nodesLeft + nodesRight + 1;(3) (2) (current node)

Page 31: Week 7 Part 2

Full Code

Page 32: Week 7 Part 2

Depth

•Basic operation: how deep is the tree?

Page 33: Week 7 Part 2

Base Case

•A tree that is not there (i.e. NULL) has no depth (i.e. 0)

Page 34: Week 7 Part 2

Base Case

•A tree that is not there (i.e. NULL) has no depth (i.e. 0)

if ( node == NULL ) { return 0;}

Page 35: Week 7 Part 2

Recursive Case•Given:

•The depth of the tree on the left

•The depth of the tree on the right

•How deep is the tree?

... ...(depth 3)(depth 2)

Page 36: Week 7 Part 2

Recursive Case

... ...(depth 3)(depth 2)

(3) (2) (current node)max( depthLeft, depthRight ) + 1

Page 37: Week 7 Part 2

Full Code

Page 38: Week 7 Part 2

Exam #2

Page 39: Week 7 Part 2

Exam #2

•Exam is unintentionally cumulative

•Still need to know how to use if, assignment, etc.

•Will not focus on that material

Page 40: Week 7 Part 2

Focus

•Functions

•Prototype

•Definition

•Calls

•For all of these, what it is and how to do it

Page 41: Week 7 Part 2

Focus

•Loops (while, do/while, for)

•How to read them

•How to write them

•Be able to say what code does (i.e. the variable x is 5 after this code runs)

Page 42: Week 7 Part 2

Focus

•Arrays

•Initialize them

•Index into them to get / set values

•“Given an array of length 10, find the first element that...”

Page 43: Week 7 Part 2

Focus

•File I/O

•Opening / closing

•Reading / writing

Page 44: Week 7 Part 2

Focus

•Types

•Be able to identify the type of an expression

•Just like last time, except now pointers are fully in the mix

Page 45: Week 7 Part 2

Focus

•Structs

•You will not have to trace crazy pointer logic

•You will need to know how to access them and set fields in them

•Know what -> does

Page 46: Week 7 Part 2

Don’t Worry About

•Recursion

•typedef