0% found this document useful (0 votes)
22 views23 pages

Lec02-BSTAndC++ After

Uploaded by

profiledmale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views23 pages

Lec02-BSTAndC++ After

Uploaded by

profiledmale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

CSE 100:

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

Initial groups (free seating): Weeks 1-2


Second groups: Weeks 3-6
Final groups: Weeks 7-10
Clickers, Peer Instruction, and PI Groups

Initial groups (free seating): Weeks 1-2


Second groups: Weeks 3-6
Final groups: Weeks 7-10
Academic Integrity Scenario
You just finished taking the first CSE 100 midterm. Your friend is
in the other section of CSE 100, and she will take her midterm in
an hour. She asks you how the midterm went. You say,
1: “It wasn’t too bad as long as you’ve studied the clicker
questions and done the programming assignments”
2: “It wasn’t too bad, but there was a tricky question that asks
you to prove the average case running time to insert into a
binary search tree”
Which of these violates the Academic Integrity Policy for CSE
100?
A. Neither, both are fine
B. 1 is not OK, with 2 is fine
C. 2 is not OK, but 1 is fine
D. Both are not OK
Integrity Guidelines
Basic rules
• Do not look at or copy other people’s code and do not share your
code with others (other than your partner). Period.
• “Other people” includes what you can find/share on the internet.
• Read the Integrity Statement carefully. Ask if you have questions.
Integrity
• You will be tested on your ability to understand and write code for
data structures in this class (and invariably during interviews)
• Cheaters will likely get “caught” during the exam because exams,
for the most part, make your grade in this class.
• Why else shouldn’t you cheat?
• Its unethical
• Its unfair to students who do the work legitimately
• Hurts the reputation of the UCSD CSE degree
Which of the following is/are a tree?
A. B.

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++

C++’s main priority is getting correct programs to run as fast as it


can; incorrect programs are on their own.

Java’s main priority is not allowing incorrect programs to run;


hopefully correct programs run reasonably fast, and the language
makes it easier to generate correct programs by restricting some
bad programming constructs.

-- Mark Allen Weiss, C++ for Java Programmers

Why C++ for data structures?


C++, attempt 1:
In Java:
class BSTNode {
public class BSTNode {
public BSTNode left;
public BSTNode left;
public BSTNode right;
public BSTNode right;
public BSTNode parent;
public BSTNode parent;
public int data;
public int data;
public BSTNode( const int & d ) {
public BSTNode( int d )
data = d;
{
}
data = d;
}
};
}

Which of the following is a problem with the C++ implementation above?


A. You should not declare the types of your variables in C++
B. The class BSTNode should be declared public
C. The semicolon at the end of the class will cause a compile error
D. In C++ you specify public and private in regions, not on each variable or function
In Java: C++, attempt 2:

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 data;
public BSTNode( int d )
{ public BSTNode( const int & d ) {
data = d; data = d;
} }
}
};
What is the problem with how we have declared left, right and parent above?
A. They should be BSTNode* (pointers to BSTNodes) and not BSTNode
type.
B. They should be declared to be const
C. They should be declared as BSTNode& (reference variables).
Pointers in C++

Which of the following statements is true about this code?

int a = 5;
int b = a;
int* pt1 = a;

A. Both pt1 and b can be used to change the value of a.


B. Only pt1 can be used to chance the value of a.
C. This code causes a compile error.
Pointers in C++
int a = 5;
int b = a;
int* pt1 = &a;

address memory cell identifier

512000 5 a

512004 5 b

512008 512000 pt1


Pointers in C++
int a = 5;
int b = a;
int* pt1 = &a;

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; }
};

What will the line


MyClass c;
do?
A. Declare a variable of type MyClass, but not create an object
B. Declare a variable of type MyClass and create an object of type MyClass
C. Declare a variable of type pointer to MyClass, but not create an object
D. Declare a variable of type pointer to MyClass, and create an object of type
MyClass
Pointers in C++
class C {
private:
int a;
public:
void setA(int a) { this->a = a; }
int getA() { return a; }
};

What will the line


C* c;
do?
A. Declare a variable of type C, but not create an object
B. Declare a variable of type C and create an object of type C
C. Declare a variable of type pointer to C, but not create an object
D. Declare a variable of type pointer to C, and create an object of type C
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
}

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:

You might also like