C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 20: Binary Trees.

Slides:



Advertisements
Similar presentations
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 21: Graphs.
Advertisements

Chapter 6: User-Defined Functions I
CCNA Guide to Cisco Networking Fundamentals Fourth Edition
C++ Programming:. From Problem Analysis
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 10 Creating Classes and Objects.

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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
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.
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.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Marc Smith and Jim Ten Eyck
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Binary Search Trees Chapter 7 Objectives
Binary Trees Chapter 6.
Data Structures Using C++1 Chapter 11 Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Starting at Binary Trees
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Data Structures Using Java1 Chapter 10 Binary 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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Lecture 17: Trees and Networks I Discrete Mathematical Structures: Theory and Applications.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
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.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Foundation of Computing Systems Lecture 4 Trees: Part I.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Binary Search Trees Chapter 7 Objectives
CSE 373 Data Structures Lecture 7
Trees Chapter 15.
Data Structure and Algorithms
Trees Chapter 11 (continued)
Recursive Objects (Part 4)
Trees Chapter 11 (continued)
Data Structures Binary Trees 1.
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Chapter 10.1 Binary Search Trees
Tree.
Section 8.1 Trees.
Data Structures Using C++ 2E
Binary Trees, Binary Search Trees
Binary Tree and General Tree
Data Structures Using Java
CS223 Advanced Data Structures and Algorithms
Binary Trees, Binary Search Trees
Trees.
Chapter 20: Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 20: Binary Trees

Objectives In this chapter, you will: Learn about binary trees Explore various binary tree traversal algorithms Learn how to organize data in a binary search tree Learn how to insert and delete items in a binary search tree Explore nonrecursive binary tree traversal algorithms C++ Programming: Program Design Including Data Structures, Fourth Edition2

Binary Trees C++ Programming: Program Design Including Data Structures, Fourth Edition3

Binary Trees (continued) C++ Programming: Program Design Including Data Structures, Fourth Edition4 Right child of ALeft child of A Root node, and Parent of B and C Directed edge, directed branch, or branch Node Empty subtree (F’s right subtree)

Binary Trees (continued) Every node has at most two children A node: −Stores its own information −Keeps track of its left subtree and right subtree lLink and rLink pointers C++ Programming: Program Design Including Data Structures, Fourth Edition9

Binary Trees (continued) A pointer to the root node of the binary tree is stored outside the tree in a pointer variable C++ Programming: Program Design Including Data Structures, Fourth Edition10

Binary Trees (continued) Leaf: node that has no left and right children U is parent of V if there’s a branch from U to V There’s a unique path from root to every node Length of a path: number of branches on path Level of a node: number of branches on the path from the root to the node −The level of the root node of a binary tree is 0 Height of a binary tree: number of nodes on the longest path from the root to a leaf C++ Programming: Program Design Including Data Structures, Fourth Edition11

A leaf A is the parent of B and C A  B  D  G is a path (of length 3) from node A to node G The longest path from root to a leaf is A  B  D  G  I The number of nodes on this path is 5  the height of the tree is 5

Binary Trees (continued) How can we calculate the height of a binary tree? This is a recursive algorithm: C++ Programming: Program Design Including Data Structures, Fourth Edition13

Copy Tree C++ Programming: Program Design Including Data Structures, Fourth Edition14

Binary Tree Traversal Inorder traversal −Traverse the left subtree −Visit the node −Traverse the right subtree Preorder traversal −Visit the node −Traverse the left subtree −Traverse the right subtree C++ Programming: Program Design Including Data Structures, Fourth Edition15

Binary Tree Traversal (continued) Postorder traversal −Traverse the left subtree −Traverse the right subtree −Visit the node C++ Programming: Program Design Including Data Structures, Fourth Edition16

Binary Tree Traversal (continued) Inorder sequence: listing of the nodes produced by the inorder traversal of the tree Preorder sequence: listing of the nodes produced by the preorder traversal of the tree Postorder sequence: listing of the nodes produced by the postorder traversal of the tree C++ Programming: Program Design Including Data Structures, Fourth Edition17

Binary Tree Traversal (continued) Inorder sequence: B D A C Preorder sequence: A B D C Postorder sequence: D B C A C++ Programming: Program Design Including Data Structures, Fourth Edition18

Implementing Binary Trees Typical operations: −Determine whether the binary tree is empty −Search the binary tree for a particular item −Insert an item in the binary tree −Delete an item from the binary tree −Find the height of the binary tree −Find the number of nodes in the binary tree −Find the number of leaves in the binary tree −Traverse the binary tree −Copy the binary tree C++ Programming: Program Design Including Data Structures, Fourth Edition20

Binary Search Trees We can traverse the tree to determine whether 53 is in the binary tree  this is slow C++ Programming: Program Design Including Data Structures, Fourth Edition27

Binary Search Trees (continued) C++ Programming: Program Design Including Data Structures, Fourth Edition28

Binary Search Trees (continued) Every binary search tree is a binary tree C++ Programming: Program Design Including Data Structures, Fourth Edition29

Binary Search Trees (continued) C++ Programming: Program Design Including Data Structures, Fourth Edition30

Search C++ Programming: Program Design Including Data Structures, Fourth Edition32

Insert C++ Programming: Program Design Including Data Structures, Fourth Edition33

Insert (continued) C++ Programming: Program Design Including Data Structures, Fourth Edition34

Delete C++ Programming: Program Design Including Data Structures, Fourth Edition35

Delete (continued) The delete operation has four cases: −The node to be deleted is a leaf −The node to be deleted has no left subtree −The node to be deleted has no right subtree −The node to be deleted has nonempty left and right subtrees C++ Programming: Program Design Including Data Structures, Fourth Edition36

Delete (continued) To delete an item from the binary search tree, we must do the following: −Find the node containing the item (if any) to be deleted −Delete the node C++ Programming: Program Design Including Data Structures, Fourth Edition42

Binary Search Tree: Analysis Let T be a binary search tree with n nodes, where n > 0 Suppose that we want to determine whether an item, x, is in T The performance of the search algorithm depends on the shape of T In the worst case, T is linear C++ Programming: Program Design Including Data Structures, Fourth Edition47

Binary Search Tree: Analysis (continued) Worst case behavior: T is linear –O(n) key comparisons C++ Programming: Program Design Including Data Structures, Fourth Edition48

Binary Search Tree: Analysis (continued) Average-case behavior: −There are n! possible orderings of the keys We assume that orderings are possible −S(n) and U(n): number of comparisons in average successful and unsuccessful case, respectively C++ Programming: Program Design Including Data Structures, Fourth Edition49

Binary Search Tree: Analysis (continued) C++ Programming: Program Design Including Data Structures, Fourth Edition50

Nonrecursive Binary Tree Traversal Algorithms The traversal algorithms discussed earlier are recursive This section discusses the nonrecursive inorder, preorder, and postorder traversal algorithms C++ Programming: Program Design Including Data Structures, Fourth Edition51

Nonrecursive Inorder Traversal For each node, the left subtree is visited first, then the node, and then the right subtree C++ Programming: Program Design Including Data Structures, Fourth Edition52 Will be visited first

Nonrecursive Preorder Traversal For each node, first the node is visited, then the left subtree, and then the right subtree C++ Programming: Program Design Including Data Structures, Fourth Edition54

Nonrecursive Postorder Traversal Visit order: left subtree, right subtree, node We must indicate to the node whether the left and right subtrees have been visited −Solution: other than saving a pointer to the node, save an integer value of 1 before moving to the left subtree and value of 2 before moving to the right subtree −When the stack is popped, the integer value associated with that pointer is popped as well C++ Programming: Program Design Including Data Structures, Fourth Edition55

Binary Tree Traversal and Functions as Parameters In a traversal algorithm, “visiting” may mean different things −Example: output value, update value in some way Problem: How do we write a generic traversal function? −Writing a specific traversal function for each type of “visit” would be cumbersome Solution: pass a function as a parameter to the traversal function C++ Programming: Program Design Including Data Structures, Fourth Edition56

Binary Tree Traversal and Functions as Parameters (continued) In C++, a function name without parentheses is considered a pointer to the function To specify a function as a formal parameter to another function: −Specify the function type, followed by name as a pointer, followed by the parameter types C++ Programming: Program Design Including Data Structures, Fourth Edition57

Summary A binary tree is either empty or it has a special node called the root node −If the tree is nonempty, the root node has two sets of nodes (left and right subtrees), such that the left and right subtrees are also binary trees The node of a binary tree has two links in it A node in the binary tree is called a leaf if it has no left and right children A node U is called the parent of a node V if there is a branch from U to V C++ Programming: Program Design Including Data Structures, Fourth Edition60

Summary (continued) Level of a node: number of branches on the path from the root to the node −The level of the root node of a binary tree is 0 −The level of the children of the root is 1 Height of a binary tree: number of nodes on the longest path from the root to a leaf C++ Programming: Program Design Including Data Structures, Fourth Edition61

Summary (continued) Inorder traversal −Traverse left, visit node, traverse right Preorder traversal −Visit node, traverse left, traverse right Postorder traversal −Traverse left, traverse right, visit node C++ Programming: Program Design Including Data Structures, Fourth Edition62