Binary Search Tree For a node: The left subtree contains nodes with keys less than the node's key. The right subtree contains nodes with keys greater than.

Slides:



Advertisements
Similar presentations
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Advertisements

Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
Chapter 17 - I/O in C. BYU CS/ECEn 124Input and Output2 Concepts to Learn… Standard Libraries Input/Output Data Streams printf() scanf() Variable Argument.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
Standard Input and Output. Overview Data communication with a C program and the outside world is performed through files Files are a non-volatile way.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
1 Lecture09: Global Variables, File I/O, and Variable-Length Arguments 5/16/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Adv. UNIX:io/91 Advanced UNIX v Objectives of these slides: –look in some detail at standard input and output in C Special Topics in Comp.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
File IO and command line input CSE 2451 Rong Shi.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
1 Homework HW6 On line – due next class Starting K&R Chapter 7 and Appendix B Also, UNIX – Various chapters in Glass.
Lecture Starting K&R Chapter 7 and Appendix B Also, UNIX – Various chapters in Glass.
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
CS 261 – Recitation 7 Spring 2015 Oregon State University School of Electrical Engineering and Computer Science.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Complex Structures Nested Structures Self referential structures A structure may have Data variables Internal structures/unions Pointer links Function.
CS 1704 Introduction to Data Structures and Software Engineering.
Files A collection of related data treated as a unit. Two types Text
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
IO revisited CSE 2451 Rong Shi. stdio.h Functions – printf – scanf(normally stops at whitespace) – fgets – sscanf Standard streams – stdin(defaults to.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Chapter 9 - Formatted Input/Output
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
TMF1414 Introduction to Programming
Chapter 7 Text Input/Output Objectives
File Access (7.5) CSE 2031 Fall July 2018.
Introduction to C CSE 2031 Fall /3/ :33 AM.
CS 261 – Recitation 7 Fall 2013 Oregon State University
Input / Output functions in C
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
CS111 Computer Programming
CSE1320 Files in C Dr. Sajib Datta
File Input/Output.
Programming in C Input / Output.
Input and Output Lecture 4.
Input / Output functions in C
Programming in C Input / Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
CSC215 Lecture Input and Output.
Chapter 9 - Formatted Input/Output
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Text and Binary File Processing
Fundamental of Programming (C)
Lecture Starting K&R Chapter 7 and Appendix B
Programming in C Input / Output.
Weeks 9-10 IO System Calls Standard IO (stdin, stdout) and Pipes
Module 12 Input and Output
Introduction to C EECS May 2019.
File I/O & UNIX System Interface
CS1100 Computational Engineering
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
I/O CS580U - Fall 2018.
Presentation transcript:

Binary Search Tree For a node: The left subtree contains nodes with keys less than the node's key. The right subtree contains nodes with keys greater than the node's key. Both the left and right subtrees must also be binary search trees.

Binary Search Tree void insert (struct node *root, struct node *entry) { /* where to insert is really algorithm dependent. A generic case only: entry is to replace the left node of root */ if (head->left) { entry->left = head->left->left; entry->right = head->left->right; } head->left = entry; }; void delete (struct node *root, struct node *entry) { /* The key is to find a node that has an entry as a child */ // Three Cases: 1) Leaf node 2) A node with one child: remove the node and replace it with its child 3) A node with two children: Replace the value of entry with the largest value in the left, or the smallest value in the right, and delete the node with largest value in the left or the smallest value in the right. }; struct node * search (struct node *root, int val)//DFS { struct node * tmp = NULL; if (!root) return NULL; else if (root->data != val) tmp = search(root->left, val); if (!tmp) tmp = search(root->right, val); return tmp; } int main() { struct node num[10] = { … }; struct node root = {0, NULL, NULL}; for (int i=0; i<10; i++) { insert (&root, &num[i]); } … return 0; }

Breadth/Depth-first Traversal BFS: 1) A->B->C->D->E->F->G->H 2)A queue is needed to keep the nodes while the tree is being traversed DFS: 1)A->B->D->C->E->F->H->G 2)Usually a recursive function A BC DEFG H root

Weeks 8-9 IO Standard IO (stdin, stdout) and Pipes Formatted IO File IO System Calls System interface to obtain services from the OS, include many subsystems Memory Scheduler File/Storage System Inter-process Communication and Network, etc A popular subsystem: File system File descriptors Low level IO File Management Examples

Programs and Standard I/O Program Standard Input (STDIN) Keyboard Output from another program A file Standard Output (STDOUT) Screen Input to another program A file Standard Error (STDERR) stdin, stdout cmd < inputfile cmd > outputfile Numbered file descriptors 0: stdin: used by function scanf() 1: stdout: used by function printf() 2: stderr …

Pipes and Input/Output Redirection When a file descriptor is assigned to something other than a terminal, it is called I/O redirection A file (the contents of the file are fed to a program as if you typed it): wc < text_file A pipe (the output of another program is fed as input as if you typed it) The shell can attach things other than your screen to standard output. A file (the output of a program is stored in file): ls > lsout (>> for append) A pipe (the output of a program is fed as input to another program) Pipes cmd1 | cmd2 | cmd3 IO Redirection cmd1 outputfile: specifies the input and output file cmd1 | cmd2 2>&1 | cmd3 : redirect stderr(:2) to stdout and input it to cmd3

Pipes A pipe is a holder for a stream of data. A pipe can be used to hold the output of one program and feed it to the input of another. Separate 2 commands with the “|” character. prog1 prog2 STDOUT STDIN

Redirection/Pipe Examples wc < test1 ls > lsout ls >> lsout ls | wc

Formatted IO Output: printf Input: scanf Arguments: a format string, followed by the arguments printf (char *fmt, arg1, arg2, …) scanf (char *fmt, arg1, arg2, …) Arguments for scanf have to be the memory addresses

Formatting Output goes to stdout Input comes from stdin Format String: regular string + conversion specification Start of a format specification: % Width, precision, adjustment: Between ‘%’ and the conversion character, in order i) ’-’: specifies left adjustment of the converted argument. A number: the minimum field width.  ii) ‘.’: separates the field width from the precision: A number: precision. string(max num of characters); floating(num of digits after the decimal point); integer(min num of digits)  iii) l or h: long or short.  Example : Hello, World (12 characters) printf(“%s”, s); %-15s; %.10s; % s; %15.10s; Refer to Chapter 7.2 Formatted Output  Go to web page for more detailshttp://

String-based input/output String-based input int sscanf (char *str, char *fmt, arg1, arg2, …); String-based output int sprintf (char * str, const char * format,... ); Example: char st[]=“3.21, 4.33”; float x,y; sscanf(st,”%f,%f”,&x,&y);//result: x=3.21, y=4.33 char newst[32]; sprintf(newst,”%f, %f”, y, x);//result: newst=“4.33,3.21”

Variable length arguments A function may be called with a varying number of arguments of varying types #include, a new datatype: va_list //declare a variable: va_list ap Associated macro functions: va_start(ap, last) Initialize ap to be the va_list after the argument: last. This need to be done before va_arg and va_end va_arg(ap, int) Expand ap to an expression that has type/value that match int ap moves to the next argument in the variable list va_end(ap) Cleanup the variable argument list when done

Variable length arguments #include int add(int n,...); int main() { printf("%d\n", add(4,1,2,3,4)); printf("%d\n", add(3,1,2,3,)); return 0; } int add(int n,...) { int i, sum, tmp; va_list arg; va_start(arg, n); for(sum = i = 0; i < n; ++i){ tmp = va_arg(arg, int); sum += tmp; } va_end(arg); return sum; }

Variable length arguments void foo(char *fmt,...) { va_list ap; int d; char c, *p, *s; va_start(ap, fmt); while (*fmt) switch(*fmt++) { case 's': /* string */ s = va_arg(ap, char *); printf("string %s\n", s); break; case 'd': /* int */ d = va_arg(ap, int); printf("int %d\n", d); break; case 'c': /* char */ c = va_arg(ap, char); printf("char %c\n", c); break; } va_end(ap); }

File Accesses Files and file descriptors: FILE *fp; FILE *fopen(char *name, char *mode); Name can be a long path Mode: a combination of ‘r’, ‘w’, ‘x’, or ‘a’ File input/output int getc(FILE *fp) int putc(int c, FILE *fp); These works with default stdin/stdout: getchar() putchar() include “stdio.h” int main() { FILE *fp=fopen(“test.txt”,”r”); int c; int n=0; while((c=getc(fp))!=EOF) { if(c==‘$’) n++; } printf(“file contains %d $\n”,n); fclose(fp); }

File Accesses “r“: Open a file for reading. The file must exist. "w“: Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file. "a“: Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist. "r+“: Open a file for update both reading and writing. The file must exist. "w+“: Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file. "a+“: Open a file for reading and appending.

Line-oriented input/output int getline(char *line, size_t n, FILE *fp) char *fgets(char *line, size_t n, FILE *fp); gets(char *line, size_t n); /* buggy, never use it */ char *fputs(char *line, FILE *fp); Don’t use scanf() before fgets().

Error handling fprintf(stderr, char *fmt, arg1, arg2, …) exit(1): exit with non-zero status ferror(FILE *fp): test for any error on fp feof(FILE *fp) : test for end-of-file perror(char *s): print the error message, s, to stderr for the last system or library calls