CS 225 Data Structures Fe February 21 Tr Trees G G Carl Evans - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures Fe February 21 Tr Trees G G Carl Evans - - PowerPoint PPT Presentation

CS 225 Data Structures Fe February 21 Tr Trees G G Carl Evans Binary T Bi Tree De Defin ined ed C A binary tree T is either: S X OR A 2 2 5 Tr Tree Property: height C height(T) : length of the longest path


slide-1
SLIDE 1

CS 225

Data Structures

Fe February 21 – Tr Trees

G G Carl Evans

slide-2
SLIDE 2

Bi Binary T Tree – De Defin ined ed

A binary tree T is either:

  • OR
  • A

X S 2 C 2 5

slide-3
SLIDE 3

Tr Tree Property: height

height(T): length of the longest path from the root to a leaf Given a binary tree T: height(T) =

A X S 2 C 2 5

slide-4
SLIDE 4

Tr Tree Property: full

A tree F is full if and only if: 1. 2.

A X S 2 C 2 5

slide-5
SLIDE 5

Tr Tree Property: perfect

A perfect tree P is defined in terms of the tree’s height. Let Ph be a perfect tree of height h, and: 1. 2.

A X S 2 C 2 5

slide-6
SLIDE 6

Tr Tree Property: complete

Conceptually: A perfect tree for every level except the last, where the last level if “pushed to the left”. Slightly more formal: For all levels k in [0, h-1], k has 2k nodes. For level h, all nodes are “pushed to the left”.

A X S 2 C 2 5 Y Z

slide-7
SLIDE 7

Tr Tree Property: complete

A complete tree C of height h, Ch:

  • 1. C-1 = {}
  • 2. Ch (where h>0) = {r, TL, TR} and either:

TL is __________ and TR is _________ OR TL is __________ and TR is _________

A X S 2 C 2 5 Y Z

slide-8
SLIDE 8

Tr Tree Property: complete

Is every full tree complete? If every complete tree full?

A X S 3 C 2 5 Y Z

slide-9
SLIDE 9

Op Open Of Office Hours

CS 225 has over 50 hours of open office hours each week, lots of time to get help!

slide-10
SLIDE 10

Op Open Of Office Hours

CS 225 has over 50 hours of open office hours each week, lots of time to get help!

  • 1. Understand the problem, don’t just give up.
  • “I segfaulted” is not enough. Where? Any idea why?
slide-11
SLIDE 11

Op Open Of Office Hours

CS 225 has over 50 hours of open office hours each week, lots of time to get help!

  • 2. Your topic must be specific to one function, one test

case, or one exam question.

  • Helps us know what to focus on before we see you!
  • Helps your peers to ensure all get questions answered!
slide-12
SLIDE 12

Op Open Of Office Hours

CS 225 has over 50 hours of open office hours each week, lots of time to get help!

  • 3. Get stuck, get help – not the other way around.
  • If you immediately re-add yourself, you’re setting

yourself up for failure.

slide-13
SLIDE 13

Op Open Of Office Hours

CS 225 has over 50 hours of open office hours each week, lots of time to get help!

  • 4. Be awesome.
slide-14
SLIDE 14

Tr Tree ADT

slide-15
SLIDE 15

Tr Tree ADT

insert, inserts an element to the tree. remove, removes an element from the tree. traverse,

slide-16
SLIDE 16

#pragma once template <class T> class BinaryTree { public: /* ... */ private: };

BinaryTree.h

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

slide-17
SLIDE 17

Tr Trees aren’t new:

C S X A 2 2 5 Y

Ø Ø Ø Ø Ø Ø Ø Ø Ø

slide-18
SLIDE 18

Tr Trees aren’t new:

A X S 2 C 2 5 Y

C S X A 2 2 5 Y

Ø Ø Ø Ø Ø Ø Ø Ø Ø

slide-19
SLIDE 19

Ho How many NUL ULLs?

Theorem: If there are n data items in our representation of a binary tree, then there are ___________ NULL pointers.

slide-20
SLIDE 20

Ho How many NUL ULLs?

Base Cases: n = 0: n = 1: n = 2:

slide-21
SLIDE 21

Ho How many NUL ULLs?

Induction Hypothesis:

slide-22
SLIDE 22

Ho How many NUL ULLs?

Consider an arbitrary tree T containing n data elements:

slide-23
SLIDE 23

Tr Traversals

*

  • b

+ / c d e a

slide-24
SLIDE 24

Tr Traversals

*

  • b

+ / c d e

template<class T> void BinaryTree<T>::__Order(TreeNode * root) { if (root != NULL) { ______________________; ___Order(root->left); ______________________; ___Order(root->right); ______________________; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

a

slide-25
SLIDE 25

Tr Traversals

*

  • b

+ / c d e

template<class T> void BinaryTree<T>::__Order(TreeNode * root) { if (root != NULL) { ______________________; ___Order(root->left); ______________________; ___Order(root->right); ______________________; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

a

slide-26
SLIDE 26

Tr Traversals

*

  • b

+ / c d e

template<class T> void BinaryTree<T>::__Order(TreeNode * root) { if (root != NULL) { ______________________; ___Order(root->left); ______________________; ___Order(root->right); ______________________; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

a