Trees Terminology (continued) Traversals February 10, 2020 Cinda - - PowerPoint PPT Presentation

trees
SMART_READER_LITE
LIVE PREVIEW

Trees Terminology (continued) Traversals February 10, 2020 Cinda - - PowerPoint PPT Presentation

Trees Terminology (continued) Traversals February 10, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1 Measuring trees The height of a node v is the length of the longest path from v to a leaf The height of the tree is the height of


slide-1
SLIDE 1

Trees

Terminology (continued) Traversals

February 10, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1

slide-2
SLIDE 2

Measuring trees

  • The height of a node v is the length of the longest path from v

to a leaf

– The height of the tree is the height of the root

  • The depth of a node v is the length of the path from v to the

root

– This is also referred to as the level of a node

  • Note that there is a slightly different formulation of the height
  • f a tree

– Where the height of a tree is said to be the number of different levels of nodes in the tree (including the root)

Cinda Heeren / Andy Roth / Geoffrey Tien 2 February 10, 2020

slide-3
SLIDE 3

Tree measurements explained

Cinda Heeren / Andy Roth / Geoffrey Tien 3

A C F B D E H G I J Level 1 Level 2 Level 3 Height of tree is 3 Height of node B is 2 Depth of node E is 2

February 10, 2020

slide-4
SLIDE 4

Perfect binary trees

  • A binary tree is perfect, if

– No node has only one child – And all the leaves have the same depth

  • A perfect binary tree of height h has

– 2h+1 – 1 nodes, of which 2h are leaves

  • Perfect trees are also complete

Cinda Heeren / Andy Roth / Geoffrey Tien 4 February 10, 2020

slide-5
SLIDE 5

Height of a perfect tree

  • Each level doubles the number of nodes

– Level 1 has 2 nodes (21) – Level 2 has 4 nodes (22) or 2 times the number in Level 1

  • Therefore a tree with ℎ levels has 2ℎ+1 − 1 nodes

– The root level has 1 node

Cinda Heeren / Andy Roth / Geoffrey Tien 5

01 11 12 21 22 23 24 31 32 33 34 35 36 37 38 Bottom level has 2ℎ nodes, i.e. just over half

  • f the nodes are leaves

February 10, 2020

slide-6
SLIDE 6

Complete binary trees

  • A binary tree is complete if

– The leaves are on at most two different levels, – The second to bottom level is completely filled in and – The leaves on the bottom level are as far to the left as possible

Cinda Heeren / Andy Roth / Geoffrey Tien 6

It's not quite perfect, but almost

A C F B D E

February 10, 2020

slide-7
SLIDE 7

Binary tree traversal

  • A traversal algorithm for a binary tree visits each node in the

tree

– Typically, it will do something while visiting each node!

  • Traversal algorithms are naturally recursive
  • There are three traversal methods

– inOrder – preOrder – postOrder

Cinda Heeren / Andy Roth / Geoffrey Tien 7

It's recursive!

February 10, 2020

slide-8
SLIDE 8

inOrder traversal algorithm

Cinda Heeren / Andy Roth / Geoffrey Tien 8

void inOrder(Node* nd) { if (nd != nullptr) { inOrder(nd->leftchild); visit(nd); inOrder(nd->rightchild); } }

The visit function would do whatever the purpose

  • f the traversal is (e.g. print the data value of the

node).

February 10, 2020

slide-9
SLIDE 9

preOrder traversal

Cinda Heeren / Andy Roth / Geoffrey Tien 9

visit(nd);

1 2 3 4 5 6 7 8

preOrder(nd->leftchild); preOrder(nd->rightchild);

visit preOrder(left) preOrder(right) visit preOrder(left) preOrder(right) visit preOrder(left) preOrder(right) visit preOrder(left) preOrder(right) visit preOrder(left) preOrder(right) visit preOrder(left) preOrder(right) visit preOrder(left) preOrder(right)

17 13 9 11 16 27 20 39

February 10, 2020

slide-10
SLIDE 10

postOrder traversal

Cinda Heeren / Andy Roth / Geoffrey Tien 10

visit(nd);

8 4 2 1 3 7 5 6

postOrder(nd->leftchild); postOrder(nd->rightchild);

visit postOrder(left) postOrder(right)

17 13 9 11 16 27 20 39

visit postOrder(left) postOrder(right) visit postOrder(left) postOrder(right) visit postOrder(left) postOrder(right) visit postOrder(left) postOrder(right) visit postOrder(left) postOrder(right) visit postOrder(left) postOrder(right)

February 10, 2020

slide-11
SLIDE 11

Exercise

  • What will be printed by an in-order traversal of the tree?

– preOrder? postOrder?

February 10, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 11

visit(nd);

17 9 27 6 16 20 31 12

inOrder(nd->leftchild); inOrder(nd->rightchild);

39 Note to Geoff: show the trick with the dots!

slide-12
SLIDE 12

Not quite recursive

  • We have seen pre-order, in-order, post-order traversals
  • What about a traversal that visits every node in a level before

working on the next level?

– level-order traversal

February 10, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 12

Another type of tree traversal

41 33 87 21 74 36 45 78 25 Use some ADT to support this?

slide-13
SLIDE 13

What is "visiting"?

  • Some operation to be done at the current node

– counting, or arithmetic – creating a node (e.g. for a copy constructor) – deleting a node (e.g. for a destructor)

February 10, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 13

  • e.g. height

What is the height of this tree? What is the height of this tree? What is the height of this tree?

int Height(Node* nd) { if (nd == nullptr) // empty tree return _____; else return _______________________; }

Which type of traversal is this? Running time?

slide-14
SLIDE 14

Readings for this lesson

  • Carrano & Henry:

– Chapter 15.1 – 15.2 (Trees, tree traversals)

  • Next class:

– Carrano & Henry: Chapter 15.2, 16.1 – 16.2 (Tree traversals, implementations)

February 10, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 14