Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
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.
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.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
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.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
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.
Trees, Binary Trees, and Binary Search Trees COMP171.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
© 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 Chapter 17: Linked Lists.
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.
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.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
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.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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 © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Binary Search Trees Chapter 7 Objectives
Definition and Application of Binary Trees
Binary Trees.
Recursive Objects (Part 4)
Binary Search Trees Chapter 7 Objectives
UNIT III TREES.
Binary Search Tree (BST)
Section 8.1 Trees.
Trees.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Binary Trees.
Find in a linked list? first last 7  4  3  8 NULL
Binary Trees, Binary Search Trees
Binary Trees.
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Binary Search Tree.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter 19: Binary Trees Banyan Tree

Topics 19.1 Definition and Application of Binary Trees 19.2 Binary Search Tree Operations 19.3 Template Considerations for Binary Search Trees 19-2

19.1 Definition & 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 (left or right) children of N 19-3 NULL

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 19-4

Binary Tree Terminology Root pointer: points to the root node of the binary tree (like a head pointer for a linked list) Node * Root: Root node: the node with no parent 19-5 NULL Root

Binary Tree Terminology Leaf nodes: nodes that have no children The nodes containing 7 and 43 ARE leaf nodes Nodes containing 19 & 59 are NOT leaf nodes 19-6 NULL NULL

Binary Tree Terminology Child nodes, children: The children of node containing 31 are the nodes containing 19 and 59 The parent of the node containing 43 is the node containing NULL NULL

Binary Tree Terminology A subtree of a binary tree is a part of the tree including node N & all subsequent nodes down to the leaf nodes Such a subtree is said to be rooted at N, and N is called the root of the subtree 19-8

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 19-9

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

Uses of Binary Trees Binary search tree: a binary tree whose data is organized to improve search efficiency 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 Duplicates – either side but must be consistent NULL NULL

19.2 Binary Search Tree Operations Create a binary search tree Repeatedly call insert function, once for each data item 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 19-12

Binary Search Tree Node A node in a binary tree is similar to linked list node, except it has two node pointer fields: struct TreeNode {int value; TreeNode *left; TreeNode *right; }; A constructor can aid in the creation of nodes 19-13

TreeNode Constructor TreeNode::TreeNode(int val) { value = val; left = NULL; right = NULL; } 19-14

Creating a New Node TreeNode *p; int num = 23; p = new TreeNode(num); NULL 23 p

Inserting an item into a Binary Search Tree 1)If tree is empty, replace empty tree with a new binary tree consisting of the new node as root, with empty left & right subtrees 2)Otherwise, 1)if item is less than (or equal to) root, recursively insert item in left subtree. 2)If item is greater than root, recursively insert the item into the right subtree 19-16

Inserting an item into a Binary Search Tree (BST) 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

Inserting to BST // allows for duplicate entries – see p for code to // avoid inserting duplicates void InsertBST (TreeNode *&tree, int num) { if (tree == NULL) // empty tree {tree = new TreeNode (num); return;} if (num value) InsertBST (tree -> left, num); else InsertBST (tree -> right, num); } 19-18

Traversing a Binary Tree - 3 methods Inorder: a)Traverse left subtree of node b)Process data in node c)Traverse right subtree of node Preorder: a)Process data in node b)Traverse left subtree of node c)Traverse right subtree of node Postorder: a)Traverse left subtree of node b)Traverse right subtree of node c)Process data in node 19-19

Inorder Traversal void Inord (Node * R) { if (R == Null) return; else {Inord (R -> left) Process (R -> value) Inord (R -> right) } 19-20

Preorder Traversal void Pre (Node * R) { if (R == Null) return; else {Process (R -> value) Pre (R -> left) Pre (R -> right) } 19-21

Postorder traversal void Post (Node * R) { if (R == Null) return; else {Post (R -> left) Post (R -> right) Process (R -> value) } 19-22

HOMEWORK! Memorize the code for the insert BST, inorder, preorder and postorder traversals of a binary tree! Will be on quiz & test! 19-23

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

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

Searching in a Binary Tree To locate the node containing 43, 1.Examine the root node ( 31 ) 2.Since 43 > 31, examine the right child of the node containing 31, ( 59 ) 3.Since 43 < 59, examine the left child of the node containing 59, ( 43 ) 4.The node containing 43 has been found NULL NULL

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

Deleting a Node from a Binary Tree – One Child If node to be deleted has one child Adjust pointers so that parent of node to be deleted points to child of node to be deleted Delete the node 19-28

Deleting a Node from a Binary Tree – One Child NULL NULL NULL Deleting node containing 19 – before deletion Deleting node containing 19 – after deletion

Deleting a node with 2 children Select a replacement node. Choose the largest node in the left subtree OR Choose the smallest node in the right subtree Copy replacement data into node being deleted Delete replacement node It will have one or zero children 19-30

Finding replacement node Largest node in left subtree From node to be deleted, go left once Then go right until a null is found Smallest node in right subtree From node to be deleted, go right once Then go left until a null is found 19-31