1. 2 3 4 5 6 7 8 9 10 11 12 13 14 15.

Slides:



Advertisements
Similar presentations
Mathematical Preliminaries
Advertisements

COSC2007 Data Structures II Chapter 10 Trees I. 2 Topics Terminology.
Chapter 17 Linked Data Structures. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Nodes and Linked Lists Creating,
Data Structures Using C++
Data Structures Using C++ 2E Chapter 10 Sorting Algorithms.
David Luebke 1 8/25/2014 CS 332: Algorithms Red-Black Trees.
25 seconds left…...
CSE Lecture 17 – Balanced trees
Rizwan Rehman Centre for Computer Studies Dibrugarh University
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 20: Binary Trees.
Data Structures Using C++ 2E
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.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
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++
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.
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.
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.
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.
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)
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.
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.
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.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
Trees Chapter 15.
Data Structure and Algorithms
Binary Trees and Binary Search Trees
Recursive Objects (Part 4)
Data Structures Binary Trees 1.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Data Structures & Algorithm Design
Data Structures Using C++ 2E
Chapter 20: Binary Trees.
Data Structures Using Java
Chapter 21: Binary Trees.
Chapter 16 Tree Implementations
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

Data Structures Using C++ 2E24 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, L T and R T, called the left subtree and right subtree of T, respectively –L T and R T are binary trees Can be shown pictorially –Parent, left child, right child Node represented as a circle –Circle labeled by the node

Data Structures Using C++ 2E25 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 11-1 Binary tree

Data Structures Using C++ 2E26 Binary Trees (cont’d.) FIGURE 11-2 Binary tree with one, two, or three nodes FIGURE 11-3 Various binary trees with three nodes

Data Structures Using C++ 2E27 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 Structures Using C++ 2E28 Binary Trees (cont’d.) Pointer to root node is stored outside the binary tree –In pointer variable called the root Of type binaryTreeNode FIGURE 11-4 Binary tree

Data Structures Using C++ 2E29 Binary Trees (cont’d.) Level of a node –Number of branches on the path Height of a binary tree –Number of nodes on the longest path from the root to a leaf

Copy Tree Shallow copy of the data –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 Data Structures Using C++ 2E30

copyTree Data Structures Using C++ 2E31

Data Structures Using C++ 2E32 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 Structures Using C++ 2E33 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

Data Structures Using C++ 2E34 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 Structures Using C++ 2E35 Binary Tree Traversal (cont’d.) FIGURE 11-5 Binary tree for an inorder traversal

Data Structures Using C++ 2E36 Binary Tree Traversal (cont’d.) Functions to implement the preorder and postorder traversals

Data Structures Using C++ 2E37 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

Recursive solutions Find the number of nodes in the binary tree –General case: 1 + num_of_nodes(left) + num_of_nodes(right) Find the number of leaves in the binary tree –General case: num_of_leaves (left) + num_of_leaves(right) Data Structures Using C++ 2E38

Data Structures Using C++ 2E39 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

Data Structures Using C++ 2E40 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 Functions: copyTree, destroy, destroyTree –See code on page 614 Copy constructor, destructor, and overloaded assignment operator –See code on page 615

Data Structures Using C++ 2E41

Data Structures Using C++ 2E42 Nonrecursive Inorder Traversal NULL: child of a leaf node Not NULL: real node Get the leaf node

Data Structures Using C++ 2E43 Nonrecursive Inorder Traversal (cont’d.) NULL: child of a leaf node Get the leaf node

Data Structures Using C++ 2E44 Nonrecursive Preorder Traversal NULL: child of a leaf node Not NULL: real node Get the leaf node

Data Structures Using C++ 2E45 Nonrecursive Preorder Traversal (cont’d.) NULL: child of a leaf node Not NULL: real node Get the leaf node

46 Nonrecursive Postorder Traversal v == 0: all the way, going left v == 1: came back from left child v == 2: came back from right child V == 2, came back from right child; visit the node V == 1, came back from left; put the node back to the stack