-
-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathtree_traversal.emojic
143 lines (117 loc) Β· 3.53 KB
/
tree_traversal.emojic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
π¦ βΉ π
π β«
βοΈ π‘ β‘οΈ π‘ π
βͺοΈ π π πβΉβ«βοΈ π
β©οΈ π€The given tree is not binary!π€
π
β©οΈ π€π€
π
π
π π² π
ππ id π’
ππ children π¨ππ²π
π depth_count π’ children_count π’ π
1 β‘οΈ πid
π¨π β‘οΈ πchildren
ππ depth_count children_countβοΈ
π
π π βοΈ given_id π’ depth_count π’ children_count π’ π
given_id β‘οΈ πid
π¨π β‘οΈ πchildren
ππ depth_count children_countβοΈ
π
βοΈ π β‘οΈ π’ π
β©οΈ id
π
βοΈ π§ β‘οΈ π¨ππ²π π
β©οΈ children
π
π Depth-First Search Recursive pre-order π
βοΈ π π
π π‘ id 10βοΈβοΈ
π child children π
π childβοΈ
π
π
π Depth-First Search Recursive post-order π
βοΈ π₯ π
π child children π
π₯ childβοΈ
π
π π‘ id 10βοΈβοΈ
π
π
Depth-First Search Recursive Inorder Binary
This assumes only 2 children.
π
βοΈ π β‘οΈ π¬βΉ π
βͺοΈ π childrenβοΈ βΆοΈ 2 π
β©οΈ πβΉβ«βοΈ
π
βͺοΈ π childrenβοΈ βΆοΈ 0 π
ππ½ children 0βοΈβοΈ
π π‘ id 10βοΈβοΈ
ππ½ children 1βοΈβοΈ
π
π
π
π π‘ id 10βοΈβοΈ
π
β©οΈ π€·ββοΈ
π
π Depth-First Search Stack π
βοΈ π₯ π
π¨ π π β‘οΈ stack
π β π stackβοΈ π 0βοΈ π
π½ stack π stackβοΈ β 1βοΈ β‘οΈ temp
π¨ stack π stackβοΈ β 1βοΈ
π π‘ π tempβοΈ 10βοΈβοΈ
π§ tempβοΈ β‘οΈ temp_children
π child temp_children π
π» stack childβοΈ
π
π
π
π Breadth-First Search Queue π
βοΈ π’ π
π¨ π π β‘οΈ queue
π β π queueβοΈ π 0βοΈ π
π½ queue 0βοΈ β‘οΈ temp
π¨ queue 0βοΈ
π π‘ π tempβοΈ 10βοΈβοΈ
π§ tempβοΈ β‘οΈ temp_children
π child temp_children π
π» queue childβοΈ
π
π
π
π βοΈ π depth_count π’ children_count π’ π
βͺοΈ β depth_count βοΈπ 1βοΈ π
π i πβ©β© 0 children_countβοΈ π
π» children ππ²βοΈ π€id βοΈ 10 β i β 1π€ π€depth_count β 1π€ children_countβοΈβοΈ
π
π
π
π
π π
ππ²π 3 3βοΈ β‘οΈ tree
π π€Tree Traversalπ€οΈβοΈ
π π€π - Depth-First Search Recursive pre-orderπ€βοΈ
πtreeβοΈ
π π€π₯ - Depth-First Search Recursive post-orderπ€βοΈ
π₯treeβοΈ
π π€π₯ - Depth-First Search Stackπ€βοΈ
π₯treeβοΈ
π π€π’ - Breadth-First Search Queueπ€βοΈ
π’treeβοΈ
π π€π - Depth-First Search Recursive Inorder Binary - Errorπ€βοΈ
π Calling the Depth-First Search Recursive Inorder Binary method here does
π result in an error, since "tree" is not a binary tree.
οΈβͺοΈ πtreeβοΈ β‘οΈ return π
π π‘returnββοΈοΈ
π
ππ²π 3 2βοΈ β‘οΈ binary_tree
π π€π - Depth-First Search Recursive Inorder Binaryπ€βοΈ
οΈβͺοΈ πbinary_treeβοΈ β‘οΈ return π
π π‘returnββοΈοΈ
π
π