Another tree example Phylogenetic tree Patient 1 Plan Clone - - PowerPoint PPT Presentation

another tree example
SMART_READER_LITE
LIVE PREVIEW

Another tree example Phylogenetic tree Patient 1 Plan Clone - - PowerPoint PPT Presentation

Another tree example Phylogenetic tree Patient 1 Plan Clone Phylogeny B C RFTA16 Om1 Tree ADT A E ROv1 SBwl D G Tree recursion F H I Tree traversal ROv2 SBwlE4 Search and score n Search runtime O(2 ) Pass message


slide-1
SLIDE 1

Another tree example

Plan

  • Tree ADT
  • Tree recursion
  • Tree traversal

ROvA4 ROv4 ROv3 ROv2 ROv1 RFTA16 Om1 SBwl SBwlE4 LFTB4 LOvB2 ApC1 Clone Phylogeny

Patient 1

A B C D E F G H I

1

Search and score Search runtime O(2 ) Pass message from leafs to root recursively Traverse tree is post-order!!

n

Phylogenetic tree

slide-2
SLIDE 2

Ordered binary tree ADT

  • Ordered binary tree node can

have left and right children

  • Recursively: either empty or

a root with left and right children which are ordered binary trees

Tree ADT

  • Insert node
  • Remove node
  • Traverse

BEE EMU ANT CAT DOG

  • o
  • o

template <class T> class tree{ public: ... private: struct Node{ T data; Node * left; Node * right;}; Node * root; ...}; 2

Like head for linked list We could add a parent pointer to Node to traverse up the tree

Empty node = NULL pointer

slide-3
SLIDE 3

Counting empty trees

How many empty trees hang off a binary tree with n nodes?

A B C D E F G H I J K L

3 Each node has 2 children => 2n nodes real or empty n real nodes only root not child => n-1 real nodes So there are n+1 empty nodes Proof by induction: Let E(n) be # empty nodes in tree w/ n nodes E(0) = 1 Empty tree has single empty node E(n)=E(n )+E(n )

L R

n = #nodes in T n = #nodes in T

R L

n + n = n - 1

L R

by I.H. E(n) = (n + 1) + (n + 1)

L R

= (n + n ) + 2 = (n - 1) + 2 = n + 1

L R L R

slide-4
SLIDE 4

Tree traversal

10 5 15 2 9 20 7 17 30

In order: 2, 5, 9, 7, 10, 15, 17, 20, 30

inOrderTraversal(Node * n){ if(n != NULL){ inOrderTraversal(n->left); inOrderTraversal(n->right); }} 4 Left Right cout << n->data << endl; in pre post

Pre 10, 5, 2, 9, 7, 15, 20, 17, 30 Post 2, 7, 9, 5, 17, 30, 20, 15, 10 Runtime O(n) T(n)=T(n ) + T(n )+

L R

T(0)=

b b b b b

b c

c c c c Pay c at each of n nodes Pay b at each of n+1 empty nodes

T(n)=cn+b(n+1) by I.H. T(n)=b(n + n + 2) + c(n + n )+c

L L R R

n + n = n - 1

R L

slide-5
SLIDE 5

Eulerian tree tour

a b c e d f h i j Pre In Post T

L

T

R

a b c e d f h i j a b c e d f h i j a b c e d f h i j

5

Pre In Post a,b,d,f,c,e,i,j,h d,b,f,a,c,i,j,e,h d,f,b,j,i,h,e,c,a

slide-6
SLIDE 6

Applications

  • Tree copy
  • Tree delete

6 Node * treeCopy(Node * n){ if(n!=NULL){ return new Node(n->data, treeCopy(n->left), treeCopy(n->right)) } } void treeDelete(Node * n){ if (n == NULL) return; treeDelete(n->left); treeDelete(n->right); delete n; }

slide-7
SLIDE 7

Level (depth) order

a b c e d f h i j

1 2 3 4

void printLevelOrder(){ if (root == NULL) return; }

7

Output: a, b, c, d, f, e, i, h, j, Queue<Node *> Q; Q.enqueue(root); while (!Q.empty()){ Node * n = Q.dequeue(); if (n == NULL) continue; cout << n->data << ","; Q.enqueue(n->left); Q.enqueue(n->right); } a b c d f e Q 1 1 2 2 2 How to show correct? Loop invariant

slide-8
SLIDE 8
slide-9
SLIDE 9