Dale - Computer Science Illuminated 336

Dale - Computer Science Illuminated 336 - Delete algorithm,...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
9.7 Trees 309 8 Print((null)) (null) = null Return to 1, finished calls 6 and 2 1 Print N, Print right subtree 9 Print((S)) (S) != null Print the left subtree 10 Print((null)) (null) = null Return to 9 9 Print S, Print right subtree 11 Print((null)) (null) = null Return to original call, finished calls 9 and 1 This algorithm prints the items in the binary search tree in ascending value order. There are other traversals of the tree that print the items in other orders. We explore them in the exercises. Other Operations By now you should realize that a binary search tree is an object with the same functionality as a list. The behavior that separates a binary search tree from a simple list is the efficiency of the operations; the behaviors are the same. We have not shown the
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Delete algorithm, because it is too complex for this text. We have also ignored the concept length that must accompany the tree if it is to be used to implement a list. Rather than keep track of the number of items in the tree as we build it, lets write an algo-rithm to count the number of nodes in the tree. How many nodes are there in an empty tree? Zero. How many nodes are there in any tree? One plus the number of nodes in the left subtree and the number of nodes in the right subtree. This definition leads to a recur-sive definition of the length operation. Length(tree) If (tree is null) return 0 Else return Length(left(tree)) + Length(right(tree)) + 1...
View Full Document

Ask a homework question - tutors are online