Download presentation
Presentation is loading. Please wait.
Published byLeo Malone Modified over 9 years ago
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)
3
A BC DE A BC DE 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 the grid. The issue is how do we denote each node using a pair?
4
A BC DE A BC DE Column: 0 1 2 3 4 0 1 2 level The in-order scan: B, D, A, E, CColumn order Question: What is the in-order traverse of this tree?
5
A BC DE A BC DE Column: 0 1 2 3 4 0 1 2 level Level: 0 To observe the level order is straight forward. Level: 1 Level: 2
6
Solve the Problem Input: a tree with node structure Draw it in a way such that each node can be denoted by a pair (level, column) in a grid LeftValueRight
7
Algorithm Given a tree T with node structure Build a new tree S with node structure Use In-order scan to traverse tree T Draw tree based on the newly built tree A queue is needed for a level-order scan. LeftValueRight LeftValueLevelColumnRight
8
Example of building new tree A BC DE Column: 2 Level: 0 Column: 0 Level: 1 Column: 4 Level: 1 Column: 1 Level: 2 Column: 3 Level: 2 A BC DE
9
Example of drawing tree A BC DE Draw the tree in the order of A, B,C, D,E Add indentation(Space) when needed.
10
Example of drawing tree A BC DE In order to get the order “A, B,C, D,E ”. A Push A
11
Example of drawing tree A BC DE In order to get the order “A, B,C, D,E ”. AB Push children of A
12
Example of drawing tree A BC DE In order to get the order “A, B,C, D,E ”. ABC Push children of A
13
Example of drawing tree A BC DE In order to get the order “A, B,C, D,E ”. BC {A} Pop A
14
Example of drawing tree A BC DE In order to get the order “A, B,C, D,E ”. BC {A}
15
Example of drawing tree A BC DE In order to get the order “A, B,C, D,E ”. BCD {A}
16
Example of drawing tree A BC DE In order to get the order “A, B,C, D,E ”. CD {A, B} Pop(B)
17
Example of drawing tree A BC DE In order to get the order “A, B,C, D,E ”. CDE {A, B}
18
Example of drawing tree A BC DE In order to get the order “A, B,C, D,E ”. CDE {A, B,C}
19
Example of drawing tree A BC DE In order to get the order “A, B,C, D,E ”. DE {A, B,C}
20
Example of drawing tree A BC DE In order to get the order “A, B,C, D,E ”. DE {A, B,C, D}
21
Example of drawing tree A BC DE In order to get the order “A, B,C, D,E ”. {A, B,C, D, E}
22
Example of drawing tree A BC DE Indentations can be any size of space. I used 1 space for each cell in the grid.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.