Starting Out with C++ Early Objects Seventh Edition
description
Transcript of Starting Out with C++ Early Objects Seventh Edition
![Page 1: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/1.jpg)
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
![Page 2: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/2.jpg)
Topics
19.1 Definition and Application of Binary Trees19.2 Binary Search Tree Operations19.3 Template Considerations for Binary Search Trees
19-2
![Page 3: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/3.jpg)
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 nodeN points to are the
(left or right)childrenof N 19-3
NULL NULL
NULL NULL NULL NULL
![Page 4: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/4.jpg)
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
![Page 5: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/5.jpg)
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 parent19-5
NULL NULL
NULL NULL NULL NULL
Root
![Page 6: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/6.jpg)
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 NULL7
19
31
43
59
NULL NULL NULL NULL
![Page 7: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/7.jpg)
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 59
19-7
NULL NULL7
19
31
43
59
NULL NULL NULL NULL
![Page 8: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/8.jpg)
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
![Page 9: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/9.jpg)
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
![Page 10: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/10.jpg)
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
19-10
NULL NULL7
19
31
43
59
NULL NULL NULL NULL
![Page 11: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/11.jpg)
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
19-11
NULL NULL7
19
31
43
59
NULL NULL NULL NULL
![Page 12: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/12.jpg)
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
![Page 13: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/13.jpg)
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
![Page 14: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/14.jpg)
TreeNode Constructor
TreeNode::TreeNode(int val) { value = val; left = NULL; right = NULL; }
19-14
![Page 15: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/15.jpg)
Creating a New Node
TreeNode *p; int num = 23; p = new TreeNode(num);
19-15NULLNULL
23
p
![Page 16: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/16.jpg)
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 subtree19-16
![Page 17: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/17.jpg)
Inserting an item into a Binary Search Tree (BST)
19-17
NULL NULL7
19
31
43
59
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 NULL NULL NULL
value to insert:23
![Page 18: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/18.jpg)
Inserting to BST// allows for duplicate entries – see p. 1119 for code to// avoid inserting duplicates
void InsertBST (TreeNode *&tree, int num){ if (tree == NULL) // empty tree
{tree = new TreeNode (num); return;} if (num <= tree -> value)
InsertBST (tree -> left, num); else InsertBST (tree -> right, num);} 19-18
![Page 19: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/19.jpg)
Traversing a Binary Tree -3 methods
Inorder: a) Traverse left subtree of nodeb) Process data in nodec) Traverse right subtree of node
Preorder: a) Process data in nodeb) Traverse left subtree of nodec) Traverse right subtree of node
Postorder: a) Traverse left subtree of nodeb) Traverse right subtree of nodec) Process data in node 19-19
![Page 20: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/20.jpg)
Inorder Traversal
void Inord (Node * R){ if (R == Null) return; else
{ Inord (R -> left)Process (R -> value)Inord (R -> right)
}} 19-20
![Page 21: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/21.jpg)
Preorder Traversal
void Pre (Node * R){ if (R == Null) return; else
{ Process (R -> value)Pre (R -> left)Pre (R -> right)
}} 19-21
![Page 22: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/22.jpg)
Postorder traversal
void Post (Node * R){ if (R == Null) return; else
{ Post (R -> left)Post (R -> right)Process (R -> value)
}} 19-22
![Page 23: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/23.jpg)
HOMEWORK!
Memorize the code for the insert BST, inorder, preorder and postorder traversals of a binary tree!
Will be on quiz & test!
19-23
![Page 24: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/24.jpg)
Traversing a Binary Tree
19-24
NULL NULL7
19
31
43
59
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 NULL NULL NULL
![Page 25: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/25.jpg)
Searching in a Binary Tree1) Start at root node2) Examine node data:
a) Is it desired value? Doneb) 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
19-25
NULL NULL7
19
31
43
59
NULL NULL NULL NULL
![Page 26: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/26.jpg)
Searching in a Binary TreeTo 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
19-26
NULL NULL7
19
31
43
59
NULL NULL NULL NULL
![Page 27: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/27.jpg)
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
19-27
NULL7
19
NULL NULL
NULL
19
NULL
Deleting node with 7 – before deletion
Deleting node with 7 – after deletion
![Page 28: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/28.jpg)
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
![Page 29: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/29.jpg)
Deleting a Node from a Binary Tree – One Child
19-29
NULL NULL7
19
31
43
59
NULL NULL NULL NULL
NULL
7
31
43
59
NULL NULL
NULL NULL
Deleting node containing 19 – before deletion Deleting node containing
19 – after deletion
![Page 30: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/30.jpg)
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
![Page 31: Starting Out with C++ Early Objects Seventh Edition](https://reader036.fdocuments.in/reader036/viewer/2022081507/5681609b550346895dcfc36e/html5/thumbnails/31.jpg)
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