22
Drawing Binary Tree (Console- based)

In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

Embed Size (px)

Citation preview

Page 1: In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

Drawing Binary Tree (Console-based)

Page 2: In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

In a rectangular grid, each cell is denoted by a pair (level, column)

A rectangular grid

0 1 2 3 4

0 1

: (0, 1)

Page 3: In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

A

B C

D E

A

B C

D E

Column: 0 1 2 3 4 0

1 2

level

Graphical drawing Console-based drawing

If we can denote each node using a pair (level, column), we can draw a tree in thegrid.

The issue is how do we denote each node using a pair?

Page 4: In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

A

B C

D E

A

B C

D E

Column: 0 1 2 3 4 0

1 2

level

The in-order scan: B, D, A, E, C Column order

Question: What is the in-order traverse of this tree?

Page 5: In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

A

B C

D E

A

B C

D E

Column: 0 1 2 3 4 0

1 2

level

Level: 0

To observe the level order is straight forward.

Level: 1 Level: 1

Level: 2 Level: 2

Page 6: In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

Solve the ProblemInput: a tree with node structureDraw it in a way such that each node can be

denoted by a pair (level, column) in a grid

Left

Value

Right

Page 7: In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

AlgorithmGiven a tree T with node structureBuild a new tree S with node structure

Use In-order scan to traverse tree TDraw tree based on the newly built tree

A queue is needed for a level-order scan.

Left Value

Right

Left Value Level Column Right

Page 8: In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

Example of building new tree

A

B C

D E

Column: 2Level: 0

Column: 0Level: 1

Column: 4Level: 1

Column: 1Level: 2

Column: 3Level: 2

A

B C

D E

Page 9: In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

Example of drawing tree

A

B C

D E

Draw the tree in the order of A, B ,C , D ,EAdd indentation(Space) when needed.

Page 10: In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

Example of drawing tree

A

B C

D E

In order to get the order “A, B ,C , D ,E ”.

APush A

Page 11: In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

Example of drawing tree

A

B C

D E

In order to get the order “A, B ,C , D ,E ”.

A B

Push children of A

Page 12: In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

Example of drawing tree

A

B C

D E

In order to get the order “A, B ,C , D ,E ”.

A B C

Push children of A

Page 13: In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

Example of drawing tree

A

B C

D E

In order to get the order “A, B ,C , D ,E ”.

B C

{A}

Pop A

Page 14: In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

Example of drawing tree

A

B C

D E

In order to get the order “A, B ,C , D ,E ”.

B C

{A}

Page 15: In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

Example of drawing tree

A

B C

D E

In order to get the order “A, B ,C , D ,E ”.

B C D

{A}

Page 16: In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

Example of drawing tree

A

B C

D E

In order to get the order “A, B ,C , D ,E ”.

C D

{A, B}

Pop(B)

Page 17: In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

Example of drawing tree

A

B C

D E

In order to get the order “A, B ,C , D ,E ”.

C D E

{A, B}

Page 18: In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

Example of drawing tree

A

B C

D E

In order to get the order “A, B ,C , D ,E ”.

C D E

{A, B,C}

Page 19: In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

Example of drawing tree

A

B C

D E

In order to get the order “A, B ,C , D ,E ”.

D E

{A, B,C}

Page 20: In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

Example of drawing tree

A

B C

D E

In order to get the order “A, B ,C , D ,E ”.

D E

{A, B,C, D}

Page 21: In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

Example of drawing tree

A

B C

D E

In order to get the order “A, B ,C , D ,E ”.

{A, B,C, D, E}

Page 22: In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid 0 1 2 3 4 0 1 : (0, 1)

Example of drawing tree

A

B C

D E

Indentations can be any size of space. I used 1 space for each cell in the grid.