ece 242 data structures
play

ECE 242 Data Structures Lecture 17 Binary Trees October 19, 2009 - PDF document

ECE 242 Data Structures Lecture 17 Binary Trees October 19, 2009 ECE242 L17: Binary Trees Overview Problem: How do we represent non-linear structures? Binary Tree Similar to a linked list except each node has two children


  1. ECE 242 Data Structures Lecture 17 Binary Trees October 19, 2009 ECE242 L17: Binary Trees Overview ° Problem: How do we represent non-linear structures? ° Binary Tree • Similar to a linked list except each node has two children • Useful for many data representations ° Size of the binary tree often grows logarithmically • Many nice properties for searching and sorting ° We can represent the nodes as objects October 19, 2009 ECE242 L17: Binary Trees

  2. Basic Tree Structure root CEO VP of Engineering VP of Marketing VP of Sales … … Software Hardware … … … … … … leaves October 19, 2009 ECE242 L17: Binary Trees Tree ° Consist of nodes and edges (sometimes called lines or arcs) root ° Depicted upside down with the root at the top and leaves at bottom ° No cycle in the tree Leaves October 19, 2009 ECE242 L17: Binary Trees

  3. Terminology level 0 A B C D level 1 level 2 E F G level 3 H Parent: A, B, D, G Children: B, C, D, E, F, G, H Sibling: {B, C, D}, {E, F} Leaves: C, E, F, H October 19, 2009 ECE242 L17: Binary Trees Binary Tree ° A binary tree • root • left subtree • right subtree ° Each node has at most two D children ° Depth of node A = 2 B F ° Height of tree = 2 A C E G October 19, 2009 ECE242 L17: Binary Trees

  4. Representation Of a Tree ° Example for this tree: A B C D E F G first child A next sibling null H B C D null null E F G null null null null H null null October 19, 2009 ECE242 L17: Binary Trees Binary Trees ° The number of nodes in a binary tree depends on the height of the tree and on how “skinny” or “busy” the tree is. • Linear tree - Every internal node has only one child. • Perfect or Complete - All of the leaves are at the same depth. - Every internal node has exactly two children. October 19, 2009 ECE242 L17: Binary Trees

  5. Complete Binary Tree ° Complete Binary Tree (CBT) • Binary Tree • At level i , except last level, there are 2 i nodes • All nodes in the last level is as far left as possible ° Also called perfect binary trees October 19, 2009 ECE242 L17: Binary Trees Examples Of Binary Trees Which Are Not CBTs ° These are not CBTs October 19, 2009 ECE242 L17: Binary Trees

  6. Relationship Between CBT And Array ° Enumerate the nodes from top to down, from left to right, we get the array °array: a b c d e f g h i j °index: 0 1 2 3 4 5 6 7 8 9 0 a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i j 9 October 19, 2009 ECE242 L17: Binary Trees CBT To Array ° Given a CBT, we can easily get an array 3 ° CBT shown as left 10 23 ° Get the elements from top to bottom, from left to right 42 7 21 15 ° Then get Array 19 30 • { 3, 10, 23, 42, 7, 21, 15, 19, 30} October 19, 2009 ECE242 L17: Binary Trees

  7. Array To CBT ° Given an array, we can construct CBT ° Array: 3, 10, 23, 42, 7, 21, 15, 19, 30 ° Put the elements from top to bottom, from left to right 0 3 1 10 2 23 3 42 4 7 5 21 6 15 7 19 8 30 October 19, 2009 ECE242 L17: Binary Trees Index In Array ° Array: 3, 10, 23, 42, 7, 21, 15, 19, 30 ° Index: 0 1 2 3 4 5 6 7 8 0 3 index left child index right child index 1 0 1 2 10 2 23 1 3 4 3 42 4 7 5 21 6 15 2 5 6 7 19 8 30 3 7 8 … … … Left child index = 2*i+1 ? Right child index = i ??? ??? 2*i+2? October 19, 2009 ECE242 L17: Binary Trees

  8. CBT Properties ° Given the node with index i • parent’s index is (i-1)/2 • left child’s index is (2*i+1) • right child’s index is (2*i+2) ° total n nodes in CBT • height of CBT is � � � � log 2 (n + 1) � � � � ° You may prove these properties by yourself 0 3 1 10 2 23 3 42 4 7 5 21 6 15 7 19 8 30 October 19, 2009 ECE242 L17: Binary Trees Binary Tree Examples October 19, 2009 ECE242 L17: Binary Trees

  9. Binary Trees October 19, 2009 ECE242 L17: Binary Trees Summary ° Binary trees are very useful for many data representations ° The height of a CBT (or perfect binary tree) is O(log n), where n is the number of nodes in the tree ° CBTs can easily be converted to or implemented with arrays ° We will use binary trees for searching and sorting October 19, 2009 ECE242 L17: Binary Trees

Recommend


More recommend