Tôi đã tạo lớp tùy chỉnh cho các nút
class NodeTree[object]:
def __init__[self, name = None, children = None]:
self.name = name
self.children = children
và xác định một chức năng tạo ra một cây [một nút chứa các nút con cái của nó]
def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
Đầu vào là một dict với một đối số cho tên nút và danh sách với con của nó ở cùng một hình thức với cha mẹ. Chức năng hoạt động tốt và tôi đã tạo ra phương pháp chứng minh điều đó nhưng tôi không thể tìm ra cách để vượt qua nó ngay và có được chiều cao của cây. Tôi không biết liệu "chiều cao" đó là thuật ngữ đúng vì tôi biết nó có thể là mơ hồ, tôi cần coi nút là một đơn vị đo lường, như thế này:
parent
|
|
---------
| |
child child
Chiều cao của cây này là 2, tôi đã thử tất cả mọi thứ, từ quầy đến thẻ trong lớp, mọi thứ dường như thoái hóa tôi không bao giờ có được chiều cao phù hợp. Làm thế nào tôi nên tiếp cận điều đó?
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Examples:
Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 / / | \ 10 11 12 13 Output: 1 2 5 10 6 11 12 13 3 4 7 8 9 Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 Output: 1 2 5 6 3 4 7 8 9
Bàn luận
Iterative Preorder Traversal of Binary Tree.
Cho một cây K-ary. Nhiệm vụ là viết một chương trình lặp đi lặp lại để thực hiện giao dịch trước của cây n-ary đã cho. Two Cases have been taken care of in this Iterative Preorder Traversal Algorithm:
- Truyền hàng trước của một cây n-ary tương tự như đường truyền trước của cây tìm kiếm nhị phân hoặc cây nhị phân với sự khác biệt duy nhất là, tất cả các nút con của cha mẹ đều đi từ trái sang phải trong một chuỗi. Cây nhị phân.
- Các trường hợp cần xử lý trong quá trình truyền tải: Hai trường hợp đã được chăm sóc trong thuật toán truyền tải trước lần lặp đi lặp lại này: & nbsp;
Bật nút trên cùng từ ngăn xếp - trên cùng từ ngăn xếp và chèn nó vào danh sách các nút đã truy cập.: In the below python implementation, a “dequeue” is used to implement the stack instead of a list because of its efficient append and pop operations.
Đẩy tất cả các nút trẻ của đỉnh vào ngăn xếp từ phải sang trái vì đường truyền từ ngăn xếp sẽ được thực hiện theo thứ tự ngược lại. Kết quả là, Traversal đặt hàng chính xác đạt được.
C++
#include
LƯU Ý: Trong triển khai Python dưới đây, một dequeue trực tiếp được sử dụng để thực hiện ngăn xếp thay vì một danh sách vì các hoạt động tăng thêm hiệu quả của nó.
Dưới đây là việc thực hiện phương pháp trên: & nbsp; & nbsp;
using
namespace
std;
class
def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
0def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
7def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
1def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
2 parent
|
|
---------
| |
child child
2 parent
|
|
---------
| |
child child
3def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3 parent
|
|
---------
| |
child child
5def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3 parent
|
|
---------
| |
child child
7 parent
|
|
---------
| |
child child
8def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
4 def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
5 parent
|
|
---------
| |
child child
5def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 / / | \ 10 11 12 13 Output: 1 2 5 10 6 11 12 13 3 4 7 8 9 Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 Output: 1 2 5 6 3 4 7 8 93
def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 / / | \ 10 11 12 13 Output: 1 2 5 10 6 11 12 13 3 4 7 8 9 Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 Output: 1 2 5 6 3 4 7 8 95
def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
4Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 / / | \ 10 11 12 13 Output: 1 2 5 10 6 11 12 13 3 4 7 8 9 Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 Output: 1 2 5 6 3 4 7 8 97
def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 / / | \ 10 11 12 13 Output: 1 2 5 10 6 11 12 13 3 4 7 8 9 Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 Output: 1 2 5 6 3 4 7 8 99
def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
9def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
4 parent
|
|
---------
| |
child child
1 parent
|
|
---------
| |
child child
21 2 5 10 6 11 12 13 3 4 7 8 94
parent
|
|
---------
| |
child child
21 2 5 10 6 11 12 13 3 4 7 8 96
parent
|
|
---------
| |
child child
21 2 5 10 6 11 12 13 3 4 7 8 98
parent
|
|
---------
| |
child child
9 Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 / / | \ 10 11 12 13 Output: 1 2 5 10 6 11 12 13 3 4 7 8 9 Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 Output: 1 2 5 6 3 4 7 8 90
#include
4#include
5
def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
31 2 5 10 6 11 12 13 3 4 7 8 91
1 2 5 10 6 11 12 13 3 4 7 8 92
def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3 parent
|
|
---------
| |
child child
7 parent
|
|
---------
| |
child child
2#include
0 #include
1def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
4 #include
3 parent
|
|
---------
| |
child child
2using
6using
7using
8def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3 parent
|
|
---------
| |
child child
7def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3namespace
2 parent
|
|
---------
| |
child child
7 parent
|
|
---------
| |
child child
22 parent
|
|
---------
| |
child child
5def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3#include
0 #include
1using
3 using
4def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
4 namespace
5def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3namespace
8namespace
9 std;
0def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3std;
2namespace
9 std;
4def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3std;
2namespace
9 std;
8def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3class
8def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3std;
2namespace
9 class
2def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3class
4namespace
9 class
6def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
07 parent
|
|
---------
| |
child child
2namespace
9 def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
01def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
07def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3class
4namespace
9 def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
05def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
07 parent
|
|
---------
| |
child child
2namespace
9 def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
10 parent
|
|
---------
| |
child child
2namespace
9 def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
15 parent
|
|
---------
| |
child child
2namespace
9 def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
20def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
222namespace
9 def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
24def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
34 parent
|
|
---------
| |
child child
7Python3
def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
222namespace
9 def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
28def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
222namespace
9 def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
32def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
36 def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
37def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
38 def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
39class
def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
41 parent
|
|
---------
| |
child child
2def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
45def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
54def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
50def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
56def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
43 def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
44def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
45def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
46 parent
|
|
---------
| |
child child
2def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
45def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
49def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
50 def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
51def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
64def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
50def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
56def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
68def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
70def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
43 def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
58def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
60def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
50 def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
62def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
31 2 5 10 6 11 12 13 3 4 7 8 91
def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
73def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
74def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
75def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
2 parent
|
|
---------
| |
child child
2def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
78def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
50 def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
75 parent
|
|
---------
| |
child child
2def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
99def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
2 parent
|
|
---------
| |
child child
2def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
82 def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
73def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
84def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
73__#include
4
def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
95def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
50 def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
97#include
4
parent
|
|
---------
| |
child child
02def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
50 parent
|
|
---------
| |
child child
04def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
73def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
86def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
87def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
88 parent
|
|
---------
| |
child child
09 parent
|
|
---------
| |
child child
2#include
0 parent
|
|
---------
| |
child child
1212____213 parent
|
|
---------
| |
child child
14#include
1def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
75 parent
|
|
---------
| |
child child
17def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
73 parent
|
|
---------
| |
child child
19 parent
|
|
---------
| |
child child
26 parent
|
|
---------
| |
child child
31 parent
|
|
---------
| |
child child
26 parent
|
|
---------
| |
child child
33 parent
|
|
---------
| |
child child
26 parent
|
|
---------
| |
child child
35using
8#include
4
def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
82 parent
|
|
---------
| |
child child
2222223 parent
|
|
---------
| |
child child
13 parent
|
|
---------
| |
child child
25 parent
|
|
---------
| |
child child
26def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
78def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
50 def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
88def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
3 parent
|
|
---------
| |
child child
47 parent
|
|
---------
| |
child child
48 parent
|
|
---------
| |
child child
2def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
82 def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
78def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
50def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
50 def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
75def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
2#include
4
def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
97 parent
|
|
---------
| |
child child
60 parent
|
|
---------
| |
child child
61 parent
|
|
---------
| |
child child
62 parent
|
|
---------
| |
child child
60 parent
|
|
---------
| |
child child
64 parent
|
|
---------
| |
child child
62 parent
|
|
---------
| |
child child
60 parent
|
|
---------
| |
child child
67 parent
|
|
---------
| |
child child
62 parent
|
|
---------
| |
child child
69def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
75 parent
|
|
---------
| |
child child
71 parent
|
|
---------
| |
child child
72 parent
|
|
---------
| |
child child
62 parent
|
|
---------
| |
child child
69def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
75 parent
|
|
---------
| |
child child
76def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
75 parent
|
|
---------
| |
child child
71 parent
|
|
---------
| |
child child
79 parent
|
|
---------
| |
child child
62 parent
|
|
---------
| |
child child
69def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
75 parent
|
|
---------
| |
child child
71 parent
|
|
---------
| |
child child
84 parent
|
|
---------
| |
child child
62 parent
|
|
---------
| |
child child
69def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
75 parent
|
|
---------
| |
child child
76def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
88 parent
|
|
---------
| |
child child
71 parent
|
|
---------
| |
child child
91 parent
|
|
---------
| |
child child
62 parent
|
|
---------
| |
child child
69def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
75 parent
|
|
---------
| |
child child
76def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
88 parent
|
|
---------
| |
child child
71 parent
|
|
---------
| |
child child
98 parent
|
|
---------
| |
child child
62 parent
|
|
---------
| |
child child
69def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
75 parent
|
|
---------
| |
child child
76def create_tree[d]:
x = NodeTree[]
for a in d.keys[]:
if type[d[a]] == str:
x.name = d[a]
if type[d[a]] == list:
if d[a] != []:
for b in d[a]:
x.add_child[create_tree[b]]
return x
88 parent
|
|
---------
| |
child child
71Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 / / | \ 10 11 12 13 Output: 1 2 5 10 6 11 12 13 3 4 7 8 9 Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 Output: 1 2 5 6 3 4 7 8 905
parent
|
|
---------
| |
child child
62 parent
|
|
---------
| |
child child
69 parent
|
|
---------
| |
child child
61 parent
|
|
---------
| |
child child
71Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 / / | \ 10 11 12 13 Output: 1 2 5 10 6 11 12 13 3 4 7 8 9 Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 Output: 1 2 5 6 3 4 7 8 910
parent
|
|
---------
| |
child child
62 parent
|
|
---------
| |
child child
69 parent
|
|
---------
| |
child child
61 parent
|
|
---------
| |
child child
71Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 / / | \ 10 11 12 13 Output: 1 2 5 10 6 11 12 13 3 4 7 8 9 Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 Output: 1 2 5 6 3 4 7 8 915
parent
|
|
---------
| |
child child
62 parent
|
|
---------
| |
child child
69 parent
|
|
---------
| |
child child
61 parent
|
|
---------
| |
child child
71Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 / / | \ 10 11 12 13 Output: 1 2 5 10 6 11 12 13 3 4 7 8 9 Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 Output: 1 2 5 6 3 4 7 8 920
parent
|
|
---------
| |
child child
62Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 / / | \ 10 11 12 13 Output: 1 2 5 10 6 11 12 13 3 4 7 8 9 Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 Output: 1 2 5 6 3 4 7 8 922
Đầu ra
1 2 5 10 6 11 12 13 3 4 7 8 9
Phân tích độ phức tạp:
- Độ phức tạp về thời gian: O [n], trong đó n là tổng số nút trong cây đã cho. Where n is the total number of nodes in the given tree.
- Không gian phụ trợ: O [h], trong đó h là chiều cao của cây đã cho O[h], Where h is the height of the given tree