Data Structure and Algorithms

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.
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 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.
Data Structures Using C++ 2E
Binary Trees Chapter 6.
Data Structures Using C++1 Chapter 11 Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
Tree.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
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.
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.
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, 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.
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. 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.
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.
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.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
Lecture 17: Trees and Networks I Discrete Mathematical Structures: Theory and Applications.
Trees Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Chapter 11 Binary Trees Dr. Youssef Harrath
Data Structures Using C++ 2E Chapter 11 Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
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.
Foundation of Computing Systems Lecture 4 Trees: Part I.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
(c) University of Washington20-1 CSC 143 Java Trees.
Binary Search Trees Chapter 7 Objectives
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Chapter 15.
Trees Chapter 11 (continued)
Binary Trees and Binary Search Trees
Trees Chapter 11 (continued)
Data Structures Binary Trees 1.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Data Structures & Algorithm Design
Data Structures Using C++ 2E
i206: Lecture 13: Recursion, continued Trees
Data Structures Using C++ 2E
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Data Structures Using Java
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.
Binary Trees, Binary Search Trees
Trees Chapter 10.
Trees.
CSC 143 Java 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:

Data Structure and Algorithms Topic 6 Binary Trees 1

Objectives Learn about binary trees Explore various binary tree traversal algorithms Learn how to organize data in a binary search tree Explore nonrecursive binary tree traversal algorithms Data Structure and File Processing

Binary Trees Definition: a binary tree, T, is either empty or such that T has a special node called the root node T has two sets of nodes, LT and RT, called the left subtree and right subtree of T, respectively LT and RT are binary trees Can be shown pictorially Parent, left child, right child Node represented as a circle Circle labeled by the node Data Structure and File Processing

Binary Trees (cont’d.) Root node drawn at the top Left child of the root node (if any) Drawn below and to the left of the root node Right child of the root node (if any) Drawn below and to the right of the root node Directed edge (directed branch): arrow FIGURE 5-1 Binary tree Data Structure and File Processing

Binary Trees (cont’d.) FIGURE 5-2 Binary tree with one, two, or three nodes FIGURE 5-3 Various binary trees with three nodes Data Structure and File Processing

Binary Trees (cont’d.) Every node in a binary tree Has at most two children struct defining node of a binary tree For each node The data stored in info A pointer to the left child stored in llink A pointer to the right child stored in rlink Data Structure and File Processing

Binary Trees (cont’d.) Pointer to root node is stored outside the binary tree In pointer variable called the root Of type binaryTreeNode FIGURE 5-4 Binary tree Data Structure and File Processing

Binary Trees (cont’d.) Level of a node Height of a binary tree Number of branches on the path Height of a binary tree Number of nodes on the longest path from the root to a leaf See code on page 604 Data Structure and File Processing

Copy Tree Shallow copy of the data Identical copy of a binary tree Obtained when value of the pointer of the root node used to make a copy of a binary tree Identical copy of a binary tree Need to create as many nodes as there are in the binary tree to be copied Nodes must appear in the same order as in the original binary tree Function copyTree Makes a copy of a given binary tree See code on pages 604-605 Data Structure and File Processing

Binary Tree Traversal Must start with the root, and then Visit the node first or Visit the subtrees first Three different traversals Inorder Preorder Postorder Data Structure and File Processing

Binary Tree Traversal (cont’d.) Inorder traversal Traverse the left subtree Visit the node Traverse the right subtree Preorder traversal Data Structure and File Processing

Binary Tree Traversal (cont’d.) Postorder traversal Traverse the left subtree Traverse the right subtree Visit the node Each traversal algorithm: recursive Listing of nodes Inorder sequence Preorder sequence Postorder sequence Data Structure and File Processing

Binary Tree Traversal (cont’d.) FIGURE 5-5 Binary tree for an inorder traversal Data Structure and File Processing

Binary Tree Traversal (cont’d.) Functions to implement the preorder and postorder traversals Data Structure and File Processing

Implementing Binary Trees Operations typically performed on a binary tree Determine if binary tree is empty Search 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 Data Structure and File Processing

Implementing Binary Trees (cont’d.) class binaryTreeType Specifies basic operations to implement a binary tree See code on page 609 Contains statement to overload the assignment operator, copy constructor, destructor Contains several member functions that are private members of the class Binary tree empty if root is NULL See isEmpty function on page 611 Data Structure and File Processing

Implementing Binary Trees (cont’d.) Default constructor Initializes binary tree to an empty state See code on page 612 Other functions for binary trees See code on pages 612-613 Functions: copyTree, destroy, destroyTree See code on page 614 Copy constructor, destructor, and overloaded assignment operator See code on page 615 Data Structure and File Processing

Binary Search Trees Data in each node Larger than the data in its left child Smaller than the data in its right child FIGURE 5-6 Arbitrary binary tree FIGURE 5-7 Binary search tree Data Structure and File Processing

Binary Search Trees (cont’d.) A binary search tree, T, is either empty or the following is true: T has a special node called the root node T has two sets of nodes, LT and RT , called the left subtree and right subtree of T, respectively The key in the root node is larger than every key in the left subtree and smaller than every key in the right subtree LT and RT are binary search trees Data Structure and File Processing

Binary Search Trees (cont’d.) Operations performed on a binary search tree Search the binary search tree for a particular item Insert an item in the binary search tree Delete an item from the binary search tree Find the height of the binary search tree Find the number of nodes in the binary search tree Find the number of leaves in the binary search tree Traverse the binary search tree Copy the binary search tree Data Structure and File Processing

Binary Search Trees (cont’d.) Every binary search tree is a binary tree Height of a binary search tree Determined the same way as the height of a binary tree Operations to find number of nodes, number of leaves, to do inorder, preorder, postorder traversals of a binary search tree Same as those for a binary tree Can inherit functions Data Structure and File Processing

Binary Search Trees (cont’d.) class bSearchTreeType Illustrates basic operations to implement a binary search tree See code on page 618 Function search Function insert Function delete Data Structure and File Processing

Binary Search Tree: Analysis Worst case T: linear Successful case Algorithm makes (n + 1) / 2 key comparisons (average) Unsuccessful case: makes n comparisons FIGURE 5-10 Linear binary trees Data Structure and File Processing

Binary Search Tree: Analysis (cont’d.) Average-case behavior Successful case Search would end at a node n items exist, providing n! possible orderings of the keys Number of comparisons required to determine whether x is in T One more than the number of comparisons required to insert x in T Number of comparisons required to insert x in T Same as number of comparisons made in the unsuccessful search reflecting that x is not in T Data Structure and File Processing

Binary Search Tree: Analysis (cont’d.) Data Structure and File Processing

Binary Search Tree: Analysis (cont’d.) Theorem: let T be a binary search tree with n nodes, where n> 0 The average number of nodes visited in a search of T is approximately 1.39log2n =O(log2n) The number of key comparisons is approximately 2.77 log2n = O(log2n) Data Structure and File Processing

Nonrecursive Inorder Traversal Data Structure and File Processing

Nonrecursive Inorder Traversal (cont’d.) Data Structure and File Processing

Nonrecursive Preorder Traversal Data Structure and File Processing

Nonrecursive Preorder Traversal (cont’d.) Data Structure and File Processing

Nonrecursive Postorder Traversal Data Structure and File Processing

Binary Tree Traversal and Functions as Parameters Passing a function as a parameter to the traversal algorithms Enhances program’s flexibility C++ function name without any parentheses Considered a pointer to the function Specifying a function as a formal parameter to another function See Example 11-3 Data Structure and File Processing

Summary This chapter discussed Binary trees Binary search trees Recursive traversal algorithms Nonrecursive traversal algorithms Data Structure and File Processing