preparation data structures 10 trees

199

Upload: andres-mendez-vazquez

Post on 06-Aug-2015

32 views

Category:

Education


2 download

TRANSCRIPT

Data StructuresTrees

Andres Mendez-Vazquez

May 6, 2015

1 / 100

Images/cinvestav-1.jpg

Outline

1 Trees: Basic ConceptsSubtrees, Leaves and Levels

2 Binary TreesBasic ConceptsThe Height Of a Binary TreeArithmetic ExpressionsPropertiesNumbering Nodes in A Full Binary TreeArray RepresentationLinked RepresentationTraversals

2 / 100

Images/cinvestav-1.jpg

Trees Structures

Linear lists are useful for serially ordered data.

(e0,e1, ...,en−1)

Days of week.

Months in a year.

Students in this class.

Trees are useful for hierarchically ordered data

Employees of a corporation.

I President, vice presidents, managers, and so on.

Java's classes.

I Object is at the top of the hierarchy.I Subclasses of Object are next, and so on.

3 / 100

Images/cinvestav-1.jpg

Trees Structures

Linear lists are useful for serially ordered data.

(e0,e1, ...,en−1)

Days of week.

Months in a year.

Students in this class.

Trees are useful for hierarchically ordered data

Employees of a corporation.

I President, vice presidents, managers, and so on.

Java's classes.

I Object is at the top of the hierarchy.I Subclasses of Object are next, and so on.

3 / 100

Images/cinvestav-1.jpg

De�nition

First

A tree T is a �nite nonempty set of elements.

Root

One of these elements is called the root.

The rest

The remaining elements, if any, are partitioned into trees, which are calledthe subtrees of T .

4 / 100

Images/cinvestav-1.jpg

De�nition

First

A tree T is a �nite nonempty set of elements.

Root

One of these elements is called the root.

The rest

The remaining elements, if any, are partitioned into trees, which are calledthe subtrees of T .

4 / 100

Images/cinvestav-1.jpg

De�nition

First

A tree T is a �nite nonempty set of elements.

Root

One of these elements is called the root.

The rest

The remaining elements, if any, are partitioned into trees, which are calledthe subtrees of T .

4 / 100

Images/cinvestav-1.jpg

For example

Class Structure

5 / 100

Images/cinvestav-1.jpg

For example

Class Structure

ROOT

6 / 100

Images/cinvestav-1.jpg

For example

Class Structure

ROOT

Children of The ROOT

7 / 100

Images/cinvestav-1.jpg

For example

Class Structure

ROOT

Children of The ROOT

Grand Children of The ROOT

8 / 100

Images/cinvestav-1.jpg

Outline

1 Trees: Basic ConceptsSubtrees, Leaves and Levels

2 Binary TreesBasic ConceptsThe Height Of a Binary TreeArithmetic ExpressionsPropertiesNumbering Nodes in A Full Binary TreeArray RepresentationLinked RepresentationTraversals

9 / 100

Images/cinvestav-1.jpg

SubTrees

We have the following

10 / 100

Images/cinvestav-1.jpg

Leaves

We have the following

11 / 100

Images/cinvestav-1.jpg

Levels

We have the following

Level 4

Level 1

Level 2

Level 3

12 / 100

Images/cinvestav-1.jpg

Caution

BE AWARE

Some texts start level numbers at 0 rather than at 1.

Thus

Root is at level 0. Its children are at level 1.

The grand children of the root are at level 2.

And so on.

We will adapt to the following notation

Root is at level 1!!!

13 / 100

Images/cinvestav-1.jpg

Caution

BE AWARE

Some texts start level numbers at 0 rather than at 1.

Thus

Root is at level 0. Its children are at level 1.

The grand children of the root are at level 2.

And so on.

We will adapt to the following notation

Root is at level 1!!!

13 / 100

Images/cinvestav-1.jpg

Caution

BE AWARE

Some texts start level numbers at 0 rather than at 1.

Thus

Root is at level 0. Its children are at level 1.

The grand children of the root are at level 2.

And so on.

We will adapt to the following notation

Root is at level 1!!!

13 / 100

Images/cinvestav-1.jpg

Caution

BE AWARE

Some texts start level numbers at 0 rather than at 1.

Thus

Root is at level 0. Its children are at level 1.

The grand children of the root are at level 2.

And so on.

We will adapt to the following notation

Root is at level 1!!!

13 / 100

Images/cinvestav-1.jpg

Caution

BE AWARE

Some texts start level numbers at 0 rather than at 1.

Thus

Root is at level 0. Its children are at level 1.

The grand children of the root are at level 2.

And so on.

We will adapt to the following notation

Root is at level 1!!!

13 / 100

Images/cinvestav-1.jpg

General Structure

AHHHH!!!!

14 / 100

Images/cinvestav-1.jpg

Thus, we have the concept of height

Which is Height == Depth

Level 4

Level 1

Level 2

Level 3

15 / 100

Images/cinvestav-1.jpg

In addition...

Node Degree == Number Of Children

3

2 1 1

00 1 0

0

16 / 100

Images/cinvestav-1.jpg

Tree Degree == Max Node Degree

In our case 3

3

2 1 1

00 1 0

0

17 / 100

Images/cinvestav-1.jpg

Outline

1 Trees: Basic ConceptsSubtrees, Leaves and Levels

2 Binary TreesBasic ConceptsThe Height Of a Binary TreeArithmetic ExpressionsPropertiesNumbering Nodes in A Full Binary TreeArray RepresentationLinked RepresentationTraversals

18 / 100

Images/cinvestav-1.jpg

De�nition

We have that a Binary Tree is

A �nite (possibly empty) collection of elements.

Second1 A nonempty binary tree has a root element.

2 The remaining elements (if any) are partitioned into two binary trees.

3 These are called the left and right subtrees of the binary tree.

19 / 100

Images/cinvestav-1.jpg

De�nition

We have that a Binary Tree is

A �nite (possibly empty) collection of elements.

Second1 A nonempty binary tree has a root element.

2 The remaining elements (if any) are partitioned into two binary trees.

3 These are called the left and right subtrees of the binary tree.

19 / 100

Images/cinvestav-1.jpg

De�nition

We have that a Binary Tree is

A �nite (possibly empty) collection of elements.

Second1 A nonempty binary tree has a root element.

2 The remaining elements (if any) are partitioned into two binary trees.

3 These are called the left and right subtrees of the binary tree.

19 / 100

Images/cinvestav-1.jpg

De�nition

We have that a Binary Tree is

A �nite (possibly empty) collection of elements.

Second1 A nonempty binary tree has a root element.

2 The remaining elements (if any) are partitioned into two binary trees.

3 These are called the left and right subtrees of the binary tree.

19 / 100

Images/cinvestav-1.jpg

Example

A Way to See Them

ROOT

20 / 100

Images/cinvestav-1.jpg

Examples of Binary Trees

We have some examples

21 / 100

Images/cinvestav-1.jpg

Di�erences Between A Tree & A Binary Tree

First

No node in a binary tree may have a degree more than 2, whereas there isno limit on the degree of a node in a tree.

Second

A binary tree may be empty; a tree cannot be empty.

22 / 100

Images/cinvestav-1.jpg

Di�erences Between A Tree & A Binary Tree

First

No node in a binary tree may have a degree more than 2, whereas there isno limit on the degree of a node in a tree.

Second

A binary tree may be empty; a tree cannot be empty.

22 / 100

Images/cinvestav-1.jpg

Outline

1 Trees: Basic ConceptsSubtrees, Leaves and Levels

2 Binary TreesBasic ConceptsThe Height Of a Binary TreeArithmetic ExpressionsPropertiesNumbering Nodes in A Full Binary TreeArray RepresentationLinked RepresentationTraversals

23 / 100

Images/cinvestav-1.jpg

The height of full or complete trees.

Something Notable

FULL Tree Height

1

Number Of Nodes

24 / 100

Images/cinvestav-1.jpg

The height of full or complete trees.

Something Notable

FULL Tree Height

2

Number Of Nodes

25 / 100

Images/cinvestav-1.jpg

The height of full or complete trees.

Something Notable

FULL Tree Height

3

Number Of Nodes

26 / 100

Images/cinvestav-1.jpg

Now, if n is the number of nodes in a full tree,

Number of nodes

n = 2h−1 (1)

Height of the tree

h = log2 (n+1) (2)

27 / 100

Images/cinvestav-1.jpg

Now, if n is the number of nodes in a full tree,

Number of nodes

n = 2h−1 (1)

Height of the tree

h = log2 (n+1) (2)

27 / 100

Images/cinvestav-1.jpg

Outline

1 Trees: Basic ConceptsSubtrees, Leaves and Levels

2 Binary TreesBasic ConceptsThe Height Of a Binary TreeArithmetic ExpressionsPropertiesNumbering Nodes in A Full Binary TreeArray RepresentationLinked RepresentationTraversals

28 / 100

Images/cinvestav-1.jpg

Arithmetic Expressions

Imagine the following expressions

(a+b)∗ (c+d)+ e�f /g ∗h+3.25 (3)

Operators

+,−,/,∗ (4)

Operands

a,b,c ,d ,e, f ,g ,h,3.25,(a+b),(c+d),etc . (5)

29 / 100

Images/cinvestav-1.jpg

Arithmetic Expressions

Imagine the following expressions

(a+b)∗ (c+d)+ e�f /g ∗h+3.25 (3)

Operators

+,−,/,∗ (4)

Operands

a,b,c ,d ,e, f ,g ,h,3.25,(a+b),(c+d),etc . (5)

29 / 100

Images/cinvestav-1.jpg

Arithmetic Expressions

Imagine the following expressions

(a+b)∗ (c+d)+ e�f /g ∗h+3.25 (3)

Operators

+,−,/,∗ (4)

Operands

a,b,c ,d ,e, f ,g ,h,3.25,(a+b),(c+d),etc . (5)

29 / 100

Images/cinvestav-1.jpg

In addition

Delimiters

(...) (6)

30 / 100

Images/cinvestav-1.jpg

Operators Degree

De�nition

Number of operands that the operator requires.

Binary operator requires two operands.

a + b

c / d

e - f

Unary operator requires one operand.

+ g

- h

31 / 100

Images/cinvestav-1.jpg

Operators Degree

De�nition

Number of operands that the operator requires.

Binary operator requires two operands.

a + b

c / d

e - f

Unary operator requires one operand.

+ g

- h

31 / 100

Images/cinvestav-1.jpg

Operators Degree

De�nition

Number of operands that the operator requires.

Binary operator requires two operands.

a + b

c / d

e - f

Unary operator requires one operand.

+ g

- h

31 / 100

Images/cinvestav-1.jpg

About the In�x form

Something Notable

Normal way to write an expression.

Thus

Binary operators come in between their left and right operands.

Example

a * b

a + b * c a * b / c (a + b) * (c + d) + e � f/g*h + 3.25

32 / 100

Images/cinvestav-1.jpg

About the In�x form

Something Notable

Normal way to write an expression.

Thus

Binary operators come in between their left and right operands.

Example

a * b

a + b * c a * b / c (a + b) * (c + d) + e � f/g*h + 3.25

32 / 100

Images/cinvestav-1.jpg

About the In�x form

Something Notable

Normal way to write an expression.

Thus

Binary operators come in between their left and right operands.

Example

a * b

a + b * c a * b / c (a + b) * (c + d) + e � f/g*h + 3.25

32 / 100

Images/cinvestav-1.jpg

Operator Priorities

How do you �gure out the operands of an operator?

a + b * c

a * b + c / d

This is done by assigning operator priorities

priority(*) = priority(/) > priority(+) = priority(-)

Thus

When an operand lies between two operators, the operand associates withthe operator that has higher priority.

33 / 100

Images/cinvestav-1.jpg

Operator Priorities

How do you �gure out the operands of an operator?

a + b * c

a * b + c / d

This is done by assigning operator priorities

priority(*) = priority(/) > priority(+) = priority(-)

Thus

When an operand lies between two operators, the operand associates withthe operator that has higher priority.

33 / 100

Images/cinvestav-1.jpg

Operator Priorities

How do you �gure out the operands of an operator?

a + b * c

a * b + c / d

This is done by assigning operator priorities

priority(*) = priority(/) > priority(+) = priority(-)

Thus

When an operand lies between two operators, the operand associates withthe operator that has higher priority.

33 / 100

Images/cinvestav-1.jpg

Tie Breakers

However this can happen

When an operand lies between two operators that have the same priority,the operand associates with the operator on the left.

34 / 100

Images/cinvestav-1.jpg

Delimiters

What about �()�

Subexpression within delimiters is treated as a single operand, independentfrom the remainder of the expression.

Example

(a + b) * (c � d) / (e � f)

35 / 100

Images/cinvestav-1.jpg

Delimiters

What about �()�

Subexpression within delimiters is treated as a single operand, independentfrom the remainder of the expression.

Example

(a + b) * (c � d) / (e � f)

35 / 100

Images/cinvestav-1.jpg

HOWEVER

In�x Expression Is Hard To Parse

Need operator priorities, tie breaker, and delimiters.

This makes computer evaluation more di�cult than is necessary.

Post�x and pre�x expression forms do not rely on operator priorities, atie breaker, or delimiters.

So it is easier for a computer to evaluate expressions that are in theseforms.

36 / 100

Images/cinvestav-1.jpg

HOWEVER

In�x Expression Is Hard To Parse

Need operator priorities, tie breaker, and delimiters.

This makes computer evaluation more di�cult than is necessary.

Post�x and pre�x expression forms do not rely on operator priorities, atie breaker, or delimiters.

So it is easier for a computer to evaluate expressions that are in theseforms.

36 / 100

Images/cinvestav-1.jpg

HOWEVER

In�x Expression Is Hard To Parse

Need operator priorities, tie breaker, and delimiters.

This makes computer evaluation more di�cult than is necessary.

Post�x and pre�x expression forms do not rely on operator priorities, atie breaker, or delimiters.

So it is easier for a computer to evaluate expressions that are in theseforms.

36 / 100

Images/cinvestav-1.jpg

HOWEVER

In�x Expression Is Hard To Parse

Need operator priorities, tie breaker, and delimiters.

This makes computer evaluation more di�cult than is necessary.

Post�x and pre�x expression forms do not rely on operator priorities, atie breaker, or delimiters.

So it is easier for a computer to evaluate expressions that are in theseforms.

36 / 100

Images/cinvestav-1.jpg

For example

4*(3+17)

*

4 +

3 17

37 / 100

Images/cinvestav-1.jpg

Post�x Form

We have that

The post�x form of a variable or constant is the same as its in�x form.

a, b, 3.25

Relative order

The relative order of operands is the same in in�x and post�x forms.

Thus

Operators come immediately after the post�x form of their operands.

In�x = a + b

Post�x = ab+

38 / 100

Images/cinvestav-1.jpg

Post�x Form

We have that

The post�x form of a variable or constant is the same as its in�x form.

a, b, 3.25

Relative order

The relative order of operands is the same in in�x and post�x forms.

Thus

Operators come immediately after the post�x form of their operands.

In�x = a + b

Post�x = ab+

38 / 100

Images/cinvestav-1.jpg

Post�x Form

We have that

The post�x form of a variable or constant is the same as its in�x form.

a, b, 3.25

Relative order

The relative order of operands is the same in in�x and post�x forms.

Thus

Operators come immediately after the post�x form of their operands.

In�x = a + b

Post�x = ab+

38 / 100

Images/cinvestav-1.jpg

Example

1st One

In�x = a+b*c

I Post�x= abc*+

2nd One

In�x = a*b+c

I Post�x = ab*c+

3rd One

In�x = (a + b) * (c � d) / (e + f)

I Post�x = (ab)+cd-*ef+/

39 / 100

Images/cinvestav-1.jpg

Example

1st One

In�x = a+b*c

I Post�x= abc*+

2nd One

In�x = a*b+c

I Post�x = ab*c+

3rd One

In�x = (a + b) * (c � d) / (e + f)

I Post�x = (ab)+cd-*ef+/

39 / 100

Images/cinvestav-1.jpg

Example

1st One

In�x = a+b*c

I Post�x= abc*+

2nd One

In�x = a*b+c

I Post�x = ab*c+

3rd One

In�x = (a + b) * (c � d) / (e + f)

I Post�x = (ab)+cd-*ef+/

39 / 100

Images/cinvestav-1.jpg

Example

1st One

In�x = a+b*c

I Post�x= abc*+

2nd One

In�x = a*b+c

I Post�x = ab*c+

3rd One

In�x = (a + b) * (c � d) / (e + f)

I Post�x = (ab)+cd-*ef+/

39 / 100

Images/cinvestav-1.jpg

Example

1st One

In�x = a+b*c

I Post�x= abc*+

2nd One

In�x = a*b+c

I Post�x = ab*c+

3rd One

In�x = (a + b) * (c � d) / (e + f)

I Post�x = (ab)+cd-*ef+/

39 / 100

Images/cinvestav-1.jpg

Example

1st One

In�x = a+b*c

I Post�x= abc*+

2nd One

In�x = a*b+c

I Post�x = ab*c+

3rd One

In�x = (a + b) * (c � d) / (e + f)

I Post�x = (ab)+cd-*ef+/

39 / 100

Images/cinvestav-1.jpg

What about Unary Operators?

After all they need to be evaluated

Everything need to be interpreted!!!

Replace with new symbols

+ a => a @

I + a + b => a @ b +

- a => a ?

I - a-b => a ? b -

40 / 100

Images/cinvestav-1.jpg

What about Unary Operators?

After all they need to be evaluated

Everything need to be interpreted!!!

Replace with new symbols

+ a => a @

I + a + b => a @ b +

- a => a ?

I - a-b => a ? b -

40 / 100

Images/cinvestav-1.jpg

What about Unary Operators?

After all they need to be evaluated

Everything need to be interpreted!!!

Replace with new symbols

+ a => a @

I + a + b => a @ b +

- a => a ?

I - a-b => a ? b -

40 / 100

Images/cinvestav-1.jpg

What about Unary Operators?

After all they need to be evaluated

Everything need to be interpreted!!!

Replace with new symbols

+ a => a @

I + a + b => a @ b +

- a => a ?

I - a-b => a ? b -

40 / 100

Images/cinvestav-1.jpg

What about Unary Operators?

After all they need to be evaluated

Everything need to be interpreted!!!

Replace with new symbols

+ a => a @

I + a + b => a @ b +

- a => a ?

I - a-b => a ? b -

40 / 100

Images/cinvestav-1.jpg

Post�x Evaluation

How do we do the evaluation?

Scan post�x expression from left to right pushing operands on to a stack.

Then

When an operator is encountered, pop as many operands as thisoperator needs.

Then, evaluate the operator.

Then, push the result on to the stack.

IMPORTANT

This works because, in post�x, operators come immediately after theiroperands.

41 / 100

Images/cinvestav-1.jpg

Post�x Evaluation

How do we do the evaluation?

Scan post�x expression from left to right pushing operands on to a stack.

Then

When an operator is encountered, pop as many operands as thisoperator needs.

Then, evaluate the operator.

Then, push the result on to the stack.

IMPORTANT

This works because, in post�x, operators come immediately after theiroperands.

41 / 100

Images/cinvestav-1.jpg

Post�x Evaluation

How do we do the evaluation?

Scan post�x expression from left to right pushing operands on to a stack.

Then

When an operator is encountered, pop as many operands as thisoperator needs.

Then, evaluate the operator.

Then, push the result on to the stack.

IMPORTANT

This works because, in post�x, operators come immediately after theiroperands.

41 / 100

Images/cinvestav-1.jpg

Post�x Evaluation

How do we do the evaluation?

Scan post�x expression from left to right pushing operands on to a stack.

Then

When an operator is encountered, pop as many operands as thisoperator needs.

Then, evaluate the operator.

Then, push the result on to the stack.

IMPORTANT

This works because, in post�x, operators come immediately after theiroperands.

41 / 100

Images/cinvestav-1.jpg

Post�x Evaluation

How do we do the evaluation?

Scan post�x expression from left to right pushing operands on to a stack.

Then

When an operator is encountered, pop as many operands as thisoperator needs.

Then, evaluate the operator.

Then, push the result on to the stack.

IMPORTANT

This works because, in post�x, operators come immediately after theiroperands.

41 / 100

Images/cinvestav-1.jpg

Example: (a + b) * (c � d) / (e + f)

a b + c d - * e f + /

42 / 100

Images/cinvestav-1.jpg

Example: (a + b) * (c � d) / (e + f)

a b + c d - * e f + /

43 / 100

Images/cinvestav-1.jpg

Example: (a + b) * (c � d) / (e + f)

a b + c d - * e f + /

44 / 100

Images/cinvestav-1.jpg

Example: (a + b) * (c � d) / (e + f)

a b + c d - * e f + /

45 / 100

Images/cinvestav-1.jpg

Example: (a + b) * (c � d) / (e + f)

a b + c d - * e f + /

46 / 100

Images/cinvestav-1.jpg

Example: (a + b) * (c � d) / (e + f)

a b + c d - * e f + /

47 / 100

Images/cinvestav-1.jpg

Example: (a + b) * (c � d) / (e + f)

a b + c d - * e f + /

48 / 100

Images/cinvestav-1.jpg

Pre�x Form

Here

The pre�x form of a variable or constant is the same as its in�x form.

a, b, 3.25

Thus

The relative order of operands is the same in in�x and pre�x forms.

Then

Operators come immediately before the pre�x form of their operands.

In�x = a + b

Post�x = ab+

Pre�x = +ab

49 / 100

Images/cinvestav-1.jpg

Pre�x Form

Here

The pre�x form of a variable or constant is the same as its in�x form.

a, b, 3.25

Thus

The relative order of operands is the same in in�x and pre�x forms.

Then

Operators come immediately before the pre�x form of their operands.

In�x = a + b

Post�x = ab+

Pre�x = +ab

49 / 100

Images/cinvestav-1.jpg

Pre�x Form

Here

The pre�x form of a variable or constant is the same as its in�x form.

a, b, 3.25

Thus

The relative order of operands is the same in in�x and pre�x forms.

Then

Operators come immediately before the pre�x form of their operands.

In�x = a + b

Post�x = ab+

Pre�x = +ab

49 / 100

Images/cinvestav-1.jpg

Pre�x Form

Here

The pre�x form of a variable or constant is the same as its in�x form.

a, b, 3.25

Thus

The relative order of operands is the same in in�x and pre�x forms.

Then

Operators come immediately before the pre�x form of their operands.

In�x = a + b

Post�x = ab+

Pre�x = +ab

49 / 100

Images/cinvestav-1.jpg

Pre�x Form

Here

The pre�x form of a variable or constant is the same as its in�x form.

a, b, 3.25

Thus

The relative order of operands is the same in in�x and pre�x forms.

Then

Operators come immediately before the pre�x form of their operands.

In�x = a + b

Post�x = ab+

Pre�x = +ab

49 / 100

Images/cinvestav-1.jpg

Pre�x Form

Here

The pre�x form of a variable or constant is the same as its in�x form.

a, b, 3.25

Thus

The relative order of operands is the same in in�x and pre�x forms.

Then

Operators come immediately before the pre�x form of their operands.

In�x = a + b

Post�x = ab+

Pre�x = +ab

49 / 100

Images/cinvestav-1.jpg

Pre�x Form

Here

The pre�x form of a variable or constant is the same as its in�x form.

a, b, 3.25

Thus

The relative order of operands is the same in in�x and pre�x forms.

Then

Operators come immediately before the pre�x form of their operands.

In�x = a + b

Post�x = ab+

Pre�x = +ab

49 / 100

Images/cinvestav-1.jpg

Then, we can use trees to represent operations

a+b

+

a b

-a

-

a

50 / 100

Images/cinvestav-1.jpg

Then, we can use trees to represent operations

a+b

+

a b

-a

-

a

50 / 100

Images/cinvestav-1.jpg

Example

(a + b) * (c � d) / (e + f)

51 / 100

Images/cinvestav-1.jpg

Merits Of Binary Tree Form

We love them

The left and right operands are easy to visualize.

There are code optimization algorithms that work well with the binarytree form of an expression.

We can use simple recursive evaluations on each expression to get theresults.

52 / 100

Images/cinvestav-1.jpg

Merits Of Binary Tree Form

We love them

The left and right operands are easy to visualize.

There are code optimization algorithms that work well with the binarytree form of an expression.

We can use simple recursive evaluations on each expression to get theresults.

52 / 100

Images/cinvestav-1.jpg

Merits Of Binary Tree Form

We love them

The left and right operands are easy to visualize.

There are code optimization algorithms that work well with the binarytree form of an expression.

We can use simple recursive evaluations on each expression to get theresults.

52 / 100

Images/cinvestav-1.jpg

Outline

1 Trees: Basic ConceptsSubtrees, Leaves and Levels

2 Binary TreesBasic ConceptsThe Height Of a Binary TreeArithmetic ExpressionsPropertiesNumbering Nodes in A Full Binary TreeArray RepresentationLinked RepresentationTraversals

53 / 100

Images/cinvestav-1.jpg

Minimum Number Of Nodes

Question

Which is the minimum number of nodes in a binary tree whose height is h?

First

You have at least one node at each of the h levels.

Then

54 / 100

Images/cinvestav-1.jpg

Minimum Number Of Nodes

Question

Which is the minimum number of nodes in a binary tree whose height is h?

First

You have at least one node at each of the h levels.

Then

54 / 100

Images/cinvestav-1.jpg

Minimum Number Of Nodes

Question

Which is the minimum number of nodes in a binary tree whose height is h?

First

You have at least one node at each of the h levels.

Then

54 / 100

Images/cinvestav-1.jpg

Maximum Number Of Nodes

Here, all possible nodes at �rst h levels are present.

Here, we have that

1+2+4+8+ ...+2h−1 =2h−1

2−1= 2h−1 (7)

55 / 100

Images/cinvestav-1.jpg

Maximum Number Of Nodes

Here, all possible nodes at �rst h levels are present.

Here, we have that

1+2+4+8+ ...+2h−1 =2h−1

2−1= 2h−1 (7)

55 / 100

Images/cinvestav-1.jpg

Number Of Nodes & Height

We have that

Let n be the number of nodes in a binary tree whose height is h.

Then

h ≤ n ≤ 2h−1 (8)

Thus

log2 (n+1)≤ h⇒ log2 (n+1)≤ h ≤ n (9)

56 / 100

Images/cinvestav-1.jpg

Number Of Nodes & Height

We have that

Let n be the number of nodes in a binary tree whose height is h.

Then

h ≤ n ≤ 2h−1 (8)

Thus

log2 (n+1)≤ h⇒ log2 (n+1)≤ h ≤ n (9)

56 / 100

Images/cinvestav-1.jpg

Number Of Nodes & Height

We have that

Let n be the number of nodes in a binary tree whose height is h.

Then

h ≤ n ≤ 2h−1 (8)

Thus

log2 (n+1)≤ h⇒ log2 (n+1)≤ h ≤ n (9)

56 / 100

Images/cinvestav-1.jpg

Full Binary Tree

A full binary tree of a given height h has 2h�1 nodes.

57 / 100

Images/cinvestav-1.jpg

Outline

1 Trees: Basic ConceptsSubtrees, Leaves and Levels

2 Binary TreesBasic ConceptsThe Height Of a Binary TreeArithmetic ExpressionsPropertiesNumbering Nodes in A Full Binary TreeArray RepresentationLinked RepresentationTraversals

58 / 100

Images/cinvestav-1.jpg

Numbering Nodes In A Full Binary Tree

It is possible to do the following

1 Number the nodes 1 through 2h � 1.

2 Number by levels from top to bottom.

3 Within a level number from left to right.

We get the following

59 / 100

Images/cinvestav-1.jpg

Numbering Nodes In A Full Binary Tree

It is possible to do the following

1 Number the nodes 1 through 2h � 1.

2 Number by levels from top to bottom.

3 Within a level number from left to right.

We get the following

59 / 100

Images/cinvestav-1.jpg

Numbering Nodes In A Full Binary Tree

It is possible to do the following

1 Number the nodes 1 through 2h � 1.

2 Number by levels from top to bottom.

3 Within a level number from left to right.

We get the following

59 / 100

Images/cinvestav-1.jpg

Numbering Nodes In A Full Binary Tree

It is possible to do the following

1 Number the nodes 1 through 2h � 1.

2 Number by levels from top to bottom.

3 Within a level number from left to right.

We get the following

59 / 100

Images/cinvestav-1.jpg

Node Number Properties

We have the following

The parent of node i is nodebi/2c, unless i = 1.

Node 1 is the root and has no parent.

What about the children

Left child of node i is node 2i , unless 2i > n, where n is the number ofnodes.

If 2i > n, node i has no left child.

60 / 100

Images/cinvestav-1.jpg

Node Number Properties

We have the following

The parent of node i is nodebi/2c, unless i = 1.

Node 1 is the root and has no parent.

What about the children

Left child of node i is node 2i , unless 2i > n, where n is the number ofnodes.

If 2i > n, node i has no left child.

60 / 100

Images/cinvestav-1.jpg

Node Number Properties

We have the following

The parent of node i is nodebi/2c, unless i = 1.

Node 1 is the root and has no parent.

What about the children

Left child of node i is node 2i , unless 2i > n, where n is the number ofnodes.

If 2i > n, node i has no left child.

60 / 100

Images/cinvestav-1.jpg

Node Number Properties

We have the following

The parent of node i is nodebi/2c, unless i = 1.

Node 1 is the root and has no parent.

What about the children

Left child of node i is node 2i , unless 2i > n, where n is the number ofnodes.

If 2i > n, node i has no left child.

60 / 100

Images/cinvestav-1.jpg

After all

We get the following

61 / 100

Images/cinvestav-1.jpg

What about the right Children?

We have the following

Right child of node i is node 2i +1, unless 2i +1> n, where n is thenumber of nodes.

If 2i +1> n, node i has no right child.

62 / 100

Images/cinvestav-1.jpg

What about the right Children?

We have the following

Right child of node i is node 2i +1, unless 2i +1> n, where n is thenumber of nodes.

If 2i +1> n, node i has no right child.

62 / 100

Images/cinvestav-1.jpg

Complete Binary Tree With n Nodes

De�ntion1 It starts with a full binary tree that has at least n nodes.

2 Number the nodes as described earlier.

3 The binary tree de�ned by the nodes numbered 1 through n is theunique n node complete binary tree.

63 / 100

Images/cinvestav-1.jpg

Complete Binary Tree With n Nodes

De�ntion1 It starts with a full binary tree that has at least n nodes.

2 Number the nodes as described earlier.

3 The binary tree de�ned by the nodes numbered 1 through n is theunique n node complete binary tree.

63 / 100

Images/cinvestav-1.jpg

Complete Binary Tree With n Nodes

De�ntion1 It starts with a full binary tree that has at least n nodes.

2 Number the nodes as described earlier.

3 The binary tree de�ned by the nodes numbered 1 through n is theunique n node complete binary tree.

63 / 100

Images/cinvestav-1.jpg

Example

Complete binary tree with 10 nodes.

64 / 100

Images/cinvestav-1.jpg

How we represent them?

We have two ways

Array Representation

Linked Representation

65 / 100

Images/cinvestav-1.jpg

Outline

1 Trees: Basic ConceptsSubtrees, Leaves and Levels

2 Binary TreesBasic ConceptsThe Height Of a Binary TreeArithmetic ExpressionsPropertiesNumbering Nodes in A Full Binary TreeArray RepresentationLinked RepresentationTraversals

66 / 100

Images/cinvestav-1.jpg

Array Representation

We do the following

Number the nodes using the numbering scheme for a full binary tree. Thenode that is numbered i is stored in tree[i ].

Then

67 / 100

Images/cinvestav-1.jpg

Array Representation

We do the following

Number the nodes using the numbering scheme for a full binary tree. Thenode that is numbered i is stored in tree[i ].

Then

67 / 100

Images/cinvestav-1.jpg

What if we have something like this

Right-Skewed Binary Tree

What a waste of memory!!!

68 / 100

Images/cinvestav-1.jpg

What if we have something like this

Right-Skewed Binary Tree

What a waste of memory!!!

68 / 100

Images/cinvestav-1.jpg

Outline

1 Trees: Basic ConceptsSubtrees, Leaves and Levels

2 Binary TreesBasic ConceptsThe Height Of a Binary TreeArithmetic ExpressionsPropertiesNumbering Nodes in A Full Binary TreeArray RepresentationLinked RepresentationTraversals

69 / 100

Images/cinvestav-1.jpg

What can we do?

Linked Representation

Each binary tree node is represented as an object whose data type isBinaryTreeNode.

The space required by an n node binary tree is n * (space required byone node).

70 / 100

Images/cinvestav-1.jpg

What can we do?

Linked Representation

Each binary tree node is represented as an object whose data type isBinaryTreeNode.

The space required by an n node binary tree is n * (space required byone node).

70 / 100

Images/cinvestav-1.jpg

Code for a Node

We have then with Generic Key and Item

package Tree ;p u b l i c c l a s s BinaryTreeNode{

Key key ;I tem e lement ;BinaryTreeNode l e f t C h i l d ; // l e f t s u b t r e eBinaryTreeNode r i g h t C h i l d ; // r i g h t s u b t r e e// c o n s t r u c t o r s and any o th e r methods// come he r e

}

71 / 100

Images/cinvestav-1.jpg

Example

Look at this

72 / 100

Images/cinvestav-1.jpg

Which Operations

We might want to have

Determine the height.

Determine the number of nodes.

Make a clone.

Determine if two binary trees are clones.

Maybe, we want

Display the binary tree.

Evaluate the arithmetic expression represented by a binary tree.

Finally, we want

Obtain the in�x form of an expression.

Obtain the pre�x form of an expression.

Obtain the post�x form of an expression.

73 / 100

Images/cinvestav-1.jpg

Which Operations

We might want to have

Determine the height.

Determine the number of nodes.

Make a clone.

Determine if two binary trees are clones.

Maybe, we want

Display the binary tree.

Evaluate the arithmetic expression represented by a binary tree.

Finally, we want

Obtain the in�x form of an expression.

Obtain the pre�x form of an expression.

Obtain the post�x form of an expression.

73 / 100

Images/cinvestav-1.jpg

Which Operations

We might want to have

Determine the height.

Determine the number of nodes.

Make a clone.

Determine if two binary trees are clones.

Maybe, we want

Display the binary tree.

Evaluate the arithmetic expression represented by a binary tree.

Finally, we want

Obtain the in�x form of an expression.

Obtain the pre�x form of an expression.

Obtain the post�x form of an expression.

73 / 100

Images/cinvestav-1.jpg

Which Operations

We might want to have

Determine the height.

Determine the number of nodes.

Make a clone.

Determine if two binary trees are clones.

Maybe, we want

Display the binary tree.

Evaluate the arithmetic expression represented by a binary tree.

Finally, we want

Obtain the in�x form of an expression.

Obtain the pre�x form of an expression.

Obtain the post�x form of an expression.

73 / 100

Images/cinvestav-1.jpg

Which Operations

We might want to have

Determine the height.

Determine the number of nodes.

Make a clone.

Determine if two binary trees are clones.

Maybe, we want

Display the binary tree.

Evaluate the arithmetic expression represented by a binary tree.

Finally, we want

Obtain the in�x form of an expression.

Obtain the pre�x form of an expression.

Obtain the post�x form of an expression.

73 / 100

Images/cinvestav-1.jpg

Which Operations

We might want to have

Determine the height.

Determine the number of nodes.

Make a clone.

Determine if two binary trees are clones.

Maybe, we want

Display the binary tree.

Evaluate the arithmetic expression represented by a binary tree.

Finally, we want

Obtain the in�x form of an expression.

Obtain the pre�x form of an expression.

Obtain the post�x form of an expression.

73 / 100

Images/cinvestav-1.jpg

Which Operations

We might want to have

Determine the height.

Determine the number of nodes.

Make a clone.

Determine if two binary trees are clones.

Maybe, we want

Display the binary tree.

Evaluate the arithmetic expression represented by a binary tree.

Finally, we want

Obtain the in�x form of an expression.

Obtain the pre�x form of an expression.

Obtain the post�x form of an expression.

73 / 100

Images/cinvestav-1.jpg

Which Operations

We might want to have

Determine the height.

Determine the number of nodes.

Make a clone.

Determine if two binary trees are clones.

Maybe, we want

Display the binary tree.

Evaluate the arithmetic expression represented by a binary tree.

Finally, we want

Obtain the in�x form of an expression.

Obtain the pre�x form of an expression.

Obtain the post�x form of an expression.

73 / 100

Images/cinvestav-1.jpg

Which Operations

We might want to have

Determine the height.

Determine the number of nodes.

Make a clone.

Determine if two binary trees are clones.

Maybe, we want

Display the binary tree.

Evaluate the arithmetic expression represented by a binary tree.

Finally, we want

Obtain the in�x form of an expression.

Obtain the pre�x form of an expression.

Obtain the post�x form of an expression.

73 / 100

Images/cinvestav-1.jpg

Outline

1 Trees: Basic ConceptsSubtrees, Leaves and Levels

2 Binary TreesBasic ConceptsThe Height Of a Binary TreeArithmetic ExpressionsPropertiesNumbering Nodes in A Full Binary TreeArray RepresentationLinked RepresentationTraversals

74 / 100

Images/cinvestav-1.jpg

Traversals

Something Notable

Many binary tree operations are done by performing a traversal of thebinary tree.

Actually

In a traversal, each element of the binary tree is visited exactly once.

Actions

During the visit of an element, all action (make a clone, display, evaluatethe operator, etc.) with respect to this element is taken.

75 / 100

Images/cinvestav-1.jpg

Traversals

Something Notable

Many binary tree operations are done by performing a traversal of thebinary tree.

Actually

In a traversal, each element of the binary tree is visited exactly once.

Actions

During the visit of an element, all action (make a clone, display, evaluatethe operator, etc.) with respect to this element is taken.

75 / 100

Images/cinvestav-1.jpg

Traversals

Something Notable

Many binary tree operations are done by performing a traversal of thebinary tree.

Actually

In a traversal, each element of the binary tree is visited exactly once.

Actions

During the visit of an element, all action (make a clone, display, evaluatethe operator, etc.) with respect to this element is taken.

75 / 100

Images/cinvestav-1.jpg

Binary Tree Traversal Methods

Thus, we have

Preorder

Inorder

Postorder

Level order

76 / 100

Images/cinvestav-1.jpg

Binary Tree Traversal Methods

Thus, we have

Preorder

Inorder

Postorder

Level order

76 / 100

Images/cinvestav-1.jpg

Binary Tree Traversal Methods

Thus, we have

Preorder

Inorder

Postorder

Level order

76 / 100

Images/cinvestav-1.jpg

Binary Tree Traversal Methods

Thus, we have

Preorder

Inorder

Postorder

Level order

76 / 100

Images/cinvestav-1.jpg

Remarks

First

In a traversal of a binary tree, each element of the binary tree is visitedexactly once.

Second

During the visit of an element, all action (make a clone, display, evaluatethe operator, etc.) with respect to this element is taken.

77 / 100

Images/cinvestav-1.jpg

Remarks

First

In a traversal of a binary tree, each element of the binary tree is visitedexactly once.

Second

During the visit of an element, all action (make a clone, display, evaluatethe operator, etc.) with respect to this element is taken.

77 / 100

Images/cinvestav-1.jpg

Preorder Traversals

Code

p u b l i c s t a t i c vo i d p reOrde r ( BinaryTreeNode t ){

i f ( t != n u l l ){

v i s i t ( t ) ;p reOrde r ( t . l e f t C h i l d ) ;p reOrde r ( t . r i g h t C h i l d ) ;

}}

78 / 100

Images/cinvestav-1.jpg

Example I : (visit = print)

Something Notable

79 / 100

Images/cinvestav-1.jpg

Example II : (visit = print)

Something Notable

80 / 100

Images/cinvestav-1.jpg

Actually the preorder can be used to express the pre�xform!!!

Example

81 / 100

Images/cinvestav-1.jpg

Inorder Traversals

Code

p u b l i c s t a t i c vo i d i nOrde r ( BinaryTreeNode t ){

i f ( t != n u l l ){

i nOrde r ( t . l e f t C h i l d ) ;v i s i t ( t ) ;i nOrde r ( t . r i g h t C h i l d ) ;

}}

82 / 100

Images/cinvestav-1.jpg

Example I : (visit = print)

Something Notable

83 / 100

Images/cinvestav-1.jpg

Example II : (visit = print)

Something Notable

84 / 100

Images/cinvestav-1.jpg

Example III : (visit = print)

Inorder By Projection (Squishing)

85 / 100

Images/cinvestav-1.jpg

Inorder Of Expression Tree

Example

Not only you get the in�x form

But, we have a implicit representation of the parenthesis

86 / 100

Images/cinvestav-1.jpg

Inorder Of Expression Tree

Example

Not only you get the in�x form

But, we have a implicit representation of the parenthesis

86 / 100

Images/cinvestav-1.jpg

Postorder Traversal

Code

p u b l i c s t a t i c vo i d pos tOrde r ( BinaryTreeNode t ){

i f ( t != n u l l ){

pos tOrde r ( t . l e f t C h i l d ) ;pos tOrde r ( t . r i g h t C h i l d ) ;v i s i t ( t ) ;

}}

87 / 100

Images/cinvestav-1.jpg

Example I : (visit = print)

Something Notable

88 / 100

Images/cinvestav-1.jpg

Example II : (visit = print)

Something Notable

89 / 100

Images/cinvestav-1.jpg

Actually the preorder can be used to express the post�xform!!!

Example

90 / 100

Images/cinvestav-1.jpg

Traversal Applications

First One

Make a Clone

Second

Determine the height.

Third

Determine number of nodes.

91 / 100

Images/cinvestav-1.jpg

Traversal Applications

First One

Make a Clone

Second

Determine the height.

Third

Determine number of nodes.

91 / 100

Images/cinvestav-1.jpg

Traversal Applications

First One

Make a Clone

Second

Determine the height.

Third

Determine number of nodes.

91 / 100

Images/cinvestav-1.jpg

Now

What if we want to traverse each node in every level �rst - level order

92 / 100

Images/cinvestav-1.jpg

Level Order

Code

Which one?

The code

93 / 100

Images/cinvestav-1.jpg

Level Order

Code

Which one?

The code

Let t be the t r e e r oo t .wh i l e ( t != n u l l ){

v i s i t t and put i t s c h i l d r e n on a FIFO queue ;remove a node from the FIFO queue and c a l l i t t ;// remove r e t u r n s n u l l when queue i s empty

}

93 / 100

Images/cinvestav-1.jpg

Binary Tree Construction

Question

Can you construct the binary tree, given two traversal sequences?

However

It depends on which two sequences are given.

Properties

94 / 100

Images/cinvestav-1.jpg

Binary Tree Construction

Question

Can you construct the binary tree, given two traversal sequences?

However

It depends on which two sequences are given.

Properties

94 / 100

Images/cinvestav-1.jpg

Binary Tree Construction

Question

Can you construct the binary tree, given two traversal sequences?

However

It depends on which two sequences are given.

Properties

94 / 100

Images/cinvestav-1.jpg

Preorder And Postorder

If we have the following

Something Notable

Preorder = ab

Postorder = ba

We have a problem

Preorder and postorder do not uniquely de�ne a binary tree.

Nor do preorder and level order (same example).

Nor do postorder and level order (same example).

95 / 100

Images/cinvestav-1.jpg

Preorder And Postorder

If we have the following

Something Notable

Preorder = ab

Postorder = ba

We have a problem

Preorder and postorder do not uniquely de�ne a binary tree.

Nor do preorder and level order (same example).

Nor do postorder and level order (same example).

95 / 100

Images/cinvestav-1.jpg

Preorder And Postorder

If we have the following

Something Notable

Preorder = ab

Postorder = ba

We have a problem

Preorder and postorder do not uniquely de�ne a binary tree.

Nor do preorder and level order (same example).

Nor do postorder and level order (same example).

95 / 100

Images/cinvestav-1.jpg

Preorder And Postorder

If we have the following

Something Notable

Preorder = ab

Postorder = ba

We have a problem

Preorder and postorder do not uniquely de�ne a binary tree.

Nor do preorder and level order (same example).

Nor do postorder and level order (same example).

95 / 100

Images/cinvestav-1.jpg

Preorder And Postorder

If we have the following

Something Notable

Preorder = ab

Postorder = ba

We have a problem

Preorder and postorder do not uniquely de�ne a binary tree.

Nor do preorder and level order (same example).

Nor do postorder and level order (same example).

95 / 100

Images/cinvestav-1.jpg

Preorder And Postorder

If we have the following

Something Notable

Preorder = ab

Postorder = ba

We have a problem

Preorder and postorder do not uniquely de�ne a binary tree.

Nor do preorder and level order (same example).

Nor do postorder and level order (same example).

95 / 100

Images/cinvestav-1.jpg

What if we have Inorder And Preorder

Example

inorder = g d h b e i a f j c

preorder = a b d g h e i c f j

Process

Scan the preorder left to right using the inorder to separate left and rightsubtrees.

Thus

a is the root of the tree; gdhbei are in the left subtree; fjc are in the rightsubtree.

96 / 100

Images/cinvestav-1.jpg

What if we have Inorder And Preorder

Example

inorder = g d h b e i a f j c

preorder = a b d g h e i c f j

Process

Scan the preorder left to right using the inorder to separate left and rightsubtrees.

Thus

a is the root of the tree; gdhbei are in the left subtree; fjc are in the rightsubtree.

96 / 100

Images/cinvestav-1.jpg

What if we have Inorder And Preorder

Example

inorder = g d h b e i a f j c

preorder = a b d g h e i c f j

Process

Scan the preorder left to right using the inorder to separate left and rightsubtrees.

Thus

a is the root of the tree; gdhbei are in the left subtree; fjc are in the rightsubtree.

96 / 100

Images/cinvestav-1.jpg

Then

First

preorder = a b d g h e i c f j

97 / 100

Images/cinvestav-1.jpg

Then

First

preorder = a b d g h e i c f j

97 / 100

Images/cinvestav-1.jpg

Cont...

d is the next root; g is in the left subtree; h is in the right subtree.

98 / 100

Images/cinvestav-1.jpg

Inorder And Postorder

Thus

Scan postorder from right to left using inorder to separate left and rightsubtrees.

Example

inorder = g d h b e i a f j c

postorder = g h d i e b j f c a

Thus

Tree root is a; gdhbei are in left subtree; fjc are in right subtree.

99 / 100

Images/cinvestav-1.jpg

Inorder And Postorder

Thus

Scan postorder from right to left using inorder to separate left and rightsubtrees.

Example

inorder = g d h b e i a f j c

postorder = g h d i e b j f c a

Thus

Tree root is a; gdhbei are in left subtree; fjc are in right subtree.

99 / 100

Images/cinvestav-1.jpg

Inorder And Postorder

Thus

Scan postorder from right to left using inorder to separate left and rightsubtrees.

Example

inorder = g d h b e i a f j c

postorder = g h d i e b j f c a

Thus

Tree root is a; gdhbei are in left subtree; fjc are in right subtree.

99 / 100

Images/cinvestav-1.jpg

Inorder And Postorder

Thus

Scan postorder from right to left using inorder to separate left and rightsubtrees.

Example

inorder = g d h b e i a f j c

postorder = g h d i e b j f c a

Thus

Tree root is a; gdhbei are in left subtree; fjc are in right subtree.

99 / 100

Images/cinvestav-1.jpg

Inorder And Level Order

Thus

Scan level order from left to right using inorder to separate left and rightsubtrees.

Example

inorder = g d h b e i a f j c

level order = a b c d e f g h i j

Thus

Tree root is a; gdhbei are in left subtree; fjc are in right subtree.

100 / 100

Images/cinvestav-1.jpg

Inorder And Level Order

Thus

Scan level order from left to right using inorder to separate left and rightsubtrees.

Example

inorder = g d h b e i a f j c

level order = a b c d e f g h i j

Thus

Tree root is a; gdhbei are in left subtree; fjc are in right subtree.

100 / 100

Images/cinvestav-1.jpg

Inorder And Level Order

Thus

Scan level order from left to right using inorder to separate left and rightsubtrees.

Example

inorder = g d h b e i a f j c

level order = a b c d e f g h i j

Thus

Tree root is a; gdhbei are in left subtree; fjc are in right subtree.

100 / 100

Images/cinvestav-1.jpg

Inorder And Level Order

Thus

Scan level order from left to right using inorder to separate left and rightsubtrees.

Example

inorder = g d h b e i a f j c

level order = a b c d e f g h i j

Thus

Tree root is a; gdhbei are in left subtree; fjc are in right subtree.

100 / 100