CHARUTAR VIDYA MANDAL S SEMCOM Vallabh Vidyanagar

Similar documents
News. Recap: While Loop Example. Reading. Recap: Do Loop Example. Recap: For Loop Example

Programming in Fortran 90 : 2017/2018

The Greedy Method. Outline and Reading. Change Money Problem. Greedy Algorithms. Applications of the Greedy Strategy. The Greedy Method Technique

An Optimal Algorithm for Prufer Codes *

Lecture 5: Multilayer Perceptrons

Design and Analysis of Algorithms

CMPS 10 Introduction to Computer Science Lecture Notes

Compiler Design. Spring Register Allocation. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Sequential search. Building Java Programs Chapter 13. Sequential search. Sequential search

Machine Learning: Algorithms and Applications

CE 221 Data Structures and Algorithms

ELEC 377 Operating Systems. Week 6 Class 3

Priority queues and heaps Professors Clark F. Olson and Carol Zander

Problem Set 3 Solutions

Parallel matrix-vector multiplication

High level vs Low Level. What is a Computer Program? What does gcc do for you? Program = Instructions + Data. Basic Computer Organization

Sorting Review. Sorting. Comparison Sorting. CSE 680 Prof. Roger Crawfis. Assumptions

CSCI 104 Sorting Algorithms. Mark Redekopp David Kempe


Today s Outline. Sorting: The Big Picture. Why Sort? Selection Sort: Idea. Insertion Sort: Idea. Sorting Chapter 7 in Weiss.

SLAM Summer School 2006 Practical 2: SLAM using Monocular Vision

Esc101 Lecture 1 st April, 2008 Generating Permutation

Course Introduction. Algorithm 8/31/2017. COSC 320 Advanced Data Structures and Algorithms. COSC 320 Advanced Data Structures and Algorithms

CS240: Programming in C. Lecture 12: Polymorphic Sorting

CS221: Algorithms and Data Structures. Priority Queues and Heaps. Alan J. Hu (Borrowing slides from Steve Wolfman)

Analysis of Continuous Beams in General

Virtual Memory. Background. No. 10. Virtual Memory: concept. Logical Memory Space (review) Demand Paging(1) Virtual Memory

Assembler. Building a Modern Computer From First Principles.

Introduction to Programming. Lecture 13: Container data structures. Container data structures. Topics for this lecture. A basic issue with containers

R s s f. m y s. SPH3UW Unit 7.3 Spherical Concave Mirrors Page 1 of 12. Notes

On Some Entertaining Applications of the Concept of Set in Computer Science Course

Parallelism for Nested Loops with Non-uniform and Flow Dependences

ON SOME ENTERTAINING APPLICATIONS OF THE CONCEPT OF SET IN COMPUTER SCIENCE COURSE

Oracle Database: SQL and PL/SQL Fundamentals Certification Course

Array transposition in CUDA shared memory

Notes on Organizing Java Code: Packages, Visibility, and Scope

Intro. Iterators. 1. Access

Support Vector Machines

9. BASIC programming: Control and Repetition

Overview. CSC 2400: Computer Systems. Pointers in C. Pointers - Variables that hold memory addresses - Using pointers to do call-by-reference in C

CS1100 Introduction to Programming

Report on On-line Graph Coloring

CSE 326: Data Structures Quicksort Comparison Sorting Bound

For instance, ; the five basic number-sets are increasingly more n A B & B A A = B (1)

Assignment # 2. Farrukh Jabeen Algorithms 510 Assignment #2 Due Date: June 15, 2009.

Brave New World Pseudocode Reference

Agenda & Reading. Simple If. Decision-Making Statements. COMPSCI 280 S1C Applications Programming. Programming Fundamentals

Module Management Tool in Software Development Organizations

6.854 Advanced Algorithms Petar Maymounkov Problem Set 11 (November 23, 2005) With: Benjamin Rossman, Oren Weimann, and Pouya Kheradpour

Outline. Midterm Review. Declaring Variables. Main Variable Data Types. Symbolic Constants. Arithmetic Operators. Midterm Review March 24, 2014

4/11/17. Agenda. Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. Storage Management.

Load Balancing for Hex-Cell Interconnection Network

Cluster Analysis of Electrical Behavior

Harvard University CS 101 Fall 2005, Shimon Schocken. Assembler. Elements of Computing Systems 1 Assembler (Ch. 6)

USING GRAPHING SKILLS

Assembler. Shimon Schocken. Spring Elements of Computing Systems 1 Assembler (Ch. 6) Compiler. abstract interface.

A Taste of Java and Object-Oriented Programming

5.1 The ISR: Overvieui. chapter

TN348: Openlab Module - Colocalization

A SYSTOLIC APPROACH TO LOOP PARTITIONING AND MAPPING INTO FIXED SIZE DISTRIBUTED MEMORY ARCHITECTURES

Some material adapted from Mohamed Younis, UMBC CMSC 611 Spr 2003 course slides Some material adapted from Hennessy & Patterson / 2003 Elsevier

Summarizing Data using Bottom-k Sketches

Exercises (Part 4) Introduction to R UCLA/CCPR. John Fox, February 2005

3D vector computer graphics

A MOVING MESH APPROACH FOR SIMULATION BUDGET ALLOCATION ON CONTINUOUS DOMAINS

Loop Transformations, Dependences, and Parallelization

An Application of the Dulmage-Mendelsohn Decomposition to Sparse Null Space Bases of Full Row Rank Matrices

A New Approach For the Ranking of Fuzzy Sets With Different Heights

A Binarization Algorithm specialized on Document Images and Photos

A Fast Content-Based Multimedia Retrieval Technique Using Compressed Data

Pose, Posture, Formation and Contortion in Kinematic Systems

Specifications in 2001

CSE 326: Data Structures Quicksort Comparison Sorting Bound

Life Tables (Times) Summary. Sample StatFolio: lifetable times.sgp

Multiblock method for database generation in finite element programs

NUMERICAL SOLVING OPTIMAL CONTROL PROBLEMS BY THE METHOD OF VARIATIONS

Wishing you all a Total Quality New Year!

Mathematics 256 a course in differential equations for engineering students

TECHNIQUE OF FORMATION HOMOGENEOUS SAMPLE SAME OBJECTS. Muradaliyev A.Z.

Efficient Distributed File System (EDFS)

Insertion Sort. Divide and Conquer Sorting. Divide and Conquer. Mergesort. Mergesort Example. Auxiliary Array

Solving two-person zero-sum game by Matlab

An Iterative Solution Approach to Process Plant Layout using Mixed Integer Optimisation

Sorting. Sorting. Why Sort? Consistent Ordering

LOOP ANALYSIS. The second systematic technique to determine all currents and voltages in a circuit

This chapter discusses aspects of heat conduction. The equilibrium heat conduction on a rod. In this chapter, Arrays will be discussed.

AP PHYSICS B 2008 SCORING GUIDELINES

Private Information Retrieval (PIR)

FROG and TURTLE: Visual Bridges Between Files and Object-Oriented Data y

2x x l. Module 3: Element Properties Lecture 4: Lagrange and Serendipity Elements

Review of approximation techniques

A mathematical programming approach to the analysis, design and scheduling of offshore oilfields

Computer models of motion: Iterative calculations

PYTHON IMPLEMENTATION OF VISUAL SECRET SHARING SCHEMES

LLVM passes and Intro to Loop Transformation Frameworks

Kent State University CS 4/ Design and Analysis of Algorithms. Dept. of Math & Computer Science LECT-16. Dynamic Programming

Sorting: The Big Picture. The steps of QuickSort. QuickSort Example. QuickSort Example. QuickSort Example. Recursive Quicksort

Data Representation in Digital Design, a Single Conversion Equation and a Formal Languages Approach

Complex Numbers. Now we also saw that if a and b were both positive then ab = a b. For a second let s forget that restriction and do the following.

PHYSICS-ENHANCED L-SYSTEMS

Transcription:

CHARUTAR VIDYA MANDAL S SEMCOM Vallabh Vdyanagar Faculty Name: Am D. Trved Class: SYBCA Subject: US03CBCA03 (Advanced Data & Fle Structure) *UNIT 1 (ARRAYS AND TREES) **INTRODUCTION TO ARRAYS If we want to store a group of data together n one place then array s sutable data structure. Ths data structure enables us to arrange more than one element that s why t s known as composte data structure. In ths data structure, all the elements are stored n contguous locaton of memory. Fgure-1 shows an array of data stored n a memory block startng at locaton 453. Fgure-1 Defnton: An array s a fnte, ordered and collecton of homogeneous data elements. Array s a fnte because t contans only lmted number of elements. Array s ordered because all the elements are stored one by one n contguous locaton of computer memory n a lnear ordered fashon. Array s collecton of homogeneous (dentcal) elements because all the elements of an array are of the same data type only. An array s known as lnear data structure because all elements of the array are stored n a lnear order. e.g. 1. an array of nteger to store the age of all student n a class 2. an array of strng of characters to store name of all vllagers n vllage Termnology: 1. Sze: Number of elements n an array s called the sze of the array. It s also known as dmenson or length. 2. Type: Type of an array represents the knd of data type for whch t s meant (desgned). E.g. array of nteger, array of character. Page 1 of 18

3. Base: Base of an array s the address of memory locaton where the frst element n the array s located. E.g. 453 s the base address of the array mentoned n Fgure-1. 4. Index: All the elements n an array can be referred by a subscrpt lke A or A[], ths subscrpt s known as ndex. An ndex s always an nteger value. 5. Range of ndces:indces of an array element may change from lower bound (L) to an upper bound (U). These bounds are called the boundares of an array. If the rage of ndex vares from L...U then sze of the array can be calculated as: Sze (A) = U L + 1 **ONE DIMENSIONAL ARRAYS A one dmensonal array s one n whch only one subscrpt specfcaton s needed to specfy a partcular element of the array. Declaraton of One dmensonal array One dmensonal array can be declared as follows: data_type var_name [expresson]; data_type s the type of elements to be stored n the array. var_name specfes the name of array. It may be gven any name lke other smple varables. expresson or subscrpt specfes the number of values to be stored n the array. e.g. nt num[10]; Ths array wll store ten nteger values, whch s specfed by num. It can be vsualzed as below. Intalzng one dmensonal array Fgure-2 Array varables can be ntalzed n declaratons by constant ntalzers. These ntalzng expressons must be constant values. Expresson wth dentfers or functon calls may not be used n the dentfers. The ntalzers are specfed wthn braces and separated by commas. e.g. nt ex[10] = {12, 23, 9, 17, 16, 49}; char word[10] = { h, e, l, l, o, \0 }; Page 2 of 18

Strng ntalzers may be wrtten as strng constants nstead of character constants wthn braces. e.g. char mesg[ ] = Ths s a message ; char name[20] = Hello ; In the case of mesg[ ], enough memory s allocated to accommodate the strng plus a termnatng NULL character and we do not need to specfy the sze of the array. Accessng one dmensonal array elements Indvdual elements of the array can be accessed usng the followng syntax: array_name [ ndex or subscrpt ]; Example: 1. To access fourth element from array we wrte: num[3] The subscrpt for fourth element s 3 because the lower bound of array n C s 0. 2. To assgn a value to second locaton of the array, we wrte: num[1] = 90; 3. To read a partcular value we wrte: scanf ( %d, &num[3] ) ; Ths statement reads a value from the keyboard and assgns t to fourth locaton of the array. **ADDRESS CALCULATION OF ELEMENTS OF ONE DIMENSIONAL ARRAYS Memory allocaton for a one dmensonal array Suppose an array A[100] s to be stored n a memory as n Fgure-3. Let the memory locaton where the frst element can be stored s M. Fgure-3 Physcal Representaton of a one-dmensonal array An array can be wrtten as A[L..U] where L denotes lower bound and U denotes the upper bound for ndex. Page 3 of 18

*TO FIND ADDRESS OF AN ELEMENT IN ONE DIMENSIONAL ARRAY WITH LOWER BOUND = 1 If an element n one dmensonal array occupy 1 word (byte): If each element requres one word (byte) then the locaton for any element say A[ ] n the array can be obtaned as: Address ( A[ ] ) = M + ( 1 ) Note: Lower bound of array s assumed to be 1. M Subscrpt of an element whose address s requred to be calculated If an element n one dmensonal array occupy c word (byte): Address ( A[ ] ) = M + ( 1 ) * c Note: Lower bound of array s assumed to be 1. M c Subscrpt of an element whose address s requred to be calculated Sze of an element OR LOC( A ) = L 0 + ( 1 ) * c Note: Lower bound of array s assumed to be 1. L 0 c Subscrpt of an element whose address s requred to be calculated Sze of an element *TO FIND ADDRESS OF AN ELEMENT IN ONE DIMENSIONAL ARRAY FOR ANY VALUE OF LOWER BOUND) METHOD - 1 If array s stored startng from memory locaton M and for each element t requres w number of words then the address for A[ ] wll be: Address ( A[ ]) = M + ( L ) * w Here Lower bound of array can be any arbtrary value denoted by L. M L w Subscrpt of an element whose address s requred to be calculated Lower bound of array Sze of an element Above formula s known as ndexng formula, whch s used to map the logcal presentaton of an array to physcal presentaton. Page 4 of 18

By knowng the startng address of array M, the locaton of the th calculated nstead of movng towards from M, Fgure-4. element can be Fgure-4 Address mappng between logcal and physcal vew Example Suppose that each element requres 2 word (byte), the base address of the array a[10] s 100 and the lower bound of the array s 1. Fnd out the address of followng elements: 1. Fnd address of a[4]. 2. Fnd address of a[7]. Soluton We are gven that: Base address of the array s 100. So M=100 Lower bound of the array s 1. So L=1. Each element requres 2 word (byte). So w=2. Formula: Address ( A[ ] ) = M + ( L ) * w 1. To fnd address of a[4] Address ( A[4] ) = 100 + (4 1) * 2 = 106 2. To fnd address of a[7] Address ( A[7] ) = 100 + (7 1) * 2 = 112 METHOD - 2 If array s stored startng from memory locaton L 0 and for each element t requres c number of words then the locaton of A wll be: LOC( A ) = L 0 + ( b ) * c Here Lower bound of array can be any arbtrary value denoted by b. L 0 b c Subscrpt of an element whose address s requred to be calculated Lower bound of array Sze of an element Ths address s known as relatve address wth real values. The dstance from the frst element to any partcular element s known as dsplacement (offset) where the value of L 0 s not known. Page 5 of 18

**TWO DIMENSIONAL ARRAY Defnton: Two dmensonal arrays are the collecton of homogeneous elements where elements are ordered n a number of rows and columns. Two dmensonal arrays are also known as matrces. An m X n matrx where m denotes the number of rows and n denotes number of columns s as follows: mxn Fgure-5 The subscrpts of any arbtrary element say Aj represent th row and j th column. Memory representaton of a matrx Lke one dmensonal array, matrces are also stored n contnuous memory locaton. There are two conventons of storng any matrx n memory: 1. Row major order 2. Column major order In row major order, elements of matrx are stored on a row by row bass..e. all the elements of the frst row, then all the elements of second row and so on. In column major order, elements of matrx are stored column by column..e. all the elements of the frst column are stored n ther order of rows, then n second column and so on. Ex. Consder a matrx A of 3 X 4 Fgure-6 Matrx of Fgure-6 can be represented as shown n the Fgure-7. Reference of elements n a matrx Fgure-7 Logcally a matrx appears as two dmensonal but physcally t s stored n a lnear fashon. So n order to map from logcal vew to physcal structure, we need ndexng formula. Obvously, the ndexng formula for dfferent order wll be dfferent. Page 6 of 18

*ROW MAJOR ORDER ADDRESS CALCULATION Assume that the base address s 1 (the frst locaton of the memory). So the address of Aj wll be obtaned as Storng all the elements n frst ( -1 ) th rows + Number of elements n the th row upto the j th column.e. Address ( Aj ) = ( 1 ) * n + j So for the matrx A 3X4, the locaton of A 32 wll be calculated as 10 (see Fgure-7). TO FIND ADDRESS OF AN ELEMENT IN TWO DIMENSIONAL ARRAY WITH LOWER BOUND = 1 and UPPER BOUND = 1 Note: Ths formula assumes that lower bound for (row) and j (column) wll be 1. Address ( Aj ) = M + ( ( 1 ) * n + j 1 ) * w where n s total number of columns M n j w Row subscrpt of an element whose address s requred to be calculated. Number of columns Column subscrpt of an element whose address s requred to be calculated. Sze of an element LOC( A j ) = L 0 + [ ( 1 ) * n + ( j 1 ) ] * c Where n s total number of columns. OR L 0 n j c Row subscrpt of an element whose address s requred to be calculated. Number of columns Column subscrpt of an element whose address s requred to be calculated. Sze of an element TO FIND ADDRESS OF AN ELEMENT IN TWO DIMENSIONAL ARRAY FOR ANY VALUE OF LOWER BOUND and UPPER BOUND) METHOD 1 Address ( Aj ) = M + ( ( L 1 ) * n + j L 2 ) * w where n s no. of columns M L 1 n j L 2 w Row subscrpt of an element whose address s requred to be calculated. Lower bound of (row) Number of columns Column subscrpt of an element whose address s requred to be calculated. Lower bound of j (column). Sze of an element Page 7 of 18

METHOD 2 LOC( A j ) = L 0 + [ ( b 1 ) * ( u 2 b 2 + 1 ) + ( j b 2 ) ] * c L 0 b 1 u 2 b 2 j c Row subscrpt of an element whose address s requred to be calculated. Lower bound of (row) Upper bound of j (column) Lower bound of j (column) Column subscrpt of an element whose address s requred to be calculated. Sze of an element *COLUMN MAJOR ORDER ADDRESS CALCULATION Assume that the base address s 1 (the frst locaton of the memory). So the address of Aj wll be obtaned as Storng all the elements n frst ( j - 1) th columns + Number of elements n the j th column upto the th row.e. Address ( Aj ) = ( j 1 ) * m + So for the matrx A 3X4, the locaton of A 32 wll be calculated as 6 (see Fgure-7). TO FIND ADDRESS OF AN ELEMENT IN TWO DIMENSIONAL ARRAY WITH LOWER BOUND = 1 and UPPER BOUND = 1 Note: Ths formula assumes that lower bound for (row) and j (column) wll be 1. Address ( Aj ) = M + ( ( j 1 ) * m + 1 ) * w where m s total number of rows M j m w Column subscrpt of an element whose address s requred to be calculated. Number of rows Row subscrpt of an element whose address s requred to be calculated. Sze of an element LOC( A j ) = L 0 + [ ( j 1 ) * m + ( 1 ) ] * c Where m s total number of rows. OR L 0 j m c Column subscrpt of an element whose address s requred to be calculated. Number of rows Row subscrpt of an element whose address s requred to be calculated. Sze of an element Page 8 of 18

TO FIND ADDRESS OF AN ELEMENT IN TWO DIMENSIONAL ARRAY FOR ANY VALUE OF LOWER BOUND and UPPER BOUND) METHOD 1 Address ( Aj ) = M + ( ( j L 2 ) * m + L 1 ) * w where m s no. of rows M j L 2 m L 1 w Column subscrpt of an element whose address s requred to be calculated. Lower bound of j (column). Number of rows Row subscrpt of an element whose address s requred to be calculated. Lower bound of (row) Sze of an element METHOD 2 LOC( A j ) = L 0 + [ ( j b 2 ) * ( u 1 b 1 + 1 ) + ( b 1 ) ] * c L 0 j b 2 u 1 b 1 c Column subscrpt of an element whose address s requred to be calculated. Lower bound of j (column) Upper bound of (row) Lower bound of (row) Row subscrpt of an element whose address s requred to be calculated. Sze of an element EXAMPLE Assume that the base address of the two dmensonal array A[3][3] s 100, each element requres 2 byte (word). Fnd the address of the element A[3][2] usng 1. Row major order 2. Column major order Soluton We are gven that: Base address of the array s 100. So M=100 Each element requres 2 word (byte). So w=2. Number of rows are 3. So m=3. Number of columns are 3. So n=3. Lower bound of (row) L 1 = 1 Lower bound of j (column) L 2 = 1 Page 9 of 18

1. Row major To fnd address of the A[3][2] element of the array. So = 3 and j = 2 Address ( A[3][2] ) = M + ( ( L 1 ) * n + j L 2 ) * w = 100 + ( ( 3 1 ) * 3 + 2 1 ) * 2 = 114 2. Column major To fnd address of the A[3][2] element of the array. So = 3 and j = 2 Address ( A[3][2] ) = M + ( ( j L 2 ) * m + L 1 ) * w = 100 + ( ( 2 1 ) * 3 + 3 1 ) * 2 = 110 SPARSE MATRICES A useful applcaton of lnear lst n the representaton of matrces that contan a preponderance (majorty / large number) of 0 elements. These matrces are called sparse matrces..e. n such types of matrces the total number of zero elements s hgher than the total number of none zero elements. They are commonly used n scentfc applcaton and contan 100 or even 1000 of rows and columns. The presentaton of such large matrces s wasteful of storage and operatons wth these matrces are neffcent f the sequental allocaton methods are used for ther storage. Of the 42 elements n ths 6 X 7 matrx, only 10 are non zero. They are: A[1,3] = 6, A[1,5] = 9, a[2,1] = 2, A[2,4] = 7, A[2,5] = 8, a[2,7] = 4, A[3,1] = 10, A[4,3] = 12, A[6,4] = 3, A[6,7] = 5 Fgure-8 One of the basc methods for storng such a sparse matrces s to store non zero elements n a one dmensonal array and to dentfy each array element wth row and column ndces, as shown n Fgure-9 (a). Fgure-9 (a) Fgure-9(b) Sequental representaton of Sparse Matrces Page 10 of 18

The th element of vector A s the matrx element wth row and column ndces Row() and Column(). A more effcent representaton n terms of storage requrements and access tme to the rows of the matrx s shown n Fgure-9 (b). For large matrces the conservaton of storage s very sgnfcant. The sequental allocaton scheme for sparse matrces s also of value n that matrx operatons can be executed faster than possble wth a conventonal two-dmensonal array representaton, partcularly when matrces are large. APPLICATION OF ARRAYS 1. Arrays are used to mplement mathematcal vectors and matrces, as well as other knds of rectangular tables. Many databases, small and large, consst of (or nclude) one-dmensonal arrays whose elements are records. 2. Arrays are used to mplement other data structures, such as heaps, hash tables, deques, queues, stacks, strngs, and VLsts. 3. One or more large arrays are sometmes used to emulate n-program dynamc memory allocaton, partcularly memory pool allocaton. Hstorcally, ths has sometmes been the only way to allocate "dynamc memory" portably. 4. Arrays can be used to determne partal or complete control flow n programs, as a compact alternatve to (otherwse repettve), multple IF statements. They are known n ths context as control tables and are used n conjuncton wth a purpose bult nterpreter whose control flow s altered accordng to values contaned n the array. The array may contan subroutneponters (or relatve subroutne numbers that can be acted upon by SWITCH statements) - that drect the path of the executon. 5. There are wde applcatons of arrays n computaton. That s why almost every programmng language ncludes ths data type as a bult n data type. 6. Suppose you want to store records of all students n a class. The record structure s gven by STUDENT Roll No. Mark1 Mark2 Mark3 Total Grade Alpha Numerc Numerc Numerc Numerc Numerc Character Fgure-10 If sequental storage of record s not objecton then we can store the records by mantanng 6 array whose sze s specfed by the total number of students n the class as Fgure-10. 7. Arrays can be used to represent polynomals so that mathematcal operatons can be performed n an effcent manner. Page 11 of 18

Arrays used to store Polynomals A two-dmensonal array can be used to represent a polynomal n two varables. In a programmng language whch allows subscrpts to start wth zero (lke C), the coeffcent of the term x y j would be stored n the element dentfed by row I and column j of the array. If we restrct the sze of an array to maxmum 5 rows and 5 columns then the powers of x and y must not exceed a value of 4 n any term of the polynomal. The array representng polynomal 2x 2 + 5xy + y 2 s gven as 0 0 1 0 0 0 5 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 LIMITATIONS OF LINEAR ARRAYS 1. Number of elements n array s necessary to known n advance. 2. Arrays are statc structures..e. memory s allocated at complaton tme. So array s memory can not be reduced or expanded. 3. An nserton / Deleton of an element from array s tme consumng and requre large amount of data movement. INTRODUCTION TO TREES Arrays, Stacks, Queues and Lnked lsts are known as lnear data structures because elements are arranged n a lnear fashon. Another very useful data structure s tree, where elements appear n a non lnear fashon. Tree: A tree s a non lnear data structure n whch tems are arranged n a sorted sequence. It s used to represent herarchcal relatonshp exstng amongst several data tems. A tree s a fnte set of one or more nodes such that 1. There s a specally desgnated node called the root, 2. Remanng nodes are parttoned nto n (n >0) dsjont sets T1, T2, T3,.,Tn where each T (=1,2,.,n) s a tree; T1, T2, T3,.,Tn are called sub trees of the root. Fgure-11 Page 12 of 18

BASIC TERMINOLOGIES 1. Root It s specally desgned data tem n a tree. It s the frst n the herarchcal arrangement of data tems. OR Ths s a specally desgnated node whch has no parent. In above tree, A s root. 2. Node Each data tem n a tree s called node. It s the basc structure n a tree. It stores the actual data and lnks to another node. Fgure-12 (a) represents the structure of the node. 3. Degree of Node Fgure-12(a) It s the number of sub trees of a node n a gven tree. 4. Parent Parent of the node s the mmedate predecessor of a node. Here, X s the parent of Y and Z as shown n Fgure-12(b) 5. Leaf or Termnal node Fgure-12(b) The node whch s at the end and whch does not have any chld s called leaf node. In fgure 12(c) H, I, K, L and M are the leaf nodes. Leaf node s also known as termnal node whch contans degree zero. Page 13 of 18

Fgure-12(c) 6. Level Level s a rank of the herarchy. The entre tree structure s leveled n such a way that root node s always at level 0. Then ts mmedate chldren are at level 1 and ther mmedate chldren are at level 2 and so on up to the termnal nodes. If a node s at level l (el) then ts chld s at l +1 and parent s at level l 1. Ths s true for all nodes except the root node. 7. Sblngs The nodes whch have the same parent are called sblngs. They are also called brothers. In above tree: E and F are sblngs of parent node B. 8. Heght The maxmum number of nodes that s possble n a path startng from root node to a leaf node s called heght of a tree. It s the maxmum level of any node n a gven tree. The term heght s also used to denote the depth. 9. Branch Branch means a lnk between parent and ts chld. 10. Edge It s connectng lne of two nodes. The lne drawn from one node to another node s called and edge. 11. Path It s a sequence of consecutve edges from the source node to destnaton node. In above tree, the path between A and J s gven by the node pars, (A,B), (B,F) and (F,J). Page 14 of 18

12. Forest It s a set of dsjont trees. In a gven tree, f you remove ts root node then t becomes a forest. In above tree, there s forest wth three trees. 13. Drected Tree It s an acyclc dagraph whch has one node called ts root whose ndegree s 0 whle other nodes have ndegree 1. Applcatons of tree data structure 1. Trees can be used to store nformaton that naturally forms a herarchy. For example, the fle system on a computer: Fle system college / \ bba bca / \ course class / \ fy sy ty 2. Trees can hold objects that are sorted by ther keys. Fgure-13 The nodes are ordered so that all keys n a node s left sub tree are less than the key of the object at the node, and all keys n a node s rght sub tree are greater than the key of the object at the node. Here s an example of a tree of records, where each record s stored wth ts nteger key n a tree node: Fgure-14 3. Trees can hold objects that are located by keys that are sequences. For example, we mght have some books wth followng catalog numbers: QA76 book1 QA7 book2 Q17 book3 B1 book4 Z4 book5 Fgure-15 The books s keys are sequences and the sequences label the branches of a tree that hold the books: Page 15 of 18

* +---------------+---------------+ B Q Z * * * 1 1 / \ A 4 book4 * * book5 7 7 book3 book2 6 book1 Fgure-16 Books can be stored at nodes or leaves, and not all nodes hold a book (e.g Q1). Ths tree s called a spellng tree and t has the advantage that the nserton and retreval tme of an object s related only to the length of the key. 4. Tree can represent a structured object, such as a house that must be explored by a robot or human player n an adventure game: house's entrance ---- upper hallway ---- bedroom --- closet ---... +-----prvate bath---... +---study---... lower hallway--- ktchen ---... + --- lounge ---... Fgure-17 5. Trees are used to represent phrase structure of sentences, whch s crucal to language processng programs. Here s the phrase structure tree (parse tree) for the java statements: nt x; x = 3 + y; STATEMENT SEQUENCE / \ DECLARATION ASSIGNMENT / \ / \ TYPE VARIABLE VARIABLE EXPRESSION / \ nt x x NUMERAL + VARIABLE 3 y Fgure-18 6. An operatng system mantans a dsk s fle system as a tree, where fle folders act as tree nodes. Fgure-19 Page 16 of 18

7. If we organze keys n form of a tree (wth some orderng e.g., BST.e. Bnary Search Tree), we can search for a gven key n moderate tme (qucker than Lnked Lst and slower than arrays). 8. We can nsert / delete keys n moderate tme (qucker than Arrays and slower than Unordered Lnked Lsts). 9. Lke Lnked Lsts and unlke Arrays, Ponter mplementaton of trees doesn t have an upper lmt on number of nodes as nodes are lnked usng ponters. 10. As per Wkpeda, followng are the common uses of tree. Exercse 1. Manpulate herarchcal data. 2. Make nformaton easy to search (see tree traversal). 3. Manpulate sorted lsts of data. 4. As a workflow for compostng dgtal mages for vsual effects. 5. Router algorthms 1. Assume that 4 byte of storage are requred to hold each element of a one dmensonal array A[10]. Further assume that the storage for the array begns at 501 n memory. Gve the actual address of the elements A[3] and A[9]. 2. Assume that 2 byte of storage are requred to hold each element of a one dmensonal array A[10]. Further assume that the storage for the array begns at 101 n memory. Subscrpt lmts of an array s -5 4. Calculate the actual address of the elements A[-3], A[-1] and A[4]. 3. Suppose an array A[-15 64] s stored n a memory whose startng address s 459. Assume that the word sze for each element s 2. What s the locaton for A[50]? 4. Assume that 8 byte of storage are requred to hold each element of a two dmensonal array A[20][20]. Further assume that the storage for the array begns at 1000 n memory. Gve the actual address of the elements A[6][4] and A[15][9] when. The array s stored n Row major. The array s stored n Column major 5. Fnd address of an element a[1,3] and a[4,5] where. Storage representaton of array a s row major. Subscrpt lmts of an array : 0 5, 1 j 6. Storage for array begns at 100 bytes n memory v. 2 bytes of storage s requred to hold each element of array 6. A two dmensonal array defnes as a[4 7, -1 3] requres 2 bytes of storage space per each element. If the array s stored n row major form then calculate the address of element at locaton a[6,2]. Base address s 100. 7. Each element of an array a[-20 20, 10 35] requres 1 byte of storage. If the array s column major mplemented, and the begnnng of the array s at locaton 500 (base address) then determne the address of element a[0,30]. Page 17 of 18

8. Gve Sequental and effcent representaton for followng Sparse matrx B. Dsclamer 0 0 0 4 0 0 1 0 9 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 6 7 8 0 0 0 0 20 0 0 0 0 0 2 0 0 0 0 0 1 0 0 0 5 7 0 0 0 13 0 0 The study materal s compled by Am D. Trved. The basc objectve of ths materal s to supplement teachng and dscusson n the classroom n the subject. Students are requred to go for extra readng n the subject through lbrary work. Page 18 of 18