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

ece 242 data structures
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

ECE242 L17: Binary Trees October 19, 2009

ECE 242 Data Structures

Lecture 17

Binary Trees

ECE242 L17: Binary Trees October 19, 2009

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

slide-2
SLIDE 2

ECE242 L17: Binary Trees October 19, 2009

Basic Tree Structure CEO

VP of Engineering VP of Sales

Hardware

VP of Marketing

… Software … … … … … … …

root leaves

ECE242 L17: Binary Trees October 19, 2009

Tree °Consist of nodes and edges (sometimes called lines or arcs) °Depicted upside down with the root at the top and leaves at bottom °No cycle in the tree Leaves root

slide-3
SLIDE 3

ECE242 L17: Binary Trees October 19, 2009

Terminology Parent: A, B, D, G Children: B, C, D, E, F, G, H Sibling: {B, C, D}, {E, F} Leaves: C, E, F, H A B D F G H E C level 0 level 1 level 2 level 3

ECE242 L17: Binary Trees October 19, 2009

Binary Tree ° A binary tree

  • root
  • left subtree
  • right subtree

° Each node has at most two children ° Depth of node A = 2 ° Height of tree = 2 D F B G E C A

slide-4
SLIDE 4

ECE242 L17: Binary Trees October 19, 2009

Representation Of a Tree °Example for this tree: A B D F G H E C A null

first child next sibling

B E null H null null C null D null F null null G null

ECE242 L17: Binary Trees October 19, 2009

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.
slide-5
SLIDE 5

ECE242 L17: Binary Trees October 19, 2009

Complete Binary Tree °Complete Binary Tree (CBT)

  • Binary Tree
  • At level i, except last level, there are 2i nodes
  • All nodes in the last level is as far left as possible

°Also called perfect binary trees

ECE242 L17: Binary Trees October 19, 2009

Examples Of Binary Trees Which Are Not CBTs °These are not CBTs

slide-6
SLIDE 6

ECE242 L17: Binary Trees October 19, 2009

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 b a c i h g d f e j 1 2 3 4 5 6 7 8 9

ECE242 L17: Binary Trees October 19, 2009

CBT To Array

° Given a CBT, we can easily get an array ° CBT shown as left ° Get the elements from top to bottom, from left to right ° Then get Array

  • { 3, 10, 23, 42, 7, 21, 15, 19, 30}

10 3 23 30 19 15 42 21 7

slide-7
SLIDE 7

ECE242 L17: Binary Trees October 19, 2009

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 10 3 23 30 19 15 42 21 7 1 2 3 4 5 6 7 8

ECE242 L17: Binary Trees October 19, 2009

Index In Array °Array: 3, 10, 23, 42, 7, 21, 15, 19, 30 °Index: 0 1 2 3 4 5 6 7 8 10 3 23 30 19 15 42 21 7 1 2 3 4 5 6 7 8

index left child index right child index

1 2 1 3 4 2 5 6 3 7 8 … … … i ??? ???

Left child index = 2*i+1 ? Right child index = 2*i+2?

slide-8
SLIDE 8

ECE242 L17: Binary Trees October 19, 2009

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
  • log2(n + 1)
  • °You may prove these properties by yourself

10 3 23 30 19 15 42 21 7 1 2 3 4 5 6 7 8

ECE242 L17: Binary Trees October 19, 2009

Binary Tree Examples

slide-9
SLIDE 9

ECE242 L17: Binary Trees October 19, 2009

Binary Trees

ECE242 L17: Binary Trees October 19, 2009

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