Download presentation

Presentation is loading. Please wait.

1
**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.

2
**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

3
**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

4
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

5
**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)

6
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

7
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)

8
**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

9
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 and 10.16 The smallest node in a heap is always at the root

10
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 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 and 10.20

11
**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)

12
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

13
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

14
**Summary Chapter 10 has focused on: Sets and maps**

Trees and binary search trees Heaps Handling collisions in hashtables

Similar presentations

OK

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.

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.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on rabindranath tagore in english Flexible oled display ppt on tv Ppt on phonetic transcription examples Ppt on revolution of the earth and seasons kids Ppt on sanskrit grammar for class 10 Ppt on home automation and security for mobile devices Ppt on channels of distribution powerpoint Ppt on recycling of waste in the philippines Ppt on mapping space around us Ppt on different mode of transport around the world