Main Index

36
Main Index Conten ts 1 1 Main Index Conten ts Week 6 – Binary Trees

description

1. Main Index. Contents. Week 6 – Binary Trees. Sequence and Associative Containers. Sequence containers access data by position Array (index) Vector (index) List (iterator) Associate containers can access data by value Set and Map / Binary search tree. Arrays Vectors. - PowerPoint PPT Presentation

Transcript of Main Index

Page 1: Main Index

Main IndexMain Index ContentsContents11 Main IndexMain Index ContentsContents

Week 6– Binary Trees

Page 2: Main Index

Sequence and Associative Containers

• Sequence containers access data by position1. Array (index)2. Vector (index)3. List (iterator)

• Associate containers can access data by value1. Set and Map / Binary search tree

ArraysVectors

Linkedlists Trees

Page 3: Main Index

Tree in Nature

Page 4: Main Index

Tree in our life

• Need to turn it upside downP r e side n t - C E O

P r o duc t io nM a n a ge r

Sa le sM a n a ge r

Sh ip p in gSup e r v iso r

P e r so n n e lM a n a ge r

W a r e h o useSup e r v iso r

P ur c h a sin gSup e r v iso r

H IER A R C H IC A L TR EE S TR U C TU R E

Page 5: Main Index

Tree in Computer Science

• Similar to tree in natureRoot

Leaves

Page 6: Main Index

Main IndexMain Index ContentsContents66 Main IndexMain Index ContentsContents

Tree Structures

A

JI

HGFE

DCB

(a)

(b )

A G EN ERAL T REE

RootParentChildEdgeLeaf

Interior nodeSubtreeLevel

Depth = max level

Page 7: Main Index

Terminologies used in Trees - Wiki

Page 8: Main Index

Main IndexMain Index ContentsContents88 Main IndexMain Index ContentsContents

Tree Node Level and Path Length

A

HG

FE

DCB

L e ve l: 0

L e ve l: 1

L e ve l: 2

L e ve l: 3

What is the Depth?

Page 9: Main Index

Main IndexMain Index ContentsContents9

Binary Tree Definition

A binary tree T is a finite set of nodes with one of the following properties:– (a) T is a tree if the set of nodes is empty.

(An empty tree is a tree, size=0.)

– (b) The set consists of a root, R, and exactly two distinct binary trees, the left subtree TL and

the right subtree TR. The nodes in T consist of node R and all the nodes in TL and TR.

Any node in a binary tree has at most two children

Page 10: Main Index

Main IndexMain Index ContentsContents1010 Main IndexMain Index ContentsContents

Selected Samples of Binary Trees

A

E

D

C

B

A

F

H

ED

CB

I

T ree ASiz e 9 D ep t h 3

T ree BSiz e 5 D ep t h 4

G

Does tree B have left subtree TL?

Page 11: Main Index

Density of a Binary Tree

• Intuitively, density is a measure of the size of a tree (number of nodes) relative to the depth of the tree.

• Trees with a higher density are important as data structures, because they can “pack” more nodes near the root.

• Access to the nodes is along relatively short paths from the root.

Page 12: Main Index

Complete binary tree

Page 13: Main Index

Degenerate tree

• A degenerate (or pathological) tree is where each parent node has only one associated child node. This means that performance-wise, the tree will behave like a linked list data structure.

Page 14: Main Index

Evaluating Tree Density

• Complete binary trees are an ideal storage structure, because of their ability to pack a large number of nodes near the root

• Assume we want to store n elements in a complete binary tree. We would like to know the depth d of such a tree.

Page 15: Main Index

Depth d --- Size n in complete binary tree

…………………

… …

level dhas 2d nodes

-1

20 = 1 nodes

21 = 2 nodes

Geometric series

Page 16: Main Index
Page 17: Main Index

Main IndexMain Index ContentsContents1717 Main IndexMain Index ContentsContents

Binary Tree Nodes

Abs tract T re e M ode l

A

E F

H

D

CB

G

l e ft A ri gh t

T re e N ode M ode l

l e ft B ri gh t

l e ft E ri gh t

l e ft G ri gh t

l e ft D ri gh t

l e ft C ri gh t

l e ft H ri gh t

l e ft F ri gh t

Page 18: Main Index

Node classtemplate <typename T>{public:

T nodeValue;tnode<T> *left, *right;tnode()

{}tnode(const T& item, tnode<T> *lptr=NULL,

tnode<T> *rptr=NULL):nodeValue(item), left(lptr), right(rptr)

{}};

Page 19: Main Index

Node structure

Page 20: Main Index

Building a Binary Tree

• A binary tree consists of a collection of dynamically allocated tnode objects whose pointer values specify links to their children.

Page 21: Main Index

Recursion• Solution to a problem depends on solutions to smaller

instances of the same problem.

• As a tree is a self-referential (recursively defined) data structure, traversal can naturally be described by recursion.

• Recursive function: a function that calls itself.

21

Page 22: Main Index

BINARY TREE SCAN ALGORITHMS

• How to traverse the tree so that each node is visited exactly once?

1. Depth-first• Pre-order• In-order• Post-order

2. Breadth-first (level-order)

Page 23: Main Index

Depth-first

• Defined as operations recursively at each node.

• The actions include: visiting the node and performing some task (N), making a recursive descent to the left subtree (L), making a recursive descent to the right subtree (R).

• The different scanning strategies depend on the order in which we perform the tasks.

Page 24: Main Index

In-order Scan• The prefix “in” comes from the fact that the visit

occurs between the descents into the two subtrees.

• In this example, we use the order LNR.1. Traverse the left subtree (“go left”).2. Visit the node.3. Traverse the right subtree (“go right”).

L

N

R

Recursively!

Page 25: Main Index

In-order example

L

N

R

L N R

L N RL N RL N R

Recursively!In-order scan: B, D, A, E, C

Page 26: Main Index

In-order output

Page 27: Main Index

Post-order scan

• 1. Traverse the left subtree (“go left”).• 2. Traverse the right subtree (“go right”).• 3. Visit the node.

1.

3.

2.

D, B, E, C, A

Page 28: Main Index

Post-order output

Page 29: Main Index

Pre-order scan

1. Visit the node.2. Traverse the left subtree (“go left”).3. Traverse the right subtree (“go right”).

2.

1.

3.A, B, D, C, E

Page 30: Main Index

Breadth-first (Level-Order) Scan

Page 31: Main Index

Example QuestionF

B

C E

I

H

DA

G

Pre-order? In-order? Post-order? Level-order ?

Page 33: Main Index

Computing the Leaf Count

Pre-order scan

Page 34: Main Index

Computing the Depth of a Tree

Post-order scan

Page 35: Main Index

Deleting Tree Nodes

Post-order scan

Page 36: Main Index

Reading

• Chapter 4