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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Starting Out with C++, 3 rd Edition 1 Chapter 20 Binary Trees.
Marc Smith and Jim Ten Eyck
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
Binary Search Trees Chapter 7 Objectives
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
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.
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
© 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.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
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.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Binary Tree.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
(c) University of Washington20-1 CSC 143 Java Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Binary Search Trees Chapter 7 Objectives
Definition and Application of Binary Trees
Data Structure and Algorithms
Trees Chapter 11 (continued)
Recursive Objects (Part 4)
Trees Chapter 11 (continued)
Binary Search Tree (BST)
Section 8.1 Trees.
Trees.
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.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Chapter 16 Tree Implementations
Binary Trees, Binary Search Trees
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

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 Trees

Chapter 19 Starting Out with C++: Early Objects 5/e slide 2 © 2006 Pearson Education. All Rights Reserved Topics 19.1 Definition and Application of Binary Trees 19.2 Binary Search Tree Operations 19.3 Template Considerations for Binary Search Trees

Chapter 19 Starting Out with C++: Early Objects 5/e slide 3 © 2006 Pearson Education. All Rights Reserved 19.1 Definition and Application of Binary Trees Binary tree: a nonlinear data structure in which each node may point to 0, 1, or two other nodes The nodes that a node N points to are the children of N NULL

Chapter 19 Starting Out with C++: Early Objects 5/e slide 4 © 2006 Pearson Education. All Rights Reserved Terminology If a node N is a child of another node P, then P is called the parent of N A node that has no children is called a leaf In a binary tree there is a unique node with no parent. This is the root of the tree

Chapter 19 Starting Out with C++: Early Objects 5/e slide 5 © 2006 Pearson Education. All Rights Reserved Binary Tree Terminology Tree pointer: like a head pointer for a linked list, it points to the first node in the binary tree Root node: the node with no parent NULL

Chapter 19 Starting Out with C++: Early Objects 5/e slide 6 © 2006 Pearson Education. All Rights Reserved Binary Tree Terminology Leaf nodes: nodes that have no children The nodes containing 7 and 43 are leaf nodes NULL NULL

Chapter 19 Starting Out with C++: Early Objects 5/e slide 7 © 2006 Pearson Education. All Rights Reserved Binary Tree Terminology Child nodes, children: The children of the node containing 31 are the nodes containing 19 and 59 NULL NULL

Chapter 19 Starting Out with C++: Early Objects 5/e slide 8 © 2006 Pearson Education. All Rights Reserved Binary Tree Terminology The parent of the node containing 43 is the node containing 59 NULL NULL

Chapter 19 Starting Out with C++: Early Objects 5/e slide 9 © 2006 Pearson Education. All Rights Reserved Binary Tree Terminology A subtree of a binary tree is a part of the tree from a node N down to the leaf nodes Such a subtree is said to be rooted at N, and N is called the root of the subtree

Chapter 19 Starting Out with C++: Early Objects 5/e slide 10 © 2006 Pearson Education. All Rights Reserved Subtrees of Binary Trees A subtree of a binary tree is itself a binary tree A nonempty binary tree consists of a root node, with the rest of its nodes forming two subtrees, called the left and right subtree

Chapter 19 Starting Out with C++: Early Objects 5/e slide 11 © 2006 Pearson Education. All Rights Reserved Binary Tree Terminology The node containing 31 is the root The nodes containing 19 and 7 form the left subtree The nodes containing 59 and 43 form the right subtree NULL NULL

Chapter 19 Starting Out with C++: Early Objects 5/e slide 12 © 2006 Pearson Education. All Rights Reserved Uses of Binary Trees Binary search tree: a binary tree whose data is organized to simplify searches Left subtree at each node contains data values less than the data in the node Right subtree at each node contains values greater than the data in the node NULL NULL

Chapter 19 Starting Out with C++: Early Objects 5/e slide 13 © 2006 Pearson Education. All Rights Reserved 19.2 Binary Search Tree Operations Create a binary search tree Insert a node into a binary tree – put node into tree in its correct position to maintain order Find a node in a binary tree – locate a node with particular data value Delete a node from a binary tree – remove a node and adjust links to preserve the binary tree and the order

Chapter 19 Starting Out with C++: Early Objects 5/e slide 14 © 2006 Pearson Education. All Rights Reserved Binary Search Tree Node A node in a binary tree is like a node in a linked list, except it has two node pointer fields: struct TreeNode {int value; TreeNode *left; TreeNode *right; }; A constructor can aid in the creation of nodes

Chapter 19 Starting Out with C++: Early Objects 5/e slide 15 © 2006 Pearson Education. All Rights Reserved TreeNode Constructor TreeNode::TreeNode(int val, TreeNode *l1=0, TreeNode *r1=0) { value = val; left = l1; right = r1; }

Chapter 19 Starting Out with C++: Early Objects 5/e slide 16 © 2006 Pearson Education. All Rights Reserved Creating a New Node TreeNode *p; int num = 23; p = new TreeNode(num); NULL 23

Chapter 19 Starting Out with C++: Early Objects 5/e slide 17 © 2006 Pearson Education. All Rights Reserved Inserting an item into a Binary Search Tree 1)If tree is empty, replace the empty tree with a new binary tree consisting of the new node as root, with empty left and right subtrees 2)Otherwise, if the item is less than root, recursively insert the item in the left subtree. If the item is greater than the root, recursively insert the item into the right subtree

Chapter 19 Starting Out with C++: Early Objects 5/e slide 18 © 2006 Pearson Education. All Rights Reserved Inserting an item into a Binary Search Tree NULL root Step 1: 23 is less than 31. Recursively insert 23 into the left subtree Step 2: 23 is greater than 19. Recursively insert 23 into the right subtree Step 3: Since the right subtree is NULL, insert 23 here NULL value to insert: 23

Chapter 19 Starting Out with C++: Early Objects 5/e slide 19 © 2006 Pearson Education. All Rights Reserved Traversing a Binary Tree Three traversal methods: 1)Inorder: a)Traverse left subtree of node b)Process data in node c)Traverse right subtree of node 2)Preorder: a)Process data in node b)Traverse left subtree of node c)Traverse right subtree of node 3)Postorder: a)Traverse left subtree of node b)Traverse right subtree of node c)Process data in node

Chapter 19 Starting Out with C++: Early Objects 5/e slide 20 © 2006 Pearson Education. All Rights Reserved Traversing a Binary Tree NULL TRAVERSAL METHOD NODES VISITED IN ORDER Inorder 7, 19, 31, 43, 59 Preorder 31, 19, 7, 59, 43 Postorder 7, 19, 43, 59, 31 NULL

Chapter 19 Starting Out with C++: Early Objects 5/e slide 21 © 2006 Pearson Education. All Rights Reserved Searching in a Binary Tree 1)Start at root node 2)Examine node data: a)Is it desired value? Done b)Else, is desired data < node data? Repeat step 2 with left subtree c)Else, is desired data > node data? Repeat step 2 with right subtree 3)Continue until desired value found or NULL pointer reached NULL NULL

Chapter 19 Starting Out with C++: Early Objects 5/e slide 22 © 2006 Pearson Education. All Rights Reserved Searching in a Binary Tree To locate the node containing 43, –Examine the root node ( 31 ) –Since 43 > 31, examine the right child of the node containing 31, ( 59 ) –Since 43 < 59, examine the left child of the node containing 59, ( 43 ) –The node containing 43 has been found NULL NULL

Chapter 19 Starting Out with C++: Early Objects 5/e slide 23 © 2006 Pearson Education. All Rights Reserved Deleting a Node from a Binary Tree – Leaf Node If node to be deleted is a leaf node, replace parent node’s pointer to it with a NULL pointer, then delete the node NULL 7 19 NULL 19 NULL Deleting node with 7 – before deletion Deleting node with 7 – after deletion

Chapter 19 Starting Out with C++: Early Objects 5/e slide 24 © 2006 Pearson Education. All Rights Reserved Deleting a Node from a Binary Tree – One Child If node to be deleted has one child node, adjust pointers so that parent of node to be deleted points to child of node to be deleted, then delete the node

Chapter 19 Starting Out with C++: Early Objects 5/e slide 25 © 2006 Pearson Education. All Rights Reserved Deleting a Node from a Binary Tree – One Child NULL NULL NULL Deleting node with 19 – before deletion Deleting node with 19 – after deletion

Chapter 19 Starting Out with C++: Early Objects 5/e slide 26 © 2006 Pearson Education. All Rights Reserved Deleting a Node from a Binary Tree – Two Children If node to be deleted has left and right children, –‘Promote’ one child to take the place of the deleted node –Locate correct position for other child in subtree of promoted child Convention in text: "promote" the right child, position left subtree underneath

Chapter 19 Starting Out with C++: Early Objects 5/e slide 27 © 2006 Pearson Education. All Rights Reserved Deleting a Node from a Binary Tree – Two Children NULL NULL NULL Deleting node with 31 – before deletion Deleting node with 31 – after deletion 7 19 NULL

Chapter 19 Starting Out with C++: Early Objects 5/e slide 28 © 2006 Pearson Education. All Rights Reserved 19.3 Template Considerations for Binary Search Trees Binary tree can be implemented as a template, allowing flexibility in determining type of data stored Implementation must support relational operators >, <, and == to allow comparison of nodes

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 Trees