lecture16

lecture16 - Wednesday,May31 st CompleteBinaryTrees...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Representing Binary Trees Using Arrays Complete Binary Trees Priority Queues Heaps… of what? The Heap Sort Review Challenge Wednesday, May 31 st  
Background image of page 1

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

View Full DocumentRight Arrow Icon
Representing Binary Trees As we’ve seen, you can represent a binary tree with a linked-list data  structure.  You can also represent a binary tree with an  array ! 1. The root of the binary tree goes in  array[0] 1. If the data for a node appears in  array[i] , its children, if they exist, are in  these locations: Left child array[2i+1] Right child array[2i+2] 1. If the data for a non-root node is in  array[i] , then its  parent  is always at  array[(i-1)/2]   (Use integer division) 4. You can store an invalid value like  ‘?’  or  -1  in non-existent nodes to mark  them as unused.
Background image of page 2
Binary Trees in Arrays 1. The root of the tree goes in  array[0] 1. If a node is in  array[i] , its children, if they exist,  are in: Left child array[2i+1] Right child array[2i+2] 1. If a non-root node is in  array[i] , then its  parent  is  always at  array[(i-1)/2]   (Use integer division) 4. You can store an invalid value like  ‘?’  or  -1  to mark  a slot as unused. “carey” “leon” “andrea” NULL NULL “simon” NULL NULL array [0] [1] [2] [3] [4] [5] [6] [7] “carey” “leon” “simon” “milton” NULL NULL So where does  Carey  go? Where does  Leon  go? Leon’s  parent  is in slot  0 . Leon is the  left child . So Leon goes in slot  2*0+1 . Where does  Simon  go? Simon’s  parent  is in slot  1 . Simon is the  left child . So Simon goes in slot  2*1+1 .
Background image of page 3

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

View Full DocumentRight Arrow Icon
Binary Trees in Arrays 1. The root of the tree goes in  array[0] 1. If a node is in  array[i] , its children, if they exist,  are in: Left child array[2i+1] Right child array[2i+2] 1. If a non-root node is in  array[i] , then its  parent  is  always at  array[(i-1)/2]   (Use integer division) 4. You can store an invalid value like  ‘?’  or  -1  to mark  a slot as unused. “carey” “leon” “andrea” NULL NULL “simon” NULL NULL array [0] [1] [2] [3] [4] [5] [6] [7] “carey” “leon” “simon”   “”   “” “” “andrea” “milton” NULL NULL Where does  Andrea  go? Andrea’s  parent  is in slot  0 . Andrea is the  right child . So Andrea goes in slot  2*0+2 . “milton” Where does  Milton  go? Milton’s  parent  is in slot 2. Milton is the  left child . So Milton goes in slot  2*2+1 .
Background image of page 4
Building Trees with Arrays class BinaryTree {    BinaryTree()       { num = 0;  }         int GetRootIndex()      { return(0); }    int LeftChildLoc(int i)  { return(2*i+1); }    int RightChildLoc(int i) { return(2*i+2); }    int ParentLoc(int i)       { return((i-1)/2); }    int PrintVal(int i)          { cout << a[i]; }
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/20/2009 for the course CS 32 taught by Professor Davidsmallberg during the Winter '08 term at UCLA.

Page1 / 27

lecture16 - Wednesday,May31 st CompleteBinaryTrees...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online