羅德興老師的教學歷程檔案 - 108-2 資料結構 - Tree (CH06) (期末分組) |
|
|
Tree (CH06) (期末分組)''' # Binarytree Module in Python A binary tree is a data structure in which every node or vertex has atmost two children. In Python, a binary tree can be represented in different ways with different data structures(dictionary, list) and class representation for a node. However, binarytree library helps to directly implement a binary tree. It also supports heap and binary search tree(BST). This module does not come pre-installed with Python’s standard utility module. To install it type the below command in the terminal. pip install binarytree Creating Node The node class represents the structure of a particular node in the binary tree. The attributes of this class are values, left, right. Syntax: binarytree.Node(value, left=None, right=None) Parameters: value: Contains the data for a node. This value must be number. left: Conatins the details of left node child. right: Contains details of the right node child Note: If left or right child node is not an instance of binarytree.Node class then binarytree.exceptions.NodeTypeError is raised and if the node value is not a number then binarytree.exceptions.NodeValueError is raised. ''' # This is ch06-tree.py by 0000000XXX (學號) YYYYYY(姓名) on 2020/06/05 # This is 展示 Bibary tree (二元樹)的實作 # 程式的前兩行務必註明:學號、姓名、題號、及用途 from binarytree import Node root = Node(3) root.left = Node(6) root.right = Node(8) # Getting binary tree print('Binary tree (二元樹) :', root) # Getting list of nodes print('List of nodes (列出節點) :', list(root)) # Getting inorder of nodes print('Inorder of nodes (節點的中序) :', root.inorder) # Checking tree properties print('Size of tree (樹的大小) :', root.size) print('Height of tree (樹的高度):', root.height) # Get all properties at once print('Properties of tree (樹的屬性): \n', root.properties) ''' 作業6: # EX6-1. 從一個 list 的 10 個數值 依序填滿建立一個二元樹,並印出其值。 # 使用 from binarytree import build # EX6-2. 輸入樹高,建立一個亂數數值的二元樹,並印出其值。 # 使用 from binarytree import tree # EX6-3. 輸入樹高,建立一個二元搜尋樹,並印出其值。 # 使用 from binarytree import bst # EX6-4. 輸入樹高,建立一個二元搜尋樹,並印出其值。 # 使用 from binarytree import heap # EX6-5. 使用前序追蹤該二元樹 (MLR) # EX6-6. 使用中序追蹤該二元樹 (LMR) # EX6-7. 使用後序追蹤該二元樹 (LRM) ''' # This is ch06-tree.py by 0000000XXX (學號) YYYYYY(姓名) on 2020/06/05 # This is 展示 Bibary tree (二元樹)的實作 # 程式的前兩行務必註明:學號、姓名、題號、及用途 # EX6-2. 輸入樹高,建立一個亂數數值的二元樹,並印出其值。 # 使用 from binarytree import tree ''' Build a random binary tree tree() generates a random binary tree and returns its root node. Syntax: binarytree.tree(height=3, is_perfect=False) Parameters: height: It is the height of the tree and its value can be between the range 0-9 (inclusive) is_perfect: If set True a perfect binary is created. Returns: Root node of the binary tree. ''' from binarytree import tree print ('Ex6-2 \n') # Create a random binary # tree of any height root3 = tree(height = 3, is_perfect = True) print("Perfect binary tree of given height :") print(root3) # 建立 樹的節點 from binarytree import Node root = Node(3) root.left = Node(6) root.right = Node(8) root[1] = Node(5) root[1].left = Node(15) root[1].right = Node(7) root[2] = Node(9) root[2].left = Node(10) root[2].right = Node(2) for i in range (0,3,1): print('%d \n' % i) print (root[i]) print (root[i].left) print (root[i].right)
|
|
中華科技大學數位化學習歷程 - 意見反應 |