This commit is contained in:
Jidong Xiao
2024-03-22 17:45:02 -04:00
parent d405d1b166
commit 3f1752bb5f

View File

@@ -1,6 +1,6 @@
# Lecture 17 --- Trees, Part I # Lecture 18 --- Trees, Part I
## Review from Lecture 16 ## Review from Lecture 17
- STL set container class (like STL map, but without the pairs!) - STL set container class (like STL map, but without the pairs!)
- set iterators, insert, erase, find - set iterators, insert, erase, find
@@ -16,7 +16,7 @@
- Binary search trees are the mechanism underlying maps & sets (and multimaps & multisets). - Binary search trees are the mechanism underlying maps & sets (and multimaps & multisets).
- Mathematically speaking: A _graph_ is a set of vertices connected by edges. And a tree is a special graph that has no _cycles_. The edges that connect nodes in trees and graphs may be _directed_ or _undirected_. - Mathematically speaking: A _graph_ is a set of vertices connected by edges. And a tree is a special graph that has no _cycles_. The edges that connect nodes in trees and graphs may be _directed_ or _undirected_.
## 17.1 Definition: Binary Trees ## 18.1 Definition: Binary Trees
- A binary tree (strictly speaking, a “rooted binary - A binary tree (strictly speaking, a “rooted binary
tree”) is either empty or is a node that has tree”) is either empty or is a node that has
@@ -42,7 +42,7 @@ called a leaf node.
- A nodes parent is the unique node that points to - A nodes parent is the unique node that points to
it. Only the root has no parent. it. Only the root has no parent.
## 17.2 Definition: Binary Search Trees ## 18.2 Definition: Binary Search Trees
- A binary search tree (often abbreviated to - A binary search tree (often abbreviated to
BST) is a binary tree where at each node BST) is a binary tree where at each node
@@ -58,7 +58,7 @@ storing string values.
![alt text](bst.png "binary search tree") ![alt text](bst.png "binary search tree")
## 17.3 Definition: Balanced Trees ## 18.3 Definition: Balanced Trees
- The number of nodes on each subtree of each node in a - The number of nodes on each subtree of each node in a
“balanced” tree is approximately the same. In order to “balanced” tree is approximately the same. In order to
@@ -67,7 +67,7 @@ about the number of nodes in the tree?
- In order to claim the performance advantages of trees, we must assume and ensure that our data structure - In order to claim the performance advantages of trees, we must assume and ensure that our data structure
remains approximately balanced. (Youll see much more of this in Intro to Algorithms!) remains approximately balanced. (Youll see much more of this in Intro to Algorithms!)
## 17.4 Exercise ## 18.4 Exercise
Consider the following values: Consider the following values:
4.5, 9.8, 3.5, 13.6, 19.2, 7.4, 11.7 4.5, 9.8, 3.5, 13.6, 19.2, 7.4, 11.7
@@ -80,7 +80,7 @@ tree structure for a given set of values is not unique!
3. How many exactly balanced binary search trees exist with these numbers? How many exactly balanced 3. How many exactly balanced binary search trees exist with these numbers? How many exactly balanced
binary trees exist with these numbers? binary trees exist with these numbers?
## 17.5 Beginning our implementation of ds_set: The Tree Node Class ## 18.5 Beginning our implementation of ds_set: The Tree Node Class
- Here is the class definition for nodes in the tree. We will use this for the tree manipulation code we write. - Here is the class definition for nodes in the tree. We will use this for the tree manipulation code we write.
@@ -99,7 +99,7 @@ public:
![alt text](ds_set_diagram.png "ds set diagram") ![alt text](ds_set_diagram.png "ds set diagram")
## 17.6 Exercises ## 18.6 Exercises
1. Write a templated function to find the smallest value stored in a binary search tree whose root node is pointed 1. Write a templated function to find the smallest value stored in a binary search tree whose root node is pointed
to by p. to by p.
@@ -108,7 +108,7 @@ to by p.
tree) of integers. The function should accept a TreeNode<int> pointer as its sole argument and return an tree) of integers. The function should accept a TreeNode<int> pointer as its sole argument and return an
integer. Hint: think recursively! integer. Hint: think recursively!
## 17.7 ds_set and Binary Search Tree Implementation ## 18.7 ds_set and Binary Search Tree Implementation
- A partial implementation of a set using a binary search tree is provided in this [ds_set_starter.h](ds_set_starter.h). We will continue to study this implementation in Lab 10 & the next lecture. - A partial implementation of a set using a binary search tree is provided in this [ds_set_starter.h](ds_set_starter.h). We will continue to study this implementation in Lab 10 & the next lecture.
- The increment and decrement operations for iterators have been omitted from this implementation. Next week - The increment and decrement operations for iterators have been omitted from this implementation. Next week
@@ -116,7 +116,7 @@ in lecture we will discuss a couple strategies for adding these operations.
- We will use this as the basis both for understanding an initial selection of tree algorithms and for thinking - We will use this as the basis both for understanding an initial selection of tree algorithms and for thinking
about how standard library sets really work. about how standard library sets really work.
## 17.8 ds_set: Class Overview ## 18.8 ds_set: Class Overview
- There is two auxiliary classes, TreeNode and tree_iterator. All three classes are templated. - There is two auxiliary classes, TreeNode and tree_iterator. All three classes are templated.
- The only member variables of the ds_set class are the root and the size (number of tree nodes). - The only member variables of the ds_set class are the root and the size (number of tree nodes).
@@ -130,7 +130,7 @@ node) that does all of the work.
- Because the class stores and manages dynamically allocated memory, a copy constructor, operator=, and - Because the class stores and manages dynamically allocated memory, a copy constructor, operator=, and
destructor must be provided. destructor must be provided.
## 17.9 Exercises ## 18.9 Exercises
1. Provide the implementation of the member function ds_set<T>::begin. This is essentially the problem of 1. Provide the implementation of the member function ds_set<T>::begin. This is essentially the problem of
finding the node in the tree that stores the smallest value. finding the node in the tree that stores the smallest value.