Programming and Data Structures

Slides:



Advertisements
Similar presentations
Structures Spring 2013Programming and Data Structure1.
Advertisements

Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees Chapter 6.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Binary Tree.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
1 Structures. 2 What is a Structure? Used for handling a group of logically related data items  Examples: Student name, roll number, and marks Real part.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
CSE 373 Data Structures Lecture 7
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Non Linear Data Structure
CS 201 Data Structures and Algorithms
CC 215 Data Structures Trees
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Linked List :: Basic Concepts
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Week 6 - Wednesday CS221.
Lecture Trees Chapter 9 of textbook 1. Concepts of trees
Trees.
Lecture 22 Binary Search Trees Chapter 10 of textbook
CMSC 341 Introduction to Trees.
Trees.
Data Structures & Algorithm Design
CSE 373 Data Structures Lecture 7
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
CSC 172– Data Structures and Algorithms
ITEC 2620M Introduction to Data Structures
i206: Lecture 13: Recursion, continued Trees
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Trees and Binary Trees.
Structures.
Find in a linked list? first last 7  4  3  8 NULL
Trees CSE 373 Data Structures.
Trees.
Trees Lecture 9 CS2110 – Fall 2009.
CMSC 202 Trees.
Binary Trees, Binary Search Trees
Trees.
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
Chapter 20: Binary Trees.
Important Problem Types and Fundamental Data Structures
Binary Trees.
Trees.
Trees CSE 373 Data Structures.
Binary Search Trees CS 580U Fall 17.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Trees Lecture 10 CS2110 – Spring 2013.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Programming and Data Structures Debasis Samanta Computer Science & Engineering Indian Institute of Technology Kharagpur Spring-2017

Lecture #09 Structures in C CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Today’s discussion… Built-in data types User-defined data types Defining a Structure in C Operations with ADTs Some examples Complex numbers Binary Tree Linked List Other Examples CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Built-in Data Type CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Built-in Data Type There are five basic data types associated with variables int - integer: a whole number float - floating point value: a number with a fractional part double - a double-precision floating point value char - a single character void - valueless special purpose type CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Example: Built-in Data Type in C #include <stdio.h> int main() { int a = 4000; // positive integer data type float b = 5.2324; // float data type char c = 'Z'; // char data type long d = 41657; // long positive integer data type long e = -21556; // long -ve integer data type int f = -185; // -ve integer data type short g = 130; // short +ve integer data type short h = -130; // short -ve integer data type double i = 4.1234567890; // double float data type float j = -3.55; // float data type } CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Advantage of Built-in Data Type Simple Really simple! Only FIVE types of data!! Easy to handle Allocation of memory and operations are already defined Built-in support by programming language The C library are there to deal with them CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Disadvantage of Built-in Data Type There is a need for storing and handling variety of data types Image, text, video, etc. Limited range Waste of memory No flexibility Error prone programming CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

User-Defined Data Type CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

What is User Defined Data Type? User can define their own data type Also, called custom data type, abstract data type (ADT), etc. Examples Complex number: z = x + i y Matrices: 𝐴 𝑚×𝑛 Date: dd/mm/yy ... And many more CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Why Abstract? Because details of the implementation are hidden. When you do some operation on the list, say insert an element, you just call a function. Details of how the list is implemented or how the insert function is written is no longer required. CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Why User-Defined Data Types? It is always convenient for handling a group of logically related data items. Examples: Student’s record name, roll number, and marks. Elements in a set Used in relational algebra, database, etc. A non non-trivial data structure becomes a trivial. Helps in organizing complex data in a more meaningful way. CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

How User-Defined Data Types? All logically related data can be grouped into a form called structure Each member into the group may be a built-in data type or any other user defined data type No recursion, that is, a structure cannot include itself Example Date : {int dd, int mm, int yy} CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Structure Definition in C CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Defining a Structure The composition of a structure may be defined as struct is the required keyword tag is the name of the structure member 1,member 2,..are individual member declarations struct tag { member 1; member 2; : member m; }; CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Defining a Structure Example Defining structure variables A new data-type struct student { char name[30]; int roll_number; int total_marks; char dob[10]; }; struct student s1, sList[100]; CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Point to be Noted The individual members can be ordinary variables, pointers, arrays, or other structures. The member names within a particular structure must be distinct from one another. A member name can be the same as the name of a variable defined outside of the structure. Once a structure has been defined, the individual structure-type variables can only be declared then. Each member in a structure can be accessed with (.) operator called scope resolution operator struct student s1, sList[100]; s1.name; sList[5].roll_number; CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Example: Complex Number Addition Scope restricted within main() #include <stdio.h> main() { struct complex float real; float complex; } a, b, c; scanf (“%f %f”, &a.real, &a.complex); scanf (“%f %f”, &b.real, &b.complex); c.real = a.real + b.real; c.complex = a.complex + b.complex; } Structure definition and Variable Declaration Reading a member variable Accessing members CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Operations with ADTs CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

ADT: Complex Number add sub multiplication Complex Number division read print CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Example: Complex Numbers struct typeX { float re; float im; }; typedef struct typeX complex; complex *add (complex a, complex b); complex *sub (complex a, complex b); complex *mul (complex a, complex b); complex *div (complex a, complex b); complex *read(); void print (complex a); Structure definition Function prototypes CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

ADT: Set union intersection Set minus insert delete size CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Example: Set Manipulation struct nodeS { int element; struct nodeS *next; }; typedef struct nodeS set; set *union (set a, set b); set *intersect (set a, set b); set *minus (set a, set b); void insert (set a, int x); void delete (set a, int x); int size (set a); Structure definition Function prototypes CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Binary Tree CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Definition A data structure in which a record is linked to two successor records, usually referred to as the left branch when greater and the right when less than the previous record. CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Tree Terminology A binary tree is made of nodes, where each node contains a "left" reference, a "right" reference, and a data element. The topmost node in the tree is called the root Every node (excluding a root) in a tree is connected by a directed edge from exactly one other node which is called a parent On the other hand, each node can be connected to arbitrary number of nodes, called children Nodes with no children are called leaves or external nodes Nodes which are not leaves are called internal nodes Nodes with the same parent are called sibling CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

More Tree Terminology The depth of a node is the number of edges from the root to the node. The height of a node is the number of edges from the node to the deepest leaf. The height of a tree is a height of the root. CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Full & Complete Binary Tree A full binary tree is a binary tree in which each node has exactly zero or two children. A complete binary tree is a binary tree, which is completely filled, with the possible exception of the bottom level, which is filled from left to right. CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Advantages of Trees Trees are so useful and frequently used, because they have some very serious advantages:- Trees reflect structural relationships in the data Trees are used to represent hierarchies Trees provide an efficient insertion and searching Trees are very flexible data, allowing to move subtrees around with minimum effort CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Declaring a Binary Tree in C struct node { int value; struct node * left; struct node * right; } CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Traversals A traversal is a process that visits all the nodes in the tree. Since a tree is a nonlinear data structure, there is no unique traversal. We will consider several traversal algorithms with we group in the following two kinds:- depth-first traversal breadth-first traversal There are three different types of depth-first traversals:- PreOrder traversal - visit the parent first and then left and right children; InOrder traversal - visit the left child, then the parent and the right child; PostOrder traversal - visit left child, then the right child and then the parent; CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Example: Traversals As an example consider the following tree and its four traversals: PreOrder - 8, 5, 9, 7, 1, 12, 2, 4, 11, 3 InOrder - 9, 5, 1, 7, 2, 12, 8, 4, 3, 11 PostOrder - 9, 1, 2, 12, 7, 5, 3, 11, 4, 8 LevelOrder - 8, 5, 4, 9, 7, 11, 1, 12, 3, 2 CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Linked List CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Definition A linked list is a sequence of data structures, which are connected together via links. Linked list is a sequence of links which contains items. Each link contains a connection to another link. Important terms to understand the concept of linked list. Data − Each link of a linked list can store a data called an element Next − Each link of a linked list contains a link to the next link called Next List − A list contains the connection link to the first link called Head CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Linked List Representation Linked list can be visualized as a chain of nodes, where every node points to the next node Important points to be considered: Linked list contains a link element called Head Each link carries a data field(s) and a link field called next Each link is linked with its next link using its next link Last link carries a link as null to mark the end of the list CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Defining A Linked List struct node { int data; struct node *next; }; CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Why Linked List? Arrays can be used to store linear data of similar types, but arrays have following limitations. The size of the arrays is fixed Inserting a new element in an array of elements is expensive Advantages over arrays Dynamic size Ease of insertion/deletion Drawbacks of linked list Random access is not allowed. We cannot do binary search with linked lists. Extra memory space for a pointer is required with each element of the list. CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Other Examples CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Example: Comparing Dates #include <stdio.h> struct date { int dd, mm, yy; } ; int date_cmp(struct date d1, struct date d2); void date_print(struct date d); int main(){ struct date d1 = {7, 3, 2015}; struct date d2 = {24, 10, 2015}; int cmp = date_cmp(d1, d2); date_print(d1); if (cmp == 0) printf(" is equal to"); else if (cmp > 0) printf(" is greater, i.e., later than "); else printf(" is smaller, i.e., earlier than"); date_print(d2); return 0; } CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Comparing Dates OUTPUT /* compare given dates d1 and d2 */ int date_cmp(struct date d1, struct date d2) { if (d1.dd == d2.dd && d1.mm == d2.mm && d1.yy == d2.yy) return 0; else if (d1.yy > d2.yy || d1.yy == d2.yy && d1.mm > d2.mm || d1.yy == d2.yy && d1.mm == d2.mm && d1.dd > d2.dd) return 1; else return -1; } /* print a given date */ void date_print(struct date d) { printf("%d/%d/%d", d.dd, d.mm, d.yy); OUTPUT 7/3/2015 is smaller, i.e., earlier than 24/10/2015 CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Example: Add Two Complex Numbers #include <stdio.h> typedef struct complex { float real; float imag; } complex; int main() complex n1, n2, temp; printf("For 1st complex number \n"); printf(" Enter re & im part respectively:\n"); scanf("%f %f", &n1.real, &n1.imag); printf("\nFor 2nd complex number \n"); printf("Enter re & im part respectively:\n"); scanf("%f %f", &n2.real, &n2.imag); temp = add(n1, n2); printf("Sum = %.1f + %.1fi", temp.real, temp.imag); return 0; } CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Add Two Complex Numbers complex add(complex n1, complex n2) { complex temp; temp.real = n1.real + n2.real; temp.imag = n1.imag + n2.imag; return(temp); } OUTPUT For 1st complex number Enter re & im part respectively: 2.3 4.5 For 2nd complex number Enter re & im part respectively: 3.4 5 Sum = 5.7 + 9.5i CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Any question? You may post your question(s) at the “Discussion Forum” maintained in the course Web page. CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Problems to ponder… Will be posted shortly… CS 11001 : Programming and Data Structures Lecture #??: © DSamanta

Problems for practice… Will be posted shortly… CS 11001 : Programming and Data Structures Lecture #??: © DSamanta