10b - TreesSlides

# 10b TreesSlides

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

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

Unformatted text preview: push 6 2: push 2 *: pop 2 and 6. Evaluate 6 * 2. Push 12. 7: push 7 +: pop 7 and 12. Evaluate 12+7. Push 19. 8: push 8 3: push 3 *: pop 3 and 8. Evaluate 8*3. Push 24. +: pop 24 and 19. Evaluate 19+24. Push 43. 6: push 6 7: push 7 /: pop 7 and 6. Evaluate 6/7. Push 0. -: pop 0 and 43. Evaluate 43-0. Push 43. END. 43- For a perfectly balanced tree, we know exactly how large an array we need. Here's a completely filled balanced tree. 1 2 4 8 To store a tree in an array, we provide sufficient space for the largest tree. 16 0 Thus, level k (k=0,1,2,...) has 2k nodes. A tree of height k would need 2k - 1 nodes. We store these nodes in consecutive elements of the array – from top to bottom, left to right. Array[0] is a. Its children b and c are c b in Array[1] and Array[1] g f e d Array[2] Array[2]. d,e,f & g h i j kl m n o are in Array[3..6]. Array[3..6] a 0 4 3 7 8 9 10 11 6 5 12 13 7 8 6 5 4 9 10 11 12 13 14 Array[0] is a. Its children b and c are c b in Array[1] and Array[1] g f e d Array[2] Array[2]. d,e,f & g h i j kl m n o a...
View Full Document

## This document was uploaded on 04/07/2014.

Ask a homework question - tutors are online