Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.

Slides:



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


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.
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Advanced Data Structures
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.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
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.
Fundamentals of Python: From First Programs Through Data Structures
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.
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.
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 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.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
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.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
Binary Search Trees (BST)
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.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
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.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Binary Search Trees Chapter 7 Objectives
Trees Chapter 15.
Data Structure and Algorithms
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
Data Structures Binary Trees 1.
Trees ---- Soujanya.
Binary Search Tree (BST)
Section 8.1 Trees.
Data Structures Using C++ 2E
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Data Structures Using Java
Chapter 21: Binary Trees.
Binary Trees, Binary Search Trees
Trees.
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Chapter 19: Binary Trees

Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize data in a binary search tree – Insert and delete items in a binary search tree – Explore nonrecursive binary tree traversal algorithms 2C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Trees Definition: a binary tree T is either empty or has these properties: – Has a root node – Has two sets of nodes: left subtree L T and right subtree R T – L T and R T are binary trees 3C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Trees (cont’d.) 4 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) C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Trees (cont’d.) 5C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Trees (cont’d.) 6C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Trees (cont’d.) Every node has at most two children A node: – Stores its own information – Keeps track of its left subtree and right subtree using pointers lLink and rLink pointers 7C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Trees (cont’d.) A pointer to the root node of the binary tree is stored outside the tree in a pointer variable 8C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Trees (cont’d.) Leaf: node that has no left and right children U is parent of V if there is a branch from U to V There is 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 – Root node level is 0 Height of a binary tree: number of nodes on the longest path from the root to a leaf 9C++ Programming: Program Design Including Data Structures, Sixth Edition

Copy Tree Binary tree is a dynamic data structure – Memory is allocated/deallocated at runtime Using just the value of the pointer of the root node makes a shallow copy of the data To make an identical copy, must create as many nodes as are in the original tree – Use a recursive algorithm 10C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Tree Traversal Insertion, deletion, and lookup operations require traversal of the tree – Must start at the root node Two choices for each node: – Visit the node first – Visit the node’s subtrees first 11C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Tree Traversal (cont’d.) 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 12C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Tree Traversal (cont’d.) Postorder traversal – Traverse the left subtree – Traverse the right subtree – Visit the node Listing of nodes produced by traversal type is called: – Inorder sequence – Preorder sequence – Postorder sequence 13C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Tree Traversal (cont’d.) Inorder sequence: – DFBACGE Preorder sequence: – ABDFCEG Postorder sequence: – FDBGECA 14C++ Programming: Program Design Including Data Structures, Sixth Edition

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 15C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Search Trees Traverse the tree to determine whether 53 is in it - this is slow 16C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Search Trees (cont’d.) In this binary tree, data in each node is: – Larger than data in its left child – Smaller than data in its right child 17C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Search Trees (cont’d.) Definition: a binary search tree T is either empty or has these properties: – Has a root node – Has two sets of nodes: left subtree L T and right subtree R T – Key in root node is larger than every key in left subtree, and smaller than every key in right subtree – L T and R T are binary search trees 18C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Search Trees (cont’d.) Typical operations on a binary search tree: – Determine if it is empty – Search for a particular item – Insert or delete an item – Find the height of the tree – Find the number of nodes and leaves in the tree – Traverse the tree – Copy the tree 19C++ Programming: Program Design Including Data Structures, Sixth Edition

Search Search steps: – Start search at root node – If no match, and search item is smaller than root node, follow lLink to left subtree – Otherwise, follow rLink to right subtree Continue these steps until item is found or search ends at an empty subtree 20C++ Programming: Program Design Including Data Structures, Sixth Edition

Insert After inserting a new item, resulting binary tree must be a binary search tree Must find location where new item should be placed – Must keep two pointers, current and parent of current, in order to insert 21C++ Programming: Program Design Including Data Structures, Sixth Edition

Delete 22C++ Programming: Program Design Including Data Structures, Sixth Edition

Delete (cont’d.) The delete operation has four cases: 1.The node to be deleted is a leaf 2.The node to be deleted has no left subtree 3.The node to be deleted has no right subtree 4.The node to be deleted has nonempty left and right subtrees Must find the node containing the item (if any) to be deleted, then delete the node 23C++ Programming: Program Design Including Data Structures, Sixth Edition

Delete (cont’d.) 24C++ Programming: Program Design Including Data Structures, Sixth Edition

Delete (cont’d.) 25 (cont’d.) C++ Programming: Program Design Including Data Structures, Sixth Edition

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 26C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Search Tree: Analysis (cont’d.) Worst case behavior: T is linear – O(n) key comparisons 27C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Search Tree: Analysis (cont'd.) 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 28C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Search Tree: Analysis (cont’d.) Theorem: Let T be a binary search tree with n nodes, where n > 0 – Average number of nodes visited in a search of T is approximately 1.39log 2 n=O(log 2 n) – Number of key comparisons is approximately 2.77log 2 n=O(log 2 n) 29C++ Programming: Program Design Including Data Structures, Sixth Edition

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

Nonrecursive Inorder Traversal For each node, the left subtree is visited first, then the node, and then the right subtree 31C++ Programming: Program Design Including Data Structures, Sixth Edition

Nonrecursive Preorder Traversal For each node, first the node is visited, then the left subtree, and then the right subtree Must save a pointer to a node before visiting the left subtree, in order to visit the right subtree later 32C++ Programming: Program Design Including Data Structures, Sixth Edition

Nonrecursive Postorder Traversal Visit order: left subtree, right subtree, node Must track for the node whether the left and right subtrees have been visited – Solution: Save a pointer to the node, and also 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 33C++ Programming: Program Design Including Data Structures, Sixth Edition

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 34C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Tree Traversal and Functions as Parameters (cont’d.) Solution: – Pass a function as a parameter to the traversal function – In C++, a function name without parentheses is considered a pointer to the function 35C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Tree Traversal and Functions as Parameters (cont’d.) 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 36C++ Programming: Program Design Including Data Structures, Sixth Edition

Summary A binary tree is either empty or it has a special node called the root node – If nonempty, 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 37C++ Programming: Program Design Including Data Structures, Sixth Edition

Summary (cont’d.) A node U is called the parent of a node V if there is a branch from U to V 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 38C++ Programming: Program Design Including Data Structures, Sixth Edition

Summary (cont’d.) Inorder traversal – Traverse left, visit node, traverse right Preorder traversal – Visit node, traverse left, traverse right Postorder traversal – Traverse left, traverse right, visit node In a binary search tree: – Root node is larger than every node in left subtree – Root node is less than every node in right subtree 39C++ Programming: Program Design Including Data Structures, Sixth Edition