CSE 100:
BSTS AND
Announcements
• PA 0 due tonight, PA1 out either late tonight or tomorrow
• Discussion section tonight. If all goes well, a demo of
Vocareum and bitbucket process.
• Vocareum has had a few bugs, but they are all resolved now… we
hope!
• PA1:
• Checkpoint due next Friday at 11:59pm (1/16)
• Final deadline Wednesday 1/21, 6pm
Pointers in C++
int a = 5;
int b = a;
int* pt1 = &a;
a: 5
b: 5
pt1:
References in C++
int main() {
int d = 5;
int & e = d;
int f = 10;
e = f;
How does the diagram change with this code?
}
d: d: 5
A. B.
e: 10
e:
f: 10 f: 10
d:
C. e: 10 D. Other or error
f:
Pointers and references. Which is the correct picture for this code?
A.
a: 5
int a = 5;
int & b = a;
int* pt1 = &a; b:
pt1:
B.
a: 5
C.
a: 5
b: 5 b:
pt1:
pt1: What are three ways
to change the value in
the box to 42?
References and const in C++
int main() {
int const d = 5;
int & e = d;
}
Does this code have an error? If so, why?
A. No, there is no error
B. Yes, there is an error because ints cannot be constant in C++
C. Yes, there is an error because a reference to a constant must also be declared
constant
References and const in C++
int main() {
int const d = 5;
const int & e = d;
}
C++, attempt 3:
class BSTNode { Imagine this code works (it doesn’t yet).
public: If it did, consider creating a new BSTNode
BSTNode* left; as follows:
BSTNode* right;
BSTNode* parent; int myInt = 42;
int const data; BSTNode* myNode = new BSTNode( myInt );
BSTNode( const int & d ) {
// body here
}
The code above in red specifies that d is passed by constant reference.
};
Which of the following diagrams best represents what that means?
A. C.
B.
myInt: myInt: 42 myInt: 42
d: 42
d is not allowed to change d: d: 42
what’s in its box
The address in d’s box d can’t change myInt
D. This code has an error can’t be changed because there’s no connection
C++ Pass-by-(const-)reference
Pass-by-reference Pass-by-value
BSTNode( const int & d ) { BSTNode( int d ) {
// body here // body here
} }
int myInt = 42; int myInt = 42;
BSTNode* myNode = BSTNode* myNode =
new BSTNode( myInt ); new BSTNode( myInt );
myInt: myInt:
d: 42 42
d: 42
In Java: C++, attempt 3:
public class BSTNode { class BSTNode {
public BSTNode left; public:
public BSTNode right; BSTNode* left;
public BSTNode parent; BSTNode* right;
public int data; BSTNode* parent;
int const data;
public BSTNode( int d )
{ BSTNode( const int & d ) {
data = d; data = d;
} }
}
};
Which of the following is a problem with the C++ implementation above?
A. Because data is a constant variable, the constructor will cause an error.
B. You cannot pass an integer by reference into a function. Integers must
be passed by value.
C. Since d is passed by reference, you cannot assign its value to data,
which is an int. You need to dereference it first.
D. The constructor needs a semi-colon at the end of its definition.
In Java: C++, attempt 3:
public class BSTNode { class BSTNode {
public BSTNode left; public:
public BSTNode right; BSTNode* left;
public BSTNode parent; BSTNode* right; Why make data const?
public int data; BSTNode* parent;
int const data;
public BSTNode( int d )
{ BSTNode( const int & d ) {
data = d; data = d;
} }
}
};
Which of the following is a problem with the C++ implementation above?
A. Because data is a constant variable, the constructor will cause an error.
C++, attempt 3:
class BSTNode { Imagine this code works (it doesn’t yet).
public: If it did, consider creating a new BSTNode
BSTNode* left; as follows:
BSTNode* right;
BSTNode* parent; int myInt = 42;
int const data; BSTNode* myNode = new BSTNode( myInt );
BSTNode( const int & d ) :
data(d) { }
};
How many copies of the int 42 are there in this code?
A. 1: myInt, d, and data all refer to the same memory location
B. 2: myInt and d refer to one memory location, but data has a separate
memory location with a copy of the value 42
C. 3: myInt, d and data all refer to different memory locations that store
the value 42
Why do it this way? Would other ways work?
In Java: C++, attempt 4:
public class BSTNode { class BSTNode {
public BSTNode left; public:
public BSTNode right; BSTNode* left;
public BSTNode parent; BSTNode* right;
public int data; BSTNode* parent;
int const data;
public BSTNode( int d )
{ BSTNode( const int & d ) :
data = d; data(d) { }
}
} };
Are there any remaining problems with this C++ implementation?
A. Yes
B. No
In Java: C++, attempt 5:
public class BSTNode { class BSTNode {
public BSTNode left; public:
public BSTNode right; BSTNode* left;
public BSTNode parent; BSTNode* right;
public int data; BSTNode* parent;
int const data;
public BSTNode( int d )
{ BSTNode( const int & d ) :
data = d; data(d) {
} left = right = parent = 0;
} }
};
ALWAYS initialize in C++. C++ won’t do it for you. Why not?
C++ and Templates
template<typename Data> Modify the code so that it uses
class BSTNode { the type parameter Data as the
public: type stored in the BST
BSTNode* left;
BSTNode* right;
BSTNode* parent;
int const data;
BSTNode( const int & d ) :
data(d) {
left = right = parent = 0;
}
};
Next time: C++ Iterators and BST analysis