Week 11 - Monday What did we talk about last time? enum Doubly - - PowerPoint PPT Presentation

week 11 monday what did we talk about last time enum
SMART_READER_LITE
LIVE PREVIEW

Week 11 - Monday What did we talk about last time? enum Doubly - - PowerPoint PPT Presentation

Week 11 - Monday What did we talk about last time? enum Doubly linked lists There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make


slide-1
SLIDE 1

Week 11 - Monday

slide-2
SLIDE 2

 What did we talk about last time?  enum  Doubly linked lists

slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5

There are two ways of constructing a software design. One way is to make it so simple that there are obviously no

  • deficiencies. And the other way is to make it so

complicated that there are no obvious deficiencies. Charles Antony Richard Hoare Inventor of Quicksort

slide-6
SLIDE 6
slide-7
SLIDE 7

 Node consists of data, a next pointer, and a previous pointer  Advantages: bi-directional movement  Disadvantages: slower, 4 pointers must change for every

insert/delete

X

head 23 47 58

X

tail

slide-8
SLIDE 8

 We'll use this definition for our node for doubly linked lists  Somewhere, we will have the following variables to hold the beginning

and ending of the list

typedef struct _node { int data; struct _node* next; struct _node* previous; } node; node* head = NULL; node* tail = NULL;

slide-9
SLIDE 9

 Let's define a function that adds a value to the back of a

(possibly empty) doubly linked list

 Since the head and the tail might both get updated, the only

reasonable way to write the function is to take a pointer to each

void addToBack(node** headPointer, node** tailPointer, int value);

slide-10
SLIDE 10
slide-11
SLIDE 11

 A tree is a data structure built out of nodes with children

  • Every child has exactly one parent node
  • There are no loops in a tree
  • A tree expresses a hierarchy or a similar relationship

 The root is the top of the tree, the node which has no parents  A leaf of a tree is a node that has no children  An inner node is a node that does have children  An edge or a link connects a node to its children  A subtree is a node in a tree and all of its children

slide-12
SLIDE 12

 A binary tree is a tree such that each node has two or fewer

children

 The two children of a node are generally called the left child

and the right child, respectively

slide-13
SLIDE 13

 A binary search tree is binary tree with three properties: 1.

The left subtree of the root only contains nodes with keys less than the root’s key

  • 2. The right subtree of the root only contains nodes with keys greater

than the root’s key

3.

Both the left and the right subtrees are also binary search trees

slide-14
SLIDE 14

4 2 5 1 3 6

slide-15
SLIDE 15

typedef struct _Tree { int data; struct _Tree* left; struct _Tree* right; } Tree;

slide-16
SLIDE 16

 Write a function that will find an element in a BST  Use recursion  Hints:

  • If the value is smaller than the current root, look to the left
  • If the value is larger than the current root, look to the right

Tree* find(Tree* root, int value);

slide-17
SLIDE 17

 Write a function that will add an element to a BST  Use recursion  Hint: Look for the location where you would add the element,

then add when you reach a NULL

Tree* add(Tree* root, int value);

slide-18
SLIDE 18
slide-19
SLIDE 19

 Think of a file as a stream of bytes  It is possible to read from the stream  It is possible to write to the stream  It is even possible to do both  Central to the idea of a stream is also a file stream pointer,

which keeps track of where in the stream you are

 We have been redirecting stdin from and stdout to files,

but we can access them directly as well

slide-20
SLIDE 20

 To open a file, call the fopen() function  It returns a pointer to a FILE object  Its first argument is the path to the file as a null-terminated

string

 Its second argument is another string that says how it is being

  • pened (for reading, writing, etc.)

FILE* file = fopen("data.txt", "r");

slide-21
SLIDE 21

 The following are legal arguments for the second string

Argument Meaning "r" Open for reading. The file must exist. "w" Open for writing. If the file exists, all its contents will be erased. "a" Open for appending. Write all data to the end of the file, preserving anything that is already there. "r+" Open a file for reading and writing, but it must exist. "w+" Open a file for reading and writing, but if it exists, its contents will be erased. "a+" Open a file for reading and writing, but all writing is done to the end of the file.

slide-22
SLIDE 22

 Once you've got a file open, write to it using fprintf() the

same way you write to the screen with printf()

 The first argument is the file pointer  The second is the format string  The third and subsequent arguments are the values

FILE* file = fopen("output.dat", "w"); fprintf(file, "Yo! I got %d on it!\n", 5);

slide-23
SLIDE 23
slide-24
SLIDE 24

 Review  Lab tomorrow

slide-25
SLIDE 25

 Keep working on Project 4  Read K&R Chapter 7  Exam 2 is Friday