Lec02-BSTAndC++ After
Lec02-BSTAndC++ After
BSTS AND
Announcements
• Discussion sections start today
• See course website for time and location
• Topic: Git and Vocareum
• IMPORTANT: Do Friday’s reading BEFORE coming to discussion
• Quia quizzes
• Take the pretest through your login (if it didn’t ask you to login, take
it again)
• Be careful with your attempts!! Attempts start when you open the
quiz. You only get a limited number (usually 4 or 5).
• Contact Qi or post on Piazza if you need a reset for pretest only
• Early Research Scholars Program now accepting
applications. See note on Piazza.
Announcements (2)
• We have 2 new textbooks for this course! Both are
electronic and online through ZyBooks.com. Reading
assignments will be given from the required one below.
• Required: UCSD CSE 100 Winter 2015: Data Structures Beta
Cost: FREE!
Instructions for student access:
1. Sign up at zyBooks.com
2. Enter zyBook code: UCSDCSE100Winter2015
3. Click 'Subscribe‘
• Optional (but encouraged for practice with C++): UCSD CSE 100
Winter 2015: Programming in C++
Cost: A small fee (around $25 I think) for access for the quarter
Instructions for student access:
1. Sign up at zyBooks.com
2. Enter zyBook code: UCSDCSE100CppWinter2015
3. Click 'Subscribe'
Goals for today
• Draw memory model diagrams for C++ pointers and
references
• Explain C++ code for implementing binary search trees
• Explain pass-by-reference and constants in C++
Clickers, Peer Instruction, and PI Groups
C.
D. A & B
E. All of A-C
Which of the following is/are a binary
search tree?
C.
A. 42 B. 42
42
32 12 32 12 32 65
12
30 38
D.
42
32 56
E. More than one of these
12 45
BST Operations
• Your first Programming Assignment (out Friday) will ask
you to implement:
• Find
• Insert
• Size
• An Iterator
• (A few other methods)
• We will assume that you have already seen these
operations and/or can learn them from the reading. We
will not explicitly cover (most of) them in class.
Today’s topic: C++
int a = 5;
int b = a;
int* pt1 = a;
512000 5 a
512004 5 b
a: 5
b: 5
pt1:
Pointers in C++
class MyClass {
private:
int a;
public:
void setA(int a) { this->a = a; }
int getA() { return a; }
};
x:
a: 5
Pointers in C++
int main() {
C* x; // declare x to be a pointer to a C object
x = new C(); // create a C object, and make x point to it
x->setA(5); // dereference x, and access a member
// note: (*x).setA(5) is equivalent
C* y = x;
} Which represents the new diagram?
x: x:
A. B. y:
a: 5 a: 5
y:
x:
C. a: 5 D. The line in red causes an error
y:
References in C++
int main() {
int d = 5;
int & e = d;
}
Which diagram represents the code above?
d: 5 d: 5
A. B.
e: 5
e:
d:
C. e: 5 D. This code causes an error
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: