Binary Trees Review From CSCI-1321 Data Abstractions CSCI-2320 - - PowerPoint PPT Presentation

binary trees
SMART_READER_LITE
LIVE PREVIEW

Binary Trees Review From CSCI-1321 Data Abstractions CSCI-2320 - - PowerPoint PPT Presentation

Binary Trees Review From CSCI-1321 Data Abstractions CSCI-2320 Dr. Tom Hicks Computer Science Department Root Root Node - A Node Without A Parent. Root Root 3 Leaf Leaf Node - A Node Without Children. Leaf Leaf 5 Ancestors {D,11} -


slide-1
SLIDE 1
  • Dr. Tom Hicks

Computer Science Department

Binary Trees

Review From CSCI-1321

Data Abstractions

CSCI-2320

slide-2
SLIDE 2
slide-3
SLIDE 3

Root

3

Root Root

Root Node - A Node Without A Parent.

slide-4
SLIDE 4
slide-5
SLIDE 5

Leaf

5

Leaf Node - A Node Without Children.

Leaf Leaf

slide-6
SLIDE 6
slide-7
SLIDE 7

Ancestors

7

{D,11} - Nodes In The Path(s) To The Root. B, F  Ancestors Of D 5, 2, 1  Ancestors Of 11

slide-8
SLIDE 8
slide-9
SLIDE 9

Descendants

9

{B,6} - Nodes In The Path(s) To The Leaves. A,D,C,E  Descendants Of B 12,13,24,25,26,27  Descendants Of 6

slide-10
SLIDE 10
slide-11
SLIDE 11

Skew Trees

Skew Tree - A Tree With No More Than 1 Node At

Each Level.

11

A Skew Tree Has The Same Performance As A Linked List!

Empty Tree

slide-12
SLIDE 12
slide-13
SLIDE 13

Full/Perfect Binary Trees

Full Tree - A Tree In Which The Lowest Level Is

Fully Populated Every Node, Other Than The Leaves, Has Two Children.

13

Best Case Tree! Also Called A "Perfect" Tree

Empty Tree

slide-14
SLIDE 14
slide-15
SLIDE 15

Height Of Tree

Height Of A Tree Is The Number Of Edges In The Longest Path From The Root To A Leaf Node

15

Height

Height = 5

slide-16
SLIDE 16
slide-17
SLIDE 17

No Levels On A Tree

Some Start With 0  Others With 1  WE ARE GOING TO USE 1

17

Height

Level       1 Level      2 Level    3 Level   4 Level   5 Level          6 Height = 5

slide-18
SLIDE 18
slide-19
SLIDE 19

Perfect/Full Trees  NL  No Levels - 1 Max Nodes  Perfect Tree  NL = 2

19

2NL - 1 = 22 - 1 = 3 2NL - 1 = 23 - 1 = 7

slide-20
SLIDE 20

Perfect/Full Trees  NL  No Levels - 2 Max Nodes  Perfect Tree  NL = 4

20

2NL - 1 = 24 - 1 = 15 Height Of Perfect Binary Tree With 15 Nodes = Log2(15) = ~4

slide-21
SLIDE 21

Perfect/Full Trees  NL  No Levels - 3

21

Max Nodes  Perfect Tree  NL = 5

2NL - 1 = 25 - 1 = 31 Height Of Perfect Binary Tree With 15 Nodes = Log2(31) = ~5

slide-22
SLIDE 22
slide-23
SLIDE 23

Average Search = Total Searches / No Nodes

23

Compute # Searches

1 2 2 3 3 3 4 4 4         1      4     9      12

26 ___

26/9 = 2.89

slide-24
SLIDE 24
slide-25
SLIDE 25

Balanced Binary Tree

A Balanced Tree is one that cannot be expressed in fewer levels; it will have the lowest Average Search for that number of Nodes.

25

slide-26
SLIDE 26

Inorder Traversal

Left  Root  Right  There Are Recursive & Non-Recursive Solutions!

26

Preorder Traversal

Root  Left  Right  There Are Recursive & Non-Recursive Solutions!

Postorder Traversal

Left  Right  Root  There Are Recursive & Non-Recursive Solutions!

slide-27
SLIDE 27

Recursive Solutions Are Quite Easy To Code

Traverse The Left Visit The Root Traverse The Right

27

slide-28
SLIDE 28

Inorder Traversal

D  B  H  E  I  A  F  C  G

28

slide-29
SLIDE 29

Inorder Traversal

D  B  H  E  I  A  F  C  G

29

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

slide-30
SLIDE 30

Preorder Traversal

A  B  D  E  H  I  C  F  G

30

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

slide-31
SLIDE 31

Postorder Traversal

D  H  I  E  B  F  G  C  A

31

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

slide-32
SLIDE 32

Data Abstractions

CSCI 2320

  • Dr. Thomas E. Hicks

Computer Science Department Trinity University

Textbook: Introduction To Algorithms 3rd Edition By Cormen, Leiserson, Rivest, Stein Textbook: A Tour Of C++ By Bjarne Stroustrup

Special Thanks To For MIT Press & Adison Wesley For Content & Graphics That Are Relevant To This Presentation.

32

slide-33
SLIDE 33

Average Search = Total Searches / No Nodes

100 50 150 25 75 125 175 40 25 40 50 75 100 125 150 175