30: General and Binary Trees Chris Wyatt Electrical and Computer - - PowerPoint PPT Presentation

30 general and binary trees
SMART_READER_LITE
LIVE PREVIEW

30: General and Binary Trees Chris Wyatt Electrical and Computer - - PowerPoint PPT Presentation

ECE 2574 Introduction to Data Structures and Algorithms 30: General and Binary Trees Chris Wyatt Electrical and Computer Engineering Trees are non-linear, value oriented structures List Holding 7 Items A B C D E F G A B C Example of


slide-1
SLIDE 1

ECE 2574 Introduction to Data Structures and Algorithms 30: General and Binary Trees

Chris Wyatt Electrical and Computer Engineering

slide-2
SLIDE 2

Trees are non-linear, value oriented structures

A B C D E F G A B C D E F G List Holding 7 Items Example of a Tree Holding 7 Items, a Hierarchy

slide-3
SLIDE 3

Tree Terminology

Tree Node (vertex) Links (edges) Parent Child Sibling Root Leaf Ancestor Descendent

A B C D E F G

slide-4
SLIDE 4

Tree Terminology

Subtree Binary Tree M-ary Tree General Tree

A B C D E F D A B C D E F G A B C D E F G

slide-5
SLIDE 5

Formal Definition of a Binary Tree

A Binary Tree T is a set of nodes such that T is empty T is partitioned into three subsets:

  • 1. A single node R, the root

Two, possible empty sets forming binary trees

  • 2. the left subtree
  • 3. the right subtree

R L R

T

slide-6
SLIDE 6

Binary Tree Terminology

Path Height Full Tree Complete Tree Balanced Tree

A B C D E F G A B C D E F A B C D G

slide-7
SLIDE 7

The Binary Tree ADT

A B D E A B C D E F G A C F G attach right subtree detach left subtree

slide-8
SLIDE 8

Traversals of Binary Trees

Preorder traversal if T is not empty visit the root of T preorder traverse left subtree of T preorder traverse right subtree of T

A B C D E F G

slide-9
SLIDE 9

Traversals of Binary Trees

Inorder traversal if T is not empty inorder traverse left subtree of T visit the root of T inorder traverse right subtree of T

A B C D E F G

slide-10
SLIDE 10

Traversals of Binary Trees

Postorder traversal if T is not empty postorder traverse left subtree of T postorder traverse right subtree of T visit the root of T

A B C D E F G

slide-11
SLIDE 11

Examples

slide-12
SLIDE 12

In class exercise

What is the preorder, inorder, and postorder traversals of the following Binary Tree

A B C D E F G H I

slide-13
SLIDE 13

What are trees good for?

Parsing and representing relationships

slide-14
SLIDE 14

What are trees good for?

Representing Hierarchies

slide-15
SLIDE 15

What are trees good for?

Modeling decisions

State space for NIM game, 7 tokens

2-1-1-1-1-1 3-1-1-1-1 2-2-1-1-1 4-1-1-1 3-2-1-1 2-2-2-1 5-1-1 4-2-1 3-2-2 3-3-1 6-1 5-2 4-3 7

slide-16
SLIDE 16

What are trees good for?

Organization and searching

slide-17
SLIDE 17

Representing Binary Trees

Array based implementation for complete trees Why does this not work for non-complete trees?

A B C D E F A B C D E F 0 1 2 3 4 5

slide-18
SLIDE 18

Representing Binary Trees

List based representation (not in your text) Consider a list with contents given by a pair (tuple) of and item (atom) followed by a list. struct list { item a; list l; }

A B C D ( A ( ( B ( D ( ) ) ) ( C ( ) ) ) )

slide-19
SLIDE 19

Representing Binary Trees

Pointer based implementation, an extension of a linked list struct node { item a; node * left; node * right; }

A B C D

slide-20
SLIDE 20

Representing Binary Trees

struct node { item a; node * left; node * right; }

slide-21
SLIDE 21

Next Actions and Reminders

Read CH pp. 442-449 Program 4 is due 11/17