Cây đại diện cho các nút được kết nối bởi các cạnh. Nó là một cấu trúc dữ liệu phi tuyến tính. Nó có các thuộc tính sau - Show
Nội phân Chính showShow
Chúng tôi tạo một cấu trúc dữ liệu cây trong Python bằng cách sử dụng nút OS Concept được thảo luận trước đó. Chúng tôi chỉ định một nút là nút gốc và sau đó thêm nhiều nút hơn làm nút con. Dưới đây là chương trình để tạo nút gốc. Tạo rootChèn vào một cái cây Đi qua một cái câyclass Node: def __init__(self, data): self.left = None self.right = None self.data = data def PrintTree(self): print(self.data) root = Node(10) root.PrintTree() Thuật toán truyền tải câyTheo đơn đặt hàng 10 Chèn vào một cái câyĐi qua một cái cây Đi qua một cái câyclass Node: def __init__(self, data): self.left = None self.right = None self.data = data def insert(self, data): # Compare the new value with the parent node if self.data: if data < self.data: if self.left is None: self.left = Node(data) else: self.left.insert(data) elif data > self.data: if self.right is None: self.right = Node(data) else: self.right.insert(data) else: self.data = data # Print the tree def PrintTree(self): if self.left: self.left.PrintTree() print( self.data), if self.right: self.right.PrintTree() # Use the insert method to add nodes root = Node(12) root.insert(6) root.insert(14) root.insert(3) root.PrintTree() Thuật toán truyền tải câyTheo đơn đặt hàng 3 6 12 14 Đi qua một cái câyThuật toán truyền tải cây Thuật toán truyền tải câyTheo đơn đặt hàng
Theo đơn đặt hàngTraversal đặt hàng trước Traversal sau đơn đặt hàng Python có một cái cây được xây dựng không? Đi qua một cái câyclass Node: def __init__(self, data): self.left = None self.right = None self.data = data # Insert Node def insert(self, data): if self.data: if data < self.data: if self.left is None: self.left = Node(data) else: self.left.insert(data) else data > self.data: if self.right is None: self.right = Node(data) else: self.right.insert(data) else: self.data = data # Print the Tree def PrintTree(self): if self.left: self.left.PrintTree() print( self.data), if self.right: self.right.PrintTree() # Inorder traversal # Left -> Root -> Right def inorderTraversal(self, root): res = [] if root: res = self.inorderTraversal(root.left) res.append(root.data) res = res + self.inorderTraversal(root.right) return res root = Node(27) root.insert(14) root.insert(35) root.insert(10) root.insert(19) root.insert(31) root.insert(42) print(root.inorderTraversal(root)) Thuật toán truyền tải câyTheo đơn đặt hàng [10, 14, 19, 27, 31, 35, 42] Traversal đặt hàng trướcTraversal sau đơn đặt hàng Python có một cái cây được xây dựng không? Làm thế nào để bạn làm một cái cây trong Python? Đi qua một cái câyclass Node: def __init__(self, data): self.left = None self.right = None self.data = data # Insert Node def insert(self, data): if self.data: if data < self.data: if self.left is None: self.left = Node(data) else: self.left.insert(data) elif data > self.data: if self.right is None: self.right = Node(data) else: self.right.insert(data) else: self.data = data # Print the Tree def PrintTree(self): if self.left: self.left.PrintTree() print( self.data), if self.right: self.right.PrintTree() # Preorder traversal # Root -> Left ->Right def PreorderTraversal(self, root): res = [] if root: res.append(root.data) res = res + self.PreorderTraversal(root.left) res = res + self.PreorderTraversal(root.right) return res root = Node(27) root.insert(14) root.insert(35) root.insert(10) root.insert(19) root.insert(31) root.insert(42) print(root.PreorderTraversal(root)) Thuật toán truyền tải câyTheo đơn đặt hàng [27, 14, 10, 19, 35, 31, 42] Traversal sau đơn đặt hàngPython có một cái cây được xây dựng không? Làm thế nào để bạn làm một cái cây trong Python? Nút và cây là gì? Đi qua một cái câyclass Node: def __init__(self, data): self.left = None self.right = None self.data = data # Insert Node def insert(self, data): if self.data: if data < self.data: if self.left is None: self.left = Node(data) else: self.left.insert(data) else if data > self.data: if self.right is None: self.right = Node(data) else: self.right.insert(data) else: self.data = data # Print the Tree def PrintTree(self): if self.left: self.left.PrintTree() print( self.data), if self.right: self.right.PrintTree() # Postorder traversal # Left ->Right -> Root def PostorderTraversal(self, root): res = [] if root: res = self.PostorderTraversal(root.left) res = res + self.PostorderTraversal(root.right) res.append(root.data) return res root = Node(27) root.insert(14) root.insert(35) root.insert(10) root.insert(19) root.insert(31) root.insert(42) print(root.PostorderTraversal(root)) Thuật toán truyền tải câyTheo đơn đặt hàng [10, 19, 14, 31, 42, 35, 27] Python có một cái cây được xây dựng không?Làm thế nào để bạn làm một cái cây trong Python?, but it's easily implemented with classes. Làm thế nào để bạn làm một cái cây trong Python?Nút và cây là gì?creating a Node class that will represent a single node. This Node class will contain 3 variables; the first is the left pointing to the left child, the second variable data containing the value for that node, and the right variable pointing to the right child. Nút và cây là gì?Một cây là một tập hợp các thực thể gọi là các nút.Các nút được kết nối bởi các cạnh.Mỗi nút chứa một giá trị hoặc dữ liệu và nó có thể có hoặc không có nút con.Nút đầu tiên của cây được gọi là gốc. . Nodes are connected by edges . Each node contains a value or data , and it may or may not have a child node . The first node of the tree is called the root . |