lecture16 - Wednesday,May31 st CompleteBinaryTrees...

Info icon This 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  
Image of page 1

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

View Full Document Right 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.
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 .
Image of page 3

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

View Full Document Right 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 .
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]; }
Image of page 5

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern