in a rectangular grid, each cell is denoted by a pair (level, column) a rectangular grid 0 1 2 3 4 0...
TRANSCRIPT
![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)](https://reader036.vdocuments.us/reader036/viewer/2022062719/56649ec65503460f94bd1efa/html5/thumbnails/1.jpg)
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)](https://reader036.vdocuments.us/reader036/viewer/2022062719/56649ec65503460f94bd1efa/html5/thumbnails/2.jpg)
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)](https://reader036.vdocuments.us/reader036/viewer/2022062719/56649ec65503460f94bd1efa/html5/thumbnails/3.jpg)
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)](https://reader036.vdocuments.us/reader036/viewer/2022062719/56649ec65503460f94bd1efa/html5/thumbnails/4.jpg)
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)](https://reader036.vdocuments.us/reader036/viewer/2022062719/56649ec65503460f94bd1efa/html5/thumbnails/5.jpg)
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)](https://reader036.vdocuments.us/reader036/viewer/2022062719/56649ec65503460f94bd1efa/html5/thumbnails/6.jpg)
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)](https://reader036.vdocuments.us/reader036/viewer/2022062719/56649ec65503460f94bd1efa/html5/thumbnails/7.jpg)
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)](https://reader036.vdocuments.us/reader036/viewer/2022062719/56649ec65503460f94bd1efa/html5/thumbnails/8.jpg)
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)](https://reader036.vdocuments.us/reader036/viewer/2022062719/56649ec65503460f94bd1efa/html5/thumbnails/9.jpg)
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)](https://reader036.vdocuments.us/reader036/viewer/2022062719/56649ec65503460f94bd1efa/html5/thumbnails/10.jpg)
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)](https://reader036.vdocuments.us/reader036/viewer/2022062719/56649ec65503460f94bd1efa/html5/thumbnails/11.jpg)
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)](https://reader036.vdocuments.us/reader036/viewer/2022062719/56649ec65503460f94bd1efa/html5/thumbnails/12.jpg)
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)](https://reader036.vdocuments.us/reader036/viewer/2022062719/56649ec65503460f94bd1efa/html5/thumbnails/13.jpg)
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)](https://reader036.vdocuments.us/reader036/viewer/2022062719/56649ec65503460f94bd1efa/html5/thumbnails/14.jpg)
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)](https://reader036.vdocuments.us/reader036/viewer/2022062719/56649ec65503460f94bd1efa/html5/thumbnails/15.jpg)
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)](https://reader036.vdocuments.us/reader036/viewer/2022062719/56649ec65503460f94bd1efa/html5/thumbnails/16.jpg)
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)](https://reader036.vdocuments.us/reader036/viewer/2022062719/56649ec65503460f94bd1efa/html5/thumbnails/17.jpg)
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)](https://reader036.vdocuments.us/reader036/viewer/2022062719/56649ec65503460f94bd1efa/html5/thumbnails/18.jpg)
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)](https://reader036.vdocuments.us/reader036/viewer/2022062719/56649ec65503460f94bd1efa/html5/thumbnails/19.jpg)
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)](https://reader036.vdocuments.us/reader036/viewer/2022062719/56649ec65503460f94bd1efa/html5/thumbnails/20.jpg)
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)](https://reader036.vdocuments.us/reader036/viewer/2022062719/56649ec65503460f94bd1efa/html5/thumbnails/21.jpg)
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)](https://reader036.vdocuments.us/reader036/viewer/2022062719/56649ec65503460f94bd1efa/html5/thumbnails/22.jpg)
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.