Announcements. Linked Data Structures

Similar documents
Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Ordered Lists and Binary Trees

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Data Structures and Algorithms

DATA STRUCTURES USING C

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Unordered Linked Lists

Analysis of a Search Algorithm

Lecture 12 Doubly Linked Lists (with Recursion)

Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap

Lecture Notes on Binary Search Trees

Data Structures Fibonacci Heaps, Amortized Analysis

Binary Search Trees (BST)

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Data Structures and Algorithms Written Examination

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

CSE 211: Data Structures Lecture Notes VII

Binary Trees. Wellesley College CS230 Lecture 17 Thursday, April 5 Handout #28. PS4 due 1:30pm Tuesday, April

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Lecture Notes on Binary Search Trees

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

The ADT Binary Search Tree

Lecture 11 Array of Linked Lists

Alex. Adam Agnes Allen Arthur

Data Structures Using C++ 2E. Chapter 5 Linked Lists

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Data Structures and Data Manipulation

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Introduction to Object-Oriented Programming

Data Structure [Question Bank]

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

LINKED DATA STRUCTURES

Common Data Structures

Converting a Number from Decimal to Binary

Algorithms and Data Structures

ADTs,, Arrays, Linked Lists

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Binary Search Trees 3/20/14

Useful Java Collections

Data Structures and Algorithms Lists

Why Use Binary Trees?

Data Structure with C

Binary Search Trees CMPSC 122

Keys and records. Binary Search Trees. Data structures for storing data. Example. Motivation. Binary Search Trees

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

PES Institute of Technology-BSC QUESTION BANK

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Linked List as an ADT (cont d.)

Binary Search Trees. Ric Glassey

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

TREE BASIC TERMINOLOGIES

Sample Questions Csci 1112 A. Bellaachia

Chapter 14 The Binary Search Tree

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Circular Linked List. Algorithms and Data Structures

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

How To Create A Tree From A Tree In Runtime (For A Tree)

Data Structures Using C++

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Symbol Tables. Introduction

Introduction to Data Structures and Algorithms

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

10CS35: Data Structures Using C

Data Structures CSC212 (1) Dr Muhammad Hussain Lecture - Binary Search Tree ADT

Basic Data Structures and Algorithms

Binary Search Tree Intro to Algorithms Recitation 03 February 9, 2011

CSE 326: Data Structures B-Trees and B+ Trees

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Queue Implementations

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Efficient representation of integer sets

TESTING WITH JUNIT. Lab 3 : Testing

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Full and Complete Binary Trees

The Relational Data Model

Project 4 DB A Simple database program

Linked Lists Linked Lists, Queues, and Stacks

Persistent Binary Search Trees

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Algorithms and Data Structures Written Exam Proposed SOLUTION

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Analysis of Algorithms I: Binary Search Trees

Section IV.1: Recursive Algorithms and Recursion Trees

Big Data and Scripting. Part 4: Memory Hierarchies

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

From Last Time: Remove (Delete) Operation

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Transcription:

Exam #1 Announcements Thursday, March 8, 6:00 :30pm in ARM 016 Reading Chapter 1, 10. 1 Linked Data Structures Like classes with refs to same class in Java Have pointers to a struct of the same type Example Declaration: typedef struct NODE { struct NODE *next; int value; Node; Node * root; next and root are pointers to Nodes root 5 10 15 1 1

Finding a value in a Linked Structure Node *find(node *current, int val) { while (current && current->value!= val) { return current; int IsIn(Node *current, int val) { while ((current!= ) && ((current->value)!= val)) { if (current) { return 1; else { 3 head Tracing Through Insert X X 5 10 15 Insert Insert 4

Inserting Into a Linked List int insert(node **head, int new_value) { Node *current = *head; Node *pred = ; Node *newitem; while (current && current->value < new_value) { newitem = (Node *) malloc(sizeof(node)); if (!newitem) return -1; newitem->value = new_value; newitem->next = current; if (!pred) { *head = newitem; else { pred->next = newitem; 5 head Tracing Through Delete 5 10 15 Delete Delete 6 3 3

Deleting From A Single Linked List int delete(node **head, int new_value) { Node *pred = ; Node *current = *head; while (current && (current->value!= new_value)) { if (!current) { return -1; /* not found */ if (pred) { pred->next = current->next; else { *head = current->next; /* deleted first item */ free (current); Doubled Linked Lists Each nodes contains a value a pointer the next and previous element Typical Declaration: typedef struct NODE { struct NODE *next; struct NODE *prev; int value; Node; root 5 10 15 8 4 4

Insert into a doubly Linked list Think about 4 cases: Middle of the list Update previous element's next pointer new node s previous pointer set to this node Update next element s previous pointer new node s next point set to that node End of the list Update previous element's next pointer new node s previous pointer set to this node new node s next pointer set to Start of the list Update head of list Update old head of list's previous element new node s next pointer set to this node new node s previous pointer set to An initially empty list Update head of the list new node s next and previous pointer both 9 Inserting Into A Doubly Linked List int insert(node **head, int new_value) { Node *pred =, *newitem; Node *current = *head; while (current && current->value < new_value) { newitem = (Node *) malloc(sizeof(node)); if (!newitem) return -1; newitem->value = new_value; newitem->next = current; newitem->prev = pred; if (!pred) { *head = newitem; else { pred->next = newitem; if (current) { current->prev = newitem; 10 5 5

Deleting from a Doubly Linked List int delete(node **head, int new_value) { Node *pred = ; Node *current = *head; while (current && (current->value!= new_value)) { if (!current) return -1; /* not found */ if (pred) { pred->next = current->next; else { *head = current->next; /* deleted first item */ if (current->next) { current->next->prev = pred; free (current); 11 Each node Binary Trees contains a value a pointer a left child and a right child Typical Declaration: typedef struct NODE { struct NODE *left; struct NODE *right; int value; Node; 5 100 509 10 83 1 6 6

Lookup In A Binary Search Tree If the element is less than the current node Look in the left child tree If the element is greater than current node Look in the right child tree Example: int Lookup(Node *root, int value) { if (!root) return -1; if (root->value == value) { return 1; else if (root->value > value) { return Lookup(root->left, value); else { return Lookup(root->right, value); 13 Insert Into a Binary SearchTree int insert(node **root, int value) { Node *new; if (!(*root)) { new = (Node *) malloc(sizefof(node)); if (!new) return -1; *root = new; new->left = new->right = ; new->value = value; if ((*root)->value > value) { return insert(&((*root)->left), value); else { return insert(&((*root)->right), value); 14

In a Graph Graphs - Directed there can be an arbitrary number of nodes each node can have an arbitrary number of out arcs Declarations typedef struct ARC { struct NODE *nodeptr; struct ARC *next; Arc; typedef struct NODE { Arc *outarcs; int value; Node; 15 Graphs key 5 Node Null 14 Null Arc Null 16 8 8

Adding Node To a Graph Node *AddNode(int value) { Node *new; new = (Node *) malloc(sizeof(node)); new->value = value; new->outarcs = ; return new; You need to store this node into some type of structure so you don t lose it linked list of nodes array of nodes tree of nodes etc. 1 Adding Arcs To a Graph int AddArc(Node *from, Node *to) { Arc *new, *pred, *current; pred = ; for (current=from->outarcs; current; current=current->next) { if (current->nodeptr == to) return 1; /* already defined this arc */ new = (Arc *) malloc(sizeof(arc)); if (!new) return -1; new->nodeptr = to; new->next = ; if (!pred) { from->outarcs = new; /* first out arc for this node */ else { pred->next = new; 18 9 9