Chapter 10: Data Structures II

Slides:



Advertisements
Similar presentations
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
Advertisements

1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Chapter 9: Data Structures I
Chapter 12 Binary Search Trees
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
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.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 17: Binary Search Trees; Heaps.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
Data Structures – Binary Tree
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
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 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.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
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.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 12: Multi-way Search Trees Java Software Structures: Designing.
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
CSE 373 Data Structures Lecture 7
Trees Chapter 15.
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Section 8.1 Trees.
Trees.
Lecture 18. Basics and types of Trees
Tonga Institute of Higher Education
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Chapter 10: Non-linear Data Structures
Chapter 21: Binary Trees.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Chapter 10: Data Structures II Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java Software Solutions is published by Addison-Wesley Presentation slides are copyright 2002 by John Lewis, William Loftus, and Cara Cocking. All rights reserved. Instructors using the textbook may use and modify these slides for pedagogical purposes.

Data Structures II Now we learn a few more data structures Chapter 10 focuses on: Sets and maps Trees and binary search trees Heaps Handling collisions in hashtables

Sets and Maps A set is a collection of elements with no duplicates Example: {5, 7, 8} is a set, {3, 3, 4} is not A map matches, or maps, keys to value Example: a dictionary is a map that maps words to definitions The keys in a map form a set (and therefore must be unique) The Set and Map interfaces in Java represent sets and maps The classes HashSet, TreeSet, HashMap, and TreeMap are implementations

Trees and Binary Trees A tree is a non-linear data structure that consists of zero or more nodes that form a hierarchy One node is the root node In a binary tree, each node has at most two children, the right child and left child Nodes without children are called leaf nodes See Figure 10.4 A subtree is formed by each child, consisting of the child and its descendents

Binary Tree Implementation A binary tree is a dynamic data structure Each node contains data and has references to the left and right children Basic structure: class TreeNode { Object value; TreeNode left; TreeNode right; } See TreeNode.java (page 549)

Tree Traversal There are 3 ways to traverse a tree, that is, to visit every node: Preorder traversal: visit the current node, then traverse its left subtree, then its right subtree Postorder traversal: traverse the left subtree, then the right subtree, then visit the current node Inorder traversal: traverse the left subtree, then visit the current node, then traverse its right subtree Tree traversal is a recursive process

Binary Search Trees A binary search tree can be used for storing sorted data For any node N, every node in N’s left subtree must be less than N, and every node in N’s right subtree must be greater than or equal to N See Figure 10.9 An inorder traversal of a binary tree visits the nodes in sorted order See SortGrades.java (page 554) See BSTree.java (page 555) See BSTNode.java (page 557)

Binary Search Trees Searching for an element is a recursive process If the desired element is less than the current node, try the left child next If the desired element is greater than the current node, try the right child next To insert an element, perform a search to find the proper spot To delete an element, replace it with its inorder successor

Heaps A heap is a complete binary tree in which each parent has a value less than both its children A complete binary tree has the maximum number of nodes on every level, except perhaps the bottom, and all the nodes are in the leftmost positions on the bottom See Figures 10.15 and 10.16 The smallest node in a heap is always at the root

Heaps To add a node to a heap, add it in the position that keeps the tree complete, then bubble it up if necessary by swapping with its parent until it is not less than its parent See Figures 10.17 and 10.18 To delete a node from a heap, replace it with the last node on the bottom level and bubble that node up or down as necessary See Figures 10.19 and 10.20

Heapsort A heap can be used to perform a sort To do a heapsort, add the elements to a heap, then remove the elements one-by-one by always removing the root node The nodes will be removed in sorted order Heapsort is O(n log n)

Hashtables We can handle collisions in hashtables using chaining or open addressing techniques With chaining, each cell in the hashtable is a linked list and thus many elements can be stored in the same cell See Figure 10.22

Hashtables With open addressing techniques, when a collision occurs, a new hash code is calculated, called rehashing Rehashing continues until a free cell is found Linear probing, a simple rehash method, probes down the hashtable (wrapping around when the end is reached) until a free cell is found See Figure 10.23

Summary Chapter 10 has focused on: Sets and maps Trees and binary search trees Heaps Handling collisions in hashtables