1 Chapter 25 Trees Iterators Heaps Priority Queues.

Slides:



Advertisements
Similar presentations
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
Advertisements

TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
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.
Trees Chapter 8.
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
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.
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 26 Binary Search Trees.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Fundamentals of Python: From First Programs Through Data Structures
Lecture Using Libraries Java has a very strong culture of open-source software Students, professors, programming hobbyists, and developers who choose.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Marc Smith and Jim Ten Eyck
Binary Search Trees Chapter 7 Objectives
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
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.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
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.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
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.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 20 Lists, Stacks,
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).
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
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.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Binary trees Binary search trees Expression trees Heaps Data Structures and Algorithms in Java, Third EditionCh06 – 1.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Binary Search Trees Chapter 7 Objectives
Non Linear Data Structure
Trees Chapter 15.
Chapter 25 Binary Search Trees
Trees Chapter 11 (continued)
Recursive Objects (Part 4)
Trees Chapter 11 (continued)
Binary Search Tree (BST)
Section 8.1 Trees.
Trees.
Chapter 17 Object-Oriented Data Structures
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.
Lecture 12 CS203 1.
Topic 6: Binary Search Tree Data structure Operations
Trees.
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

1 Chapter 25 Trees Iterators Heaps Priority Queues

2 Objectives F To design and implement a binary search tree (§25.2). F To insert an element to a binary search tree (§25.2.2). F To traverse elements in a binary tree (§25.2.3). F To delete elements from a binary search tree (§25.3). F To create iterators for traversing a binary tree (§25.4). F To design and implement a heap (§25.5). F To design and implement a priority queue (§25.6).

3 Binary Trees F A list, stack, or queue is a linear structure –consists of a sequence of elements F A binary tree is a hierarchical structure –It is either empty or consists of an element called the root and two distinct binary trees called the u left subtree and u right subtree

4 See How a Binary Tree Works cise/Exercise25_15.html

5 Binary Tree Terms F root of left (right) subtree of a node is called a left (right) child of the node F leaf - –A node without children F binary search tree – A special type of binary tree is often useful F A binary search tree (with no duplicate elements) has the property that for every node in the tree : –the value of any node in its left subtree is less than the value of the node and –the value of any node in its right subtree is greater than the value of the node F The binary trees in Figure 25.1 are all binary search trees. This section is concerned with binary search trees.

6 Representing Binary Trees F A binary tree can be represented using a set of linked nodes F Each node contains: – a value and two links named left and right that reference the left child and right child, respectively class TreeNode { E element; TreeNode left; TreeNode right; public TreeNode(E o) { element = o; }

7 Inserting an Element to a Binary Tree F If a binary tree is empty –create a root node with the new element F Otherwise –locate the parent node for the new element node –If the new element is less than the parent element u the node for the new element becomes the left child of the parent –If the new element is greater than the parent element u the node for the new element becomes the right child of the parent

8 Inserting an Element to a Binary Tree if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { parent = current; current = current.right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); else parent.right = new TreeNode(elemenet); return true; // Element inserted } Insert 101 into the following tree.

9 Trace Inserting 101 into the following tree if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { parent = current; current = current.right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); else parent.right = new TreeNode(elemenet); return true; // Element inserted } Insert 101 into the following tree.

10 Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { parent = current; current = current.right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); else parent.right = new TreeNode(elemenet); return true; // Element inserted } Insert 101 into the following tree.

11 Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { parent = current; current = current.right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); else parent.right = new TreeNode(elemenet); return true; // Element inserted } Insert 101 into the following tree.

12 Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { parent = current; current = current.right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); else parent.right = new TreeNode(elemenet); return true; // Element inserted } Insert 101 into the following tree. 101 < 60?

13 Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { parent = current; current = current.right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); else parent.right = new TreeNode(elemenet); return true; // Element inserted } Insert 101 into the following tree. 101 > 60?

14 Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { parent = current; current = current.right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); else parent.right = new TreeNode(elemenet); return true; // Element inserted } Insert 101 into the following tree. 101 > 60 true

15 Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { parent = current; current = current.right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); else parent.right = new TreeNode(elemenet); return true; // Element inserted } Insert 101 into the following tree. 101 > 60 true

16 Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { parent = current; current = current.right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); else parent.right = new TreeNode(elemenet); return true; // Element inserted } Insert 101 into the following tree. 101 > 60 true

17 Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { parent = current; current = current.right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); else parent.right = new TreeNode(elemenet); return true; // Element inserted } Insert 101 into the following tree. 101 > 100 false

18 Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { parent = current; current = current.right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); else parent.right = new TreeNode(elemenet); return true; // Element inserted } Insert 101 into the following tree. 101 > 100 true

19 Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { parent = current; current = current.right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); else parent.right = new TreeNode(elemenet); return true; // Element inserted } Insert 101 into the following tree. 101 > 100 true

20 Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { parent = current; current = current.right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); else parent.right = new TreeNode(elemenet); return true; // Element inserted } Insert 101 into the following tree. 101 > 100 true

21 Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { parent = current; current = current.right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); else parent.right = new TreeNode(elemenet); return true; // Element inserted } Insert 101 into the following tree. 101 > 100 true

22 Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { parent = current; current = current.right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); else parent.right = new TreeNode(elemenet); return true; // Element inserted } Insert 101 into the following tree. 101 < 107 true

23 Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { parent = current; current = current.right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); else parent.right = new TreeNode(elemenet); return true; // Element inserted } Insert 101 into the following tree. 101 < 107 true

24 Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { parent = current; current = current.right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); else parent.right = new TreeNode(elemenet); return true; // Element inserted } Insert 101 into the following tree. 101 < 107 true

25 Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { parent = current; current = current.right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); else parent.right = new TreeNode(elemenet); return true; // Element inserted } Insert 101 into the following tree. current is null now

26 Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { parent = current; current = current.right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); else parent.right = new TreeNode(elemenet); return true; // Element inserted } Insert 101 into the following tree. 101 < 107 true

27 Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { parent = current; current = current.right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); else parent.right = new TreeNode(elemenet); return true; // Element inserted } Insert 101 into the following tree. 101 < 107 true

28 Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { parent = current; current = current.right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); else parent.right = new TreeNode(elemenet); return true; // Element inserted } Insert 101 into the following tree. 101 < 107 true

29 Inserting 59 into the Tree if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { parent = current; current = current.right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); else parent.right = new TreeNode(elemenet); return true; // Element inserted }

30 Tree Traversal F Tree traversal – –Process of visiting each node in the tree exactly once –Several ways to traverse a tree u Inorder – visit the left subtree of the current node first, then the current node itself, and finally the right subtree of the current node u Preorder –visit the left subtree of the current node first, then the right subtree of the current node, and finally the current node itself u Postorder –visit the current node first, then the left subtree of the current node, and finally the right subtree of the current node u depth-first u breadth-first traversals –visit the nodes level by level. First visit the root, then all children of the root from left to right, then grandchildren of the root from left to right, and so on

31 Tree Traversal F For example, in the tree in Figure 25.2 –inorder is u –postorder is u –preorder is u –breadth-first traversal is u

32 Tree Interface F Tree interface –defines common operations for trees, and the AbstractTree class partially implements Tree AbstractTree Tree

33 BinaryTree Class F BinaryTree with A concrete BinaryTree class can be defined to extend AbstractTree BinaryTree

34 Example: Using Binary Trees Write a program that creates a binary tree using BinaryTree Add strings into the binary tree and traverse the tree in – – inorder –Postorder –preorder TestBinaryTree Run

35 Tree After Insertions Inorder: Adam, Daniel George, Jones, Michael, Peter, Tom Postorder: Daniel Adam, Jones, Peter, Tom, Michael, George Preorder: George, Adam, Daniel, Michael, Jones, Tom, Peter

36 Deleting Elements in a Binary Search Tree F To delete an element from a binary tree – – first locate the node that contains the element and also its parent node –Let current point to the node that contains the element in the binary tree and –parent point to the parent of the current node – current node may be a left child or a right child of the parent node –There are two cases to consider:

37 Deleting Elements in a Binary Search Tree F Case 1: –The current node does not have a left child –Simply connect the parent with the right child of the current node

38 Deleting Elements in a Binary Search Tree For example, to delete node 10 in Figure 25.9(a). Connect the parent of node 10 with the right child of node 10, as shown in Figure 25.9(b).

39 Deleting Elements in a Binary Search Tree F Case 2: –The current node has a left child –Let rightMost point to the node that contains the largest element in the left subtree of the current node and parentOfRightMost point to the parent node of the rightMost node –Note that the rightMost node cannot have a right child, but may have a left child –Replace the element value in the current node with the one in the rightMost node, connect the parentOfRightMost node with the left child of the rightMost node, and delete the rightMost node, as shown in Figure 25.10(b).

40 Deleting Elements in a Binary Search Tree Case 2 diagram

41 Deleting Elements in a Binary Search Tree Case 2 example, delete 20

42 Examples

43 Examples

44 Examples TestBinaryTreeDelete Run

45 binary tree time complexity F It is obvious that the time complexity for the inorder, preorder, and postorder is O(n) F since each node is traversed only once F The time complexity for search, insertion and deletion is the height of the tree F In the worst case, the height of the tree is O(n)

46 Tree Visualization BinaryTreeView Run DisplayBinaryTree

47 Iterators F is an object that provides a uniform way for traversing the elements in a container such as a set, list, binary tree, etc. TestBinaryTreeWithIterator Run

48 Heap F Heap is a useful data structure for designing efficient sorting algorithms and priority queues F A heap is a binary tree with the following properties: –It is a complete binary tree –Each node is greater than or equal to any of its children

49 Complete Binary Tree F A binary tree is complete – –if every level of the tree is full except that the last level may not be full and all the leaves on the last level are placed left-most –binary trees in (a) and (b) are complete –binary trees in (c) and (d) are not complete –binary tree in (a) is a heap –but the binary tree in (b) is not a heap, because the root (39) is less than its right child (42) (a) (b)(c) (d)

50 See How a Heap Works cise/Exercise25_20.html

51 Representing a Heap F For a node at position i, its left child is at position 2i+1 and its right child is at position 2i+2, and its parent is (i-1)/2 F For example, the node for element 39 is at position 4, so its left child (element 14) is at 9 (2*4+1), its right child (element 33) is at 10 (2*4+2), and its parent (element 42) is at 1 ((4-1)/2).

52 Adding Elements to the Heap Adding 3, 5, 1, 19, 11, and 22 to a heap, initially empty

53 Rebuild the heap after adding a new node Adding 88 to the heap

54 Removing the Root and Rebuild the Tree Removing root 62 from the heap

55 Removing the Root and Rebuild the Tree Move 9 to root

56 Removing the Root and Rebuild the Tree Swap 9 with 59

57 Removing the Root and Rebuild the Tree Swap 9 with 44

58 Removing the Root and Rebuild the Tree Swap 9 with 30

59 Heap Class Heap Run TestHeap

60 Priority Queue A regular queue is a first-in and first-out data structure. Elements are appended to the end of the queue and are removed from the beginning of the queue. In a priority queue, elements are assigned with priorities. When accessing elements, the element with the highest priority is removed first. A priority queue has a largest-in, first-out behavior. For example, the emergency room in a hospital assigns patients with priority numbers; the patient with the highest priority is treated first. MyPriorityQueue Run TestPriorityQueue