Grade 12 Computer Studies HG

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees Chapter 8.
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.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Agenda Questions? Problem set 5 Parts A & B Corrected  Good results  2 A’s, 1.
Starting at Binary Trees
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
Programming Practice 3 - Dynamic Data Structure
Data Structures Systems Programming. Systems Programming: Data Structures 2 2 Systems Programming: 2 Data Structures  Queues –Queuing System Models –Queue.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
CS Prelim Review – 10/15/09  First prelim is TOMORROW at 7:30 PM  Review session – Tonight 7:30 PM, Phillips 101  Things you should do:  Review every.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
(c) University of Washington20-1 CSC 143 Java Trees.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Data Structure Interview Question and Answers
12 C Data Structures.
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Stacks and Queues.
Data Structures Interview / VIVA Questions and Answers
Chapter 17 Object-Oriented Data Structures
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 21: Binary Trees.
Lesson Objectives Aims
Find in a linked list? first last 7  4  3  8 NULL
Binary Trees, Binary Search Trees
Introduction to Data Structure
CSC 143 Java Trees.
Data Structures & Algorithms
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

Grade 12 Computer Studies HG Data Structures Grade 12 Computer Studies HG

Data Types An important concept in most programming languages is that of data types Each variable or attribute that is declared needs an associated type A type identifies a certain set or collection of values that a variable can store

Type Examples For instance if we want to store counting numbers we could declare a variable of type int We do this because the int (or integer) data type can store any whole number from -2,147,483,648 to 2,147,483,647 Another example is if we wanted to represent whether someone is married or not we could declare a variable of type boolean We choose boolean because we can store two values with a boolean (true or false) which is all we need in this particular case

Java numeric data types Java has the following data types: byte -128 to 127 short -32768 to 32767 int -2,147,483,648 to 2,147,483,647 long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 float 1.4 x 10-45 to 3.4 x 1038 double 4.9 x 10-324 to 1.7 x 10308 char any unicode character

Strings The type String in Java is not really one of the basic data types The type String is actually a class in Java This class has the necessary attributes and methods to manipulate a String object e.g. substring(), length(), charAt() The designers of Java made an exception in the case of Strings – you don’t have to declare a new string object as you would with other objects. String myStr = “Hello World”; // this is fine String myStr = new String(); // this is also fine

Dynamic Variable Declaration Programming languages that support dynamic variable declaration can have variable declarations anywhere in the program Java supports this type of variable declaration You can have the line int num1; anywhere in your program so long as it is in scope when it is needed In other words the only restriction for declaration is that the variable is visible when it is needed

Static Variable Declaration Believe it or not some languages don’t use dynamic declaration like Java does Instead all variables need to be declared in a predefined section of the code. This section is usually above all other code and all variables need to be declared in that section

The need for multi-valued variables Sometimes we need to store a collection of data of the same type Often having a separate variable for each value is cumbersome For instance if we wanted to store the temperature for everyday in June we don’t want to declare 30 separate variables (temp1, temp2… temp30) The ideal situation would be to have one variable that could store 30 values such that we can choose which value we want to retrieve or store/change Luckily most programming languages provide us with this feature.

Arrays 1 An array is one variable The array can store as many data items as the programmer chooses Each data item must be of the same type (homogenous) Every data item is stored separately in the array We can specify the location of the data item we want to change or look at

Arrays 2 We can represent an array called fiveInt of 5 integers as follows: The data in position 2 is the number 6 and the data in position number 4 is 72 Notice how the numbering of the positions starts at 0 and ends at 4 So even though the array has 5 data items there is no position 5 in the array 23 34 6 56 72 1 2 3 4 position

Arrays 3 We can represent an array called myStrings of 4 Strings as follows: The data in position 0 is the word “dog” and the data in position number 3 is “frog” Position 2 has a null value. Note that this is not the word “null” but a way of indicating that the 2nd position in the array is empty “dog” “cat” null “frog” 1 2 3 position

Arrays in Java 1 Arrays are declared in a similar way to normal integers: int[] myArray = new int[4]; An array of int’s allocates memory holds 4 items called myArray

The int’s to place in each position Arrays in Java 2 The array from the following slide can be filled with values as follows: myArray[3] = 34; myArray[1] = 23; myArray[0] = 87; myArray[2] = 33; The int’s to place in each position Position in the array to place the data

Arrays in Java 3 We can use the values in the arrays as follows: myArray[0] = myArray[2] + 34 * 69; adds the data in position 2 to 34 * 69 and stores the result in position 0 System.out.println(myArray[0]); prints out the value of the integer in position 0 of the array (that is 2433)

Arrays in Java 4 Why would the following lines of code not work with the previously declared array? myArray[4] = 78; myArray[0] = “bob”; myArray = 45; Can you correct the lines?

Static Structures Arrays are static data structures because their size is determined when the code is being written The size of the array cannot be changed during the execution of the program If we declare an array of 12 integers then once the program starts running we cannot store any more than 12 integers in the array Once memory has been allocated for the array it is fixed during execution of the program Static data structures are like hose-pipes. If you get a bigger garden then too bad. Buy a new hose-pipe! Static means motionless or inactive

Dynamic Data Structures These data structures can change size during execution of a program Dynamic data structures grow and shrink as required by the program The size of the structure is determined during run-time Dynamic data structures are like Kreepy-Krauly pipes. Getting a bigger pool? Just add more pipes :) Dynamic means active or changing

Example If we wanted to write a school database and we wanted to declare a structure to store Student objects what size would we make it? Option 1 – 1300? This is fine but what if we get more students next year? Option 2 – 5000? This is way too big. We might never get that many students Option 3 – Dynamic Data Structure. Perfect this would grow and shrink depending on the amount of data items needed

The story so far… We’ve looked at the basic data types in Java int, float, double, byte, long char String Arrays of all of the above We’ve looked at the sizes of data structures Static and Dynamic In the next episode of Data Structures Advanced Data Types (ADT’s)

Advanced Data Types (ADT’s) ADT’s provide us with extra functionality in programming languages. Some ADT’s come standard with Java and others need to be implemented by the programmer. Either way they are fairly complex to code. The ADT’s that we will examine are the most widely used ADT’s in programming.

Lists Lists are everyday occurences. Telephone directories Shopping lists To Do lists Class lists Definition: A list is a sequence of items of the same type Ordered lists have their items in a predefined order. i.e. Alphabetically or numerically The size of a list is how many items it contains at any given time

Linked Lists Linked Lists are a dynamic data structure Each item in a linked list is referred to as a node. Each node consists of two parts: Data (Item) Field – This is the actually data stored in that position of the list (e.g. “dog”, 23) Next Field – Contains a pointer to the next node in the list

Diagram of a node 34 Data Field Next Field

Design of a Linked List Each node is connected to the next node via the next field of each node. The first node is indicated by a head node which contains no data field but only a next field. The last node’s next field always points to null. head 45 67 89 99 null

Operations on a Linked List Create an empty list Check if list is empty Get number of items in the list Copy one list to another list Traverse the list Retrieve an item, given it’s position Search for an item Insert an item Delete an item

Adding We need to be careful when adding items to a linked list because if we lose a next field of any of the nodes we lose all nodes after that node. 5 Steps: Create the new node with a pointer to it. Find the position where the node is to be inserted (call it position x). Create a temporary pointer to the node after the position (i.e. the node in position x + 1). Make the next field of node (x-1) point to the new item. Make the new items next field point to temporary pointer.

Adding: Step 1 head 45 67 89 99 null new 72

Adding: Step 2 position x head 45 67 89 99 null new 72

Adding: Step 3 temp head 45 67 89 99 null new 72

Adding: Step 4 temp head 45 67 89 99 null new 72

Adding: Step 5 temp head 45 67 89 99 null new 72

Deleting Deleting is much easier 2 Steps Chose the item to be deleted (position x) Point the next field of node at (x-1) to the node after position x (x+1)

Deleting: Step 1 position x head 45 67 89 99 null

Deleting: Step 2 head 45 67 89 99 null

Stacks Stacks are data structures that allow data items to be added and removed at the top only. For this reason stacks are called LIFO (Last In First Out) structures because the last item to be added to the stack is the first item to be removed. Just like a stack of plates at a restaurant. Inserting an item at the top of a stack is referred to as pushing. Removing an item off the top is referred to as popping.

Example Consider the following operations: push “R”; push “T”; push “Y”; pop; push “J”; pop; pop; R T R Y T R T R J T R T R R Output: Y J T

Stack Operations Empty the stack Check if the stack is empty Determine size of the stack Look at the top item Push Pop

Uses of the Stack Recursion Postfix Notation

Queues Queues are structures that allow data items to inserted at the rear and removed from the front only. Known as FIFO (First In First Out) structures because the first item to be inserted into the queue will be the first item to be removed. These queues are no different from the queues in everyday life (e.g. shopping centers)

Example Consider the following operations insert “A” insert “B” insert “C” remove an element remove an element A A B Output: A B A B C B C C

Uses Print Queues Real-life Simulation Statistics: Average number of element in queue Average time spent in queue Probability that queue will be longer than x Probability that waiting time will be greater than x Minimum and maximum length of a queue of a period of time Minimum and maximum waiting time over a period of time

Implementation: Stacks and Queues Option 1: Using arrays Programmer has to keep track of where the front (top) and rear of the structure is. Only homogenous items can be stored FIXED SIZE! Queues and stacks grow and shrink during execution Option 2: Using a dynamic structure Linked list!

Trees In a tree each node may have one or more next field. This results in a tree-like structure. N F Q E G T

Naming Conventions Root Node – The first node Child Node – Node beneath another node (each node has only one parent) Parent Node – A node with nodes beneath it Leaf Node – Nodes with no children Siblings – Adjacent nodes

Naming Conventions N Root node and parent of F and Q F Q Sibling of Q Child node of N and Parent of T E G T Leaf node

Binary Trees Binary trees are trees in which each node may have at most two sub trees. These are called left and right sub trees. Each node has three elements: Data (Item Field) Left Pointer Right Pointer The left children of a parent always have smaller values than their parents. The right children of a parent always have larger values that their parents.

Example Insert the following into a binary tree: 50, 34, 12, 78, 36, 30, 67, 44, 52 N, A, Y, T, F, D, S, R, R

Binary Tree Traversal Pre-Order In-Order Post-Order Visit the root Traverse the left sub-tree in pre-order Traverse the right sub-tree in pre-order In-Order Traverse the left sub-tree in in-order Traverse the right sub-tree in in-order Post-Order Traverse the left sub-tree in post-order Traverse the right sub-tree in post-order

Examples Traverse both previous trees in in-order, pre-order and post-order. What do you notice about in-order traversals?

Uses Representing mathematical expressions Compilers Searching Sorting

End