For the node at index i The parent is at index i 1 2 rounded down to an integer

For the node at index i the parent is at index i 1 2

• 6

This preview shows page 3 - 6 out of 6 pages.

For the node at index i , • The parent is at index ( i - 1) / 2 (rounded down to an integer). • The left child is at index 2 i + 1 . Data Structures and Algorithms Page 3 of 6
COMP2123/2823/9123 • The right child is at index 2 i + 2 . These operations assume that the backing array uses zero-based indexing, i.e. for an array with n elements, elements are located at indices 0 , 1 , 2 , ..., n - 1 . Different formulae are needed for one- based indexing. 1.6 Paper Exercise: In-Array Heap Properties Which of the following arrays is a valid heap? For each valid heap, draw the corresponding linked tree representation. (1, X) (2, L) (1, I) (4, T) (3, X) (5, R) (2, Z) (2, H) (4, L) (3, I) empty (6, X) (8, T) empty (1, Y) (2, Y) (6, J) (8, N) (3, B) (5, Q) empty Attempt this exercise yourself on paper, then solve the exercise collaboratively with your tutor. 1.7 Paper Exercise: Operations on an In-Array Heap Carry out the following sequence of operations on the heap given below: 1 insert(1, J) 2 removeMin() 3 insert(3, G) (4, F) (5, W) (9, A) (7, B) (6, D) empty empty Attempt this exercise yourself on paper, then solve the exercise collaboratively with your tutor. 1.8 Exercise: Validate That an Array Encodes a Binary Tree In this exercise you will write a routine which determines whether an input array encodes a valid binary tree. You will need to ensure that, in the tree encoded by the input array, every non-root node has a parent. Complete this exercise in the workspace provided in the Challenges section on Ed. You need to implement the isBinaryTree() function. Data Structures and Algorithms Page 4 of 6
COMP2123/2823/9123 1.9 Exercise: Validate That an Array Encodes a Complete Binary Tree In this exercise you will write a routine which determines whether an input array encodes a complete binary tree, i.e. a binary tree where every layer is except the deepest one is full, and the deepest layer is filled left-to-right with no gaps. Complete this exercise in the workspace provided in the Challenges section on Ed. You need to implement the isCompleteBinaryTree() function. You should copy over your code from the previous exercise. Hint: it’s a lot easier to validate this by iterating over the array, than by recursion on the tree structure. 1.10 Assessed Exercise: Validate That an Array Encodes a Heap [Due: Noon on Wednesday April 25] [Value: 1 point] In this exercise you will write a routine which determines whether an input array encodes a binary min-heap, i.e. a complete binary tree which satisfies the heap order property.

You've reached the end of your free preview.

Want to read all 6 pages?

• Three '18
• Binary heap

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