Complex Structures Nested Structures Self referential structures A structure may have Data variables Internal structures/unions Pointer links Function.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Linked Lists.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-5
Trees Types and Operations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
Computer Science C++ High School Level By Guillermo Moreno.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
Department of Computer Science University of Maryland, College Park
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
COMP 110 Introduction to Programming Mr. Joshua Stough.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 Data Structures Lists and Trees. 2 Real-Life Computational Problems All about organizing data! –What shape the data should have to solve your problem.
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.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Trees Dr. Andrew Wallace PhD BEng(hons) EurIng
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Data Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Programming Practice 3 - Dynamic Data Structure
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
 Head pointer  Last node  Build a complete linked list  Node deletion  Node insertion  Helpful hints.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Complex Structures Nested Structures Self referential structures A structure may have Data variables Internal structures/unions Pointer links Function.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
CPS120: Introduction to Computer Science Nell Dale John Lewis Abstract Data Types.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
5.13 Recursion Recursive functions Functions that call themselves
Lectures linked lists Chapter 6 of textbook
12 C Data Structures.
Linked List.
Binary search tree. Removing a node
Problems with dynamic arrays
Problems with Linked List (as we’ve seen so far…)
Lecture 22 Binary Search Trees Chapter 10 of textbook
Sequences 9/18/ :20 PM C201: Linked List.
Tree data structure.
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Linked Lists: Implementation of Queue & Deque
Tree data structure.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Search Sorted Array: Binary Search Linked List: Linear Search
Review & Lab assignments
CS6045: Advanced Algorithms
Chapter 20: Binary Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
BY PROF. IRSHAD AHMAD LONE.
Presentation transcript:

Complex Structures Nested Structures Self referential structures A structure may have Data variables Internal structures/unions Pointer links Function pointers struct course { int course_num; struct student[20]; struct course *next; int (*average)(int []); };

Dynamic Structures Link Lists A type of data structure that have its elements linked together Generic on-demand dynamic structure Easy manipulation, but may not be efficient Trees A type of data structures in which elements are connected in a tree form Complex data structures More efficient, use only if the number of elements are large

Link Lists Single Link Lists head Double Link lists head struct slink { int data; struct slink *next; }; struct dlink { int data; struct dlink *next; struct dlink *prev; }; Sample Usage: Maintain a dynamic structure of sorted numbers using any of the previous structures Important Functions: void Display_list(struct slink * llist) void Append_Node(struct slink* llist, int data) void Delete_Node(struct slink* llist, int data) int Search_Value(struct slink* llist, int data)

Functions: Single link list //Display_list(struct slink * llist) void display_list(struct slink *llist) { while(llist->next != NULL) { printf("%d ", llist->data); llist = llist->next; } printf("%d", llist->data); //the last node } //Append_Node(struct slink* llist, int data) void append_node(struct slink *llist, int data) { while(llist->next != NULL) { llist = llist->next; llist->next = (struct slink )malloc(sizeof(struct slink)); llist->next->data = data; llist->next->next = NULL; } //Build a single linked list #include struct slink { int data; struct slink *next; }; //Insert is the same way void main() { slink * curr, * head; int i; head = NULL; for(i=1;i<=10;i++) { curr = (slink *)malloc(sizeof(slink)); curr->data = i; curr->next = head; head = curr; } curr = head; }

Functions: Single link list //Search_Value(struct slink* llist, int data), return position int search_value(struct slink *llist, int data) { int retval = -1; int i = 1; while(llist->next != NULL) { if(llist->next->data == data) return i; else i++; llist = llist->next; } return retval; } //Delete_Node(struct slink* llist, int data) void delete_node(struct slink *llist, int data) { struct slink *temp; temp = (struct slink *)malloc(sizeof(struct slink)); if(llist->data == data){ /* remove the node */ temp = llist->next; free(llist); llist = temp; } else { //Transverse while(llist->next->data != data) { llist = llist->next; } temp = llist->next->next; free(llist->next); llist->next = temp; }

Functions: Double link list void insert (struct dlink *head, struct dlink *entry) { struct dlink *tmp; for (tmp = head->next; tmp!= head && tmp- >data data; tmp = tmp->next); tmp->prev->next = entry; entry->prev= tmp->prev; tmp->prev = entry; entry->next = tmp; }; void delete (struct dlink *entry) { entry>next->prev = entry->prev; entry>prev->next = entry->next; entry->next = entry->prev = NULL; }; struct dlink * search (struct dlink *head, int val) { struct dlink * tmp; for (tmp = head->next; tmp!= head && tmp->data != val; tmp = tmp->next); return (tmp==head)? NULL : tmp; } int main() { struct dlink num[10] = { … }; struct dlink head={0, NULL, NULL}; for (int i=0; i<10; i++) insert (&head, &num[i]); … return 0; }

Other Link Lists Hash link lists bin[size] Other variants: queues and stacks Queue: a link list grown at one end, shrink at another, i.e. FIFO Stack: a LIFO link list

Trees – binary trees root struct node { int data; struct node *left; struct node *right; }; rightleft leaf height

More Trees Tertiary Trees Each node has three children K-ary Trees Each node has K children B (Balanced)- Trees A tree that maintains a balanced height as it grows/shrinks …

Binary Search Tree void insert (struct node *root, struct node *entry) { /* where to insert is really algorithm dependent. A generic case only: entry is to replace the left node of head */ if (head->left) { entry->left = head->left->left; entry->right = head->left->right; } head->left = entry; }; void delete ( struct node *root, struct node *entry) { /* The key is to find a node that has an entry as a child */ }; struct node * search (struct node *root, int val) { if (!root) return NULL; else if (root->data != val) tmp = search(root->left, val); if (!tmp) tmp = search(root->right, val); return tmp; } int main() { struct node num[10] = { … }; struct node root = {0, NULL, NULL}; for (int i=0; i<10; i++) { insert (&root, &num[i]); } … return 0; }