{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# Binary Search Trees Answers - 1 2 3 4 5 6 7 The BST that...

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

Answers to Self-Study Questions Test Yourself #1 Question 1 (1) (2) (3) (4) A 10 cat 15 / \ / / \ / \ B C 5 bat rat 5 22 / / \ \ -3 ant 20 30 Tree 1 is not a BST because B is greater than A, yet B is in the left subtree of the node with key A (all keys in a node's left subtree should have keys that are less than the key in that node). Tree 4 is not a BST because 20 is greater than 15, yet 20 is in the left subtree of the node with key 15. Question 2 An inorder traversal of a BST visits the nodes in sorted order. Test Yourself #2 The BST that results from insertingn the values 5 3 7 6 2 1 4 in that order is: 5 / \ 3 7 / \ / 2 4 6 / 1 The BST that results from insertingn the values 1 2 3 4 5 6 7 in that order is:

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.

Unformatted text preview: 1 \ 2 \ 3 \ 4 \ 5 \ 6 \ 7 The BST that results from insertingn the values 4 3 5 2 6 1 7 in that order is: 4 / \ Page 1 of 2 BST Answers 2008/3/27 http://pages.cs.wisc.edu/~cs367-1/topics/Binary-Search-Trees/answers.html 3 5 / \ 2 6 / \ 1 7 Test Yourself #3 private static BinaryTreenode<K> smallestNode(BinaryTreenode<K> n) // precondition: n is not null // postcondition: return the node in the subtree rooted at n that // has the smallest value { if (n.left == null) { return n; } else { return smallestNode(n.left); } } Page 2 of 2 BST Answers 2008/3/27 http://pages.cs.wisc.edu/~cs367-1/topics/Binary-Search-Trees/answers.html...
View Full Document

{[ snackBarMessage ]}