CS 261 – Recitation 7 Fall 2013 Oregon State University

Slides:



Advertisements
Similar presentations
CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it.
Advertisements

CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
1 CSE1301 Computer Programming: Lecture 9 Input/Output.
AVL Trees ITCS6114 Algorithms and Data Structures.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
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.
File Handling Spring 2013Programming and Data Structure1.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
File IO and command line input CSE 2451 Rong Shi.
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
CSE1301 Computer Programming: Lecture 6 Input/Output.
CS 261 – Recitation 7 Spring 2015 Oregon State University School of Electrical Engineering and Computer Science.
AVL TREES By Asami Enomoto CS 146 AVL Tree is… named after Adelson-Velskii and Landis the first dynamically balanced trees to be propose Binary search.
Files A collection of related data treated as a unit. Two types Text
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
IO revisited CSE 2451 Rong Shi. stdio.h Functions – printf – scanf(normally stops at whitespace) – fgets – sscanf Standard streams – stdin(defaults to.
1 Computer Programming Lecture 15 Text File I/O Assist. Prof Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
 2007 Pearson Education, Inc. All rights reserved C File Processing.
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
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
CSIT 402 Data Structures II
File I/O.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Introduction Applications Balance Factor Rotations Deletion Example
AVL Search Trees Introduction What is an AVL Tree?
AVL Tree Mohammad Asad Abbasi Lecture 12
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
AVL Tree 27th Mar 2007.
Trees (Chapter 4) Binary Search Trees - Review Definition
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
Computer Programming Lecture 15 Text File I/O
Programming in C Input / Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
AVL Trees CENG 213 Data Structures.
CSI 121 Structured Programming Language Lecture 7: Input/Output
Chapter 11 – File Processing
CS202 - Fundamental Structures of Computer Science II
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
CS 367 – Introduction to Data Structures
AVL Trees: AVL Trees: Balanced binary search tree
CS202 - Fundamental Structures of Computer Science II
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
AVL Trees Lab 11: AVL Trees.
Programming and Data Structure
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
CSE 373: Data Structures and Algorithms
File Input and Output.
Fundamental of Programming (C)
CS202 - Fundamental Structures of Computer Science II
Lecture 9: Self Balancing Trees
EPSII 59:006 Spring 2004.
ITCS6114 Algorithms and Data Structures
Module 12 Input and Output
CSE 373 Data Structures Lecture 8
CSc 352 File I/O Saumya Debray Dept. of Computer Science
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

CS 261 – Recitation 7 Fall 2013 Oregon State University School of Electrical Engineering and Computer Science CS 261 – Recitation 7 Fall 2013 Last updated on Oct 5, 2012 by Tuan Pham 1 1

Outline AVL trees Assignment 5 – Heap Implementation of a ToDo List File handling and standard I/O in C Parts of this lecture were taken from: www.cs.txstate.edu/~rp44/cs3358_089/Lectures/bst.ppt www.cs.sjsu.edu/~lee/cs146/Asami-avl-presentation.ppt CS 261 – Data Structures 2 2

An AVL Tree is… A binary search tree with a balance condition. The sub-trees of each node differ by at most 1 in their height Named after its creators (Adelson-Velskii and Landis) 3

Definition of a balanced tree Every node must have left & right sub-trees of heights that differ by no more than 1 Ensures the tree’s height = log N Takes O(log N) time for searching, insertion, and deletion 4

An AVL tree has the following properties Sub-trees of each node can differ by at most 1 in their height Every sub-tree is an AVL tree 5

Are these AVL trees? YES Each left sub-tree has height 1 greater than each right sub-tree NO Left sub-tree of the root has height 2, but right sub-tree has height 0 6

Insertion and Deletions Performed as in binary search trees If the balance is destroyed, rotations are performed to correct balance For insertions, one single or double rotation is required. For deletions, at most O(log n) rotations are needed http://www.brpreiss.com/books/opus4/html/page327.html When an AVL tree becomes unbalanced after an insertion, exactly one single or double rotation is required to balance the tree. 7 7

Single Rotation Rotate the heavy node (1) to the right. Node (2) is heavy on the left child, which is also heavy on the left. Rotate the heavy node (1) to the right. 8

Double Rotation First, rotate heavy node (1) to the left Node (3) is heavy on the left child, which is heavy on the right. First, rotate heavy node (1) to the left 9

Double Rotation Next, rotate unbalanced node (3) to the right 10

Insertion Insert 6 Imbalance at 8 Perform rotate right 11

Deletion Imbalance at 3 Perform rotation at 3 Imbalance at 5 Delete 4 Imbalance at 3 Perform rotation at 3 Imbalance at 5 Perform rotation at 5 12

Key Points AVL tree remains balanced by applying rotations, therefore it guarantees O(log N) search time in a dynamic environment Tree can be re-balanced in at most O(log N) time 13

When to do a double rotation? Balance factor = height(left tree) – height(right tree) For node N, a double rotation is needed when: N’s balance factor is positive and N’s left subtree’s balance factor is negative N’s balance factor is negative and N’s right subtree’s BF is positive. CS 261 – Data Structures 14 14

Exercises: Insert 1-7 to an empty AVL tree Rebalancing is performed bottom up after a new value has been inserted, and only if the difference in heights of the child trees are more than one. 1(2) 2(1) 1(1) 1(0) 2(1) 1(0) 3(0) 2(0) 3(0) 2(2) 2(3) 2(2) 1(0) 4(1) 1(0) 1(0) 3(2) 3(1) 4(1) 4(0) 3(0) 5(0) 5(0) CS 261 – Data Structures 15 15

AVL Trees (cont.) 4(2) 2(2) 2(3) 1(0) 4(2) 2(1) 1(0) 5(1) 4(1) 4(2) 3(0) 5(1) 1(0) 3(0) 6(0) 3(0) 5(0) 6(0) 4(2) 4(3) 2(1) 6(1) 2(1) 5(2) 1(0) 3(0) 5(0) 7(0) 1(0) 3(0) 6(1) 7(0) CS 261 – Data Structures 16 16

File Handling in C – File Pointers C communicates with files using file pointers. This data type is defined within stdio.h, and written as FILE * Usage: FILE *output_file; 19

Opening a file pointer Your program can open a file using the fopen function, which returns the required file pointer. If the file cannot be opened for any reason then NULL will be returned. Usage: output_file = fopen("filename.txt", "w"); if (!output_file) fprintf(stderr, "Cannot open %s\n", "filename.txt"); 20

Opening a file pointer (contd.) fopen takes two arguments, both are strings: 1. the name of the file to be opened (“filename.txt”). 2. an access character, which is usually one of: “r” : open file for reading “w” : open file for writing (create file if it does not exists) “a” : open file for appending 21

Reading/Writing a file Once the file is opened, you can use the fscanf/fprintf to read/write to a file. int matched = fscanf(output_file,"%c %d %s\n", &cmd, &class, name); fprintf(output_file, "%c \n", cmd); fscanf() returns the number of items in the argument list it can match. Otherwise, EOF is returned. 22

Reading/Writing a file EOF is a character which indicates the end of a file. while (fscanf(output_file, “…", …) != EOF) { … } EOF is returned by read commands of scanf functions when they try to read beyond the end of a file. 23

Closing a file pointer The fclose command is used to disconnect a file pointer from a file. Usage: fclose(output_file); Systems have a limit on the number of files which can be open simultaneously, so it is a good idea to close a file when you have finished using it. 24

Standard I/O in C Standard I/O: input and output channels between a computer program and its environment. Standard input (stdin): usually input from the keyboard. Standard output (stdout): usually output to the text terminal (the screen). Standard error (stderr): to output error messages or diagnostics. Usually output to the screen also. stdin, stdout, stderr are ‘special’ file pointers; don’t open or close them. 25

Standard I/O Functions Output functions: printf() Input functions: scanf() int a; printf("Please enter an integer: "); scanf("%d", &a); printf(“You typed: %d\n“, a); Other input functions: getchar(), fgets(),… 26

fgets() An input function (file get string) char* fgets(char *string, int length, FILE *stream) fgets() reads a string of specific length from a file (or standard input) pointed to by stream. fgets() terminates reading: after a new-line character (\n) is found, OR after it reaches end-of-file (EOF), OR after (length - 1) characters have been read 27