c109f03t1

# c109f03t1 - CSE 109 Test 1 Wednesday 1 October 2003...

This preview shows pages 1–2. Sign up to view the full content.

CSE 109 Test 1 Wednesday 1 October 2003 >>>>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<<<<<<<,, 1. Assume that your function height(Node *rt) from programming assignment #1 works properly, returning -1 when rt==NULL and returning the height of the tree of which rt is the root node otherwise. Write the declaration and definition of a member function, balanced(), which returns true if and only if the tree is balanced. You may also want to define some other member function(s) that balance() calls. A tree is balanced if for every node the difference between the heights of the two children is -1, 0, or 1. Code using this function would look like: Tree a; .... .... if(a.balanced()) ..... ------------------------------ bool balanced(); bool balanced(Node *rt); b bool Tree::balanced() {return balanced(root);} { bool Tree::balanced(Node *rt) {int diff; if(rt==NULL) return true; diff=height(rt->child[Node::LEFT])-height(r->child[Node::RIGHT]); return diff>=-1 && diff<=1 && balanced(rt->child[Node::LEFT]) && balanced(rt->child[Node::RIGHT]); } ------------------------------ 2. Write a program whose only purpose is to display "good" when all the files

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern