Tree terminology refresh height(G)=2 A longest path Depth of node - - PowerPoint PPT Presentation

tree terminology refresh
SMART_READER_LITE
LIVE PREVIEW

Tree terminology refresh height(G)=2 A longest path Depth of node - - PowerPoint PPT Presentation

Tree terminology refresh height(G)=2 A longest path Depth of node X: number of edges on path from root to node B C X Height of node X: number of D E F G edges on longest path from X to a leaf height(A)=height(tree)=4 H I


slide-1
SLIDE 1

Tree terminology refresh

  • Depth of node X: number of

edges on path from root to node X

  • Height of node X: number of

edges on longest path from X to a leaf

  • Height of tree is height of root
  • If a tree contains n nodes how

many edges does it contain?

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

1 height(empty tree)=-1 n - 1 because every node except the root has a parent and there must be an edge between parent and child height(G)=2 height(A)=height(tree)=4 longest path

slide-2
SLIDE 2

d-ary trees

d-ary: Every node has at most d children (binary d = 2) Recursive definition:

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

full d-ary tree: every node has 0 or d nodes perfect d-ary tree: maximum nodes for height (all non-leafs have d children and leafs have 0) complete d-ary tree: each level except possibly deepest filled all nodes in deepest level as far to left as possible

2 A d-ary tree is either an empty tree OR A root (node) with at most d child trees which are d-ary trees themselves. Aside: If we consider empty trees we could say every node has exactly d children, some empty some "real" full perfect full

This will make more sense when we see the implementation

slide-3
SLIDE 3

Recursive trees

Recursive definition of height:

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

Number of nodes in a perfect binary tree of height h?

3 height(empty)=-1

TL

height(T)=1 + height( ) + height( )

TR TL TR

h=0 h=1 h=2

1 3 7

h=k

2(k+1) − 1

Base case: N(0)=1 Proof: Let N(h) be the number of nodes in a perfect tree of height h. Recursion: N(h) = 2N(h - 1) + 1 Induction hypothesis: N(k) = 2^(k+1) - 1 for all k < h Because we have root plus two children that are perfect. By I.H. and recursion: N(h)=2N(h-1)+1 =2 (2^(h-1+1) - 1) + 1 =2 (2^h) - 2 + 1 =2^(h+1) - 1

slide-4
SLIDE 4

Counting empty trees

How many empty trees hang off a binary tree of heigh h?

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

4

with n nodes?

Thus there are n+1 empty children Every node has 2 children "real" or empty. So there are 2n real or empty children in the tree. Every real node except the root is a child => n - 1 real children Proof: By induction next class.

Empty children

You can think of empty nodes/trees as NULL pointers.

slide-5
SLIDE 5

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; ...}; 5

slide-6
SLIDE 6

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); }} 6