tree
TRANSCRIPT
![Page 1: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/1.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
1
Trees
Chapter 5
![Page 2: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/2.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
2
Binary Search Tree
• Consider the following ordered list of integers
1. Examine middle element
2. Examine left, right sublist (maintain pointers)
3. Examine left, right sublists
80666249352813
![Page 3: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/3.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
3
Binary Search Tree
• Redraw the previous structure so that it has a treelike shape – a binary tree
![Page 4: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/4.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
4
Trees
• A data structure which consists of – a finite set of elements called nodes or vertices– a finite set of directed arcs which connect the
nodes
![Page 5: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/5.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
5
Trees
• Tree terminologyRoot nodeRoot node
Leaf nodesLeaf nodes
• Children of the parent (3)• Children of the parent (3)
![Page 6: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/6.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
6
Binary Trees
• Each node has at most two children• Useful in modeling processes where
– a comparison or experiment has exactly two possible outcomes
– the test is performed repeatedly
• Example– multiple coin tosses– encoding/decoding messages in dots and
dashes such as Mores code
![Page 7: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/7.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
7
Array Representation of Binary Trees
• Store the ith node in the ith location of the array
![Page 8: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/8.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
8
Array Representation of Binary Trees
• Works OK for complete trees, not for sparse trees
![Page 9: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/9.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
9
Linked Representation of Binary Trees
• Uses space more efficiently
• Provides additional flexibility
• Each node has two links– one to the left child of the node– one to the right child of the node– if no child node exists for a node, the link is set
to NULL
![Page 10: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/10.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
10
Linked Representation of Binary Trees
• Example
![Page 11: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/11.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
11
Binary Trees as Recursive Data Structures
• A binary tree is either empty …
or
• Consists of– a node called the root– root has pointers to two
disjoint binary (sub)trees called …• right (sub)tree• left (sub)tree
AnchorAnchor
Inductive step
Inductive step
Which is either empty … or …
Which is either empty … or …
Which is either empty … or …
Which is either empty … or …
![Page 12: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/12.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
12
Tree Traversal is Recursive
If the binary tree is empty thendo nothing
Else N: Visit the root, process dataL: Traverse the left subtreeR: Traverse the right subtree
The "anchor"The "anchor"
The inductive stepThe inductive step
![Page 13: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/13.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
13
ADT Binary Search Tree (BST)
• Collection of Data Elements– binary tree– each node x,
• value in left child of x value in x in right child of x
• Basic operations– Construct an empty BST– Determine if BST is empty– Search BST for given item
![Page 14: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/14.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
14
ADT Binary Search Tree (BST)
• Basic operations – Insert a new item in the BST– Delete an item from the BST– Traverse the BST
• Visit each node exactly once• The inorder traversal must visit the values in
the nodes in ascending order
![Page 15: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/15.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
15
BST Searches
• Search begins at root– If that is desired item, done
• If item is less, move downleft subtree
• If item searched for is greater, move down right subtree
• If item is not found, we will run into an empty subtree
• View search()
![Page 16: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/16.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
16
Inserting into a BST
• Insert function– Uses modified version of search
to locate insertion location or already existing item
– Pointer parent trails search
pointer locptr, keeps track
of parent node
– Thus new node can be attached to BST in proper place
• View insert() function
R
![Page 17: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/17.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
17
Recursive Deletion
Three possible cases to delete a node, x, from a BST
1. The node, x, is a leaf
![Page 18: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/18.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
18
Recursive Deletion
2. The node, x has one child
![Page 19: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/19.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
19
Recursive Deletion
• x has two children
Replace contents of x with inorder successor
Replace contents of x with inorder successor
K
Delete node pointed to by xSucc as described for
cases 1 and 2
Delete node pointed to by xSucc as described for
cases 1 and 2
View remove()
function
View remove()
function
![Page 20: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/20.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
20
BST Class Template
• View complete binary search tree template, Fig. 12.7
• View test program for BST, Fig. 12.8
![Page 21: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/21.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
21
• Tree can be balanced– each node except leaves has exactly 2 child
nodes
![Page 22: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/22.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
22
• Trees can be unbalanced– not all nodes have exactly 2 child nodes
![Page 23: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/23.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
23
• Trees can be totally lopsided– Suppose each node has a right child only– Degenerates into a linked list
Processing time affected by
"shape" of tree
Processing time affected by
"shape" of tree
![Page 24: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/24.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
24
Hash Tables
• Recall order of magnitude of searches– Linear search O(n)
– Binary search O(log2n)
– Balanced binary tree search O(log2n)
– Unbalanced binary tree can degrade to O(n)
![Page 25: Tree](https://reader035.vdocuments.us/reader035/viewer/2022070322/559124ba1a28ab9b368b46d3/html5/thumbnails/25.jpg)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
25
Non Binary Trees
• Some applications require more than two children per node
– Genealogical tree
– Game tree