羅德興老師的教學歷程檔案 - 112-2 資料結構 (2024年) - Unit 7 樹 (Tree)
 

企業資訊與管理系
助理教授/日導
羅德興


歷程檔案 Portfolio

    Unit 7 樹 (Tree)


    範例與作業


    一、
    # This is ds-ex7-0(tree).ipynb by 0000000XXX (學號)  YYYYYY(姓名) on 2023/05/15
    # This is 展示  Bibary tree (二元樹)的實作 (土法煉鋼)
    # 程式的前兩行務必註明:學號、姓名、題號、及用途
    # 資料來源:https://github.com/abg3/Python-Data-Structures/blob/master/Python%20Data%20Structures%20-%20Binary%20Trees.ipynb
    '''
    The Node class has three attributes:
    self.value
    self.left
    self.right
    '''

    class Node(object):
        def __init__(self, value):
            self.value = value
            self.left = None
            self.right = None


    class BinaryTree(object):
        def __init__(self, root):
            self.root = Node(root)

        def print_tree(self, traversal_type):
            if traversal_type == "preorder":
                return self.preorder_print(tree.root, "")
            elif traversal_type == "inorder":
                return self.inorder_print(tree.root, "")
            elif traversal_type == "postorder":
                return self.postorder_print(tree.root, "")

            else:
                print("Traversal type " + str(traversal_type) + " is not supported.")
                return False

        def preorder_print(self, start, traversal):
            """Root->Left->Right"""
            if start:
                traversal += (str(start.value) + "-")
                traversal = self.preorder_print(start.left, traversal)
                traversal = self.preorder_print(start.right, traversal)
            return traversal

        def inorder_print(self, start, traversal):
            """Left->Root->Right"""
            if start:
                traversal = self.inorder_print(start.left, traversal)
                traversal += (str(start.value) + "-")
                traversal = self.inorder_print(start.right, traversal)
            return traversal

        def postorder_print(self, start, traversal):
            """Left->Right->Root"""
            if start:
                traversal = self.postorder_print(start.left, traversal)
                traversal = self.postorder_print(start.right, traversal)
                traversal += (str(start.value) + "-")
            return traversal
    # 1-2-4-5-3-6-7-
    # 4-2-5-1-6-3-7
    # 4-5-2-6-7-3-1
    #               1
    #           /       \  
    #          2          3  
    #         /  \      /   \
    #        4    5     6   7

    # Set up tree:
    tree = BinaryTree(1)
    tree.root.left = Node(2)
    tree.root.right = Node(3)
    tree.root.left.left = Node(4)
    tree.root.left.right = Node(5)
    tree.root.right.left = Node(6)
    tree.root.right.right = Node(7)

    print('Preorder:\n',tree.print_tree("preorder"))
    print('Inorder:\n', tree.print_tree("inorder"))
    print('Postorder:\n', tree.print_tree("postorder"))
    print(tree.print_tree("laworder"))






    二、
    # This is ds-ex7-1(tree).ipynb by 0000000XXX (學號)  YYYYYY(姓名) on 2023/05/15
    # This is 展示  Bibary tree (二元樹)的實作
    # 程式的前兩行務必註明:學號、姓名、題號、及用途

    '''
    作業7:
    # EX7-1. 從一個 list 的 10 個數值 依序填滿建立一個二元樹,並印出其值。
    # 使用 from binarytree import build
    # EX7-2. 輸入樹高,建立一個亂數數值的二元樹,並印出其值。
    # 使用 from binarytree import tree
    # EX7-3. 輸入樹高,建立一個二元搜尋樹,並印出其值。
    # 使用 from binarytree import bst  
    # EX7-4. 輸入樹高,建立一個二元搜尋樹,並印出其值。
    # 使用 from binarytree import heap  
    # EX7-5. 使用前序追蹤該二元樹 (MLR)
    # EX7-6. 使用中序追蹤該二元樹 (LMR)
    # EX7-7. 使用後序追蹤該二元樹 (LRM)
    '''


    # !pip install binarytree --upgrade
    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)








    三、
    # This is ds-ex7-2(tree).ipynb by 0000000XXX (學號)  YYYYYY(姓名) on 2023/05/15
    # This is 展示  Bibary tree (二元樹)的實作
    # 程式的前兩行務必註明:學號、姓名、題號、及用途

    # EX7-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.

    '''

    # !pip install binarytree --upgrade
    from binarytree import tree
    print ('Ex7-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:', root3)


    # 建立 樹的節點
    from binarytree import Node
    print('建立 樹 的節點 中...... ')
    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)
    print('建立 樹 的節點 完畢\n')

    for i in range (0,3,1):
      print('現在是 Root :  %d 的子樹 \n ' % i)
      print (root[i])
      print (root[i].left)
      print (root[i].right)



    全部共 17則留言
    周育賢06-26 10:49:7-1https://colab.research.google.com/drive/1IpZU68B2h1J0DI8yputZyqfXd7XiXick
    周育賢06-26 10:50:7-2https://colab.research.google.com/drive/1MmFWMDYjI0Q8LcYZ9pLCG6y5PegtFnz7
    周育賢06-26 10:50:7-3https://colab.research.google.com/drive/1iRKuqyXF-wf6HgC0ed8no8POALY-_jSa
    周育賢06-26 10:51:7-4https://colab.research.google.com/drive/1zpfk7-uFEcSXuzkJIVMTWUuU_zu4VpT2
    周育賢06-26 10:51:7-5https://colab.research.google.com/drive/158AhDONATLg1eeQshxUPl-IgmSxH_jbC
    周育賢06-26 10:52:7-6https://colab.research.google.com/drive/1N_ZWtJmeRmhzwsxt91FaKhuGQu2_UKa4
    周育賢06-26 10:52:7-7https://colab.research.google.com/drive/1VWpp6PtFpEwGsl6xhkLD50RcJijss4mU
    周凱琪06-26 11:06:7-5 https://colab.research.google.com/drive/1-tltPT5nj39maKdbVn8eVBSIC_eHVUv6
    周凱琪06-26 11:06:7-6 https://colab.research.google.com/drive/1JyjxLFj6--5IoHvNA2cYy7redEiRTH7B
    周凱琪06-26 11:07:7-7 https://colab.research.google.com/drive/1DvsaeoZ-fjcpNnlCvDpt3qIqAPxJ7MDb
    06-26 11:43:https://colab.research.google.com/drive/1Wd7zt0B3QcOilBVFogboGvw4-sQ8pjxD
    王玉香06-26 12:19:7-1 https://colab.research.google.com/drive/1favM26snUK3DwsRJNt5oJPLGumhmiiJp
    林昱安05-12 10:13:https://colab.research.google.com/drive/1ugdGHoBUgmrZ5KtmCIfpfOFFsgpSKx3J?usp=sharing
    林旻毅05-12 10:14:https://colab.research.google.com/drive/1NTSCZT7yCmt_mXt1RI7OsBdnM_pFJnHk?usp=sharing
    陳法禎06-02 13:18:https://colab.research.google.com/drive/1xEcs4r9yN8QHaXfzFTVFK-aiovXWUnwr?usp=sharing
    歐千郁06-16 17:58:https://colab.research.google.com/drive/1qTQqZrg-Bs0m3qKSE7fVNIka5-l7h8Eq#scrollTo=gTQbMUFAfDoz&line=1&uniqifier=1
    歐千郁06-16 18:02:https://colab.research.google.com/drive/1qTQqZrg-Bs0m3qKSE7fVNIka5-l7h8Eq#scrollTo=gTQbMUFAfDoz&line=1&uniqifier=1
    登入帳號密碼代表遵守學術網路規範


    文章分類 Labels


    最新文章 Top10

    中華科技大學數位化學習歷程 - 意見反應