ixueaedu 2 lat temu
rodzic
commit
1f999ea7b5
3 zmienionych plików z 160 dodań i 135 usunięć
  1. 154 0
      tree/tree.py
  2. 6 29
      tree/whole_arrang.py
  3. 0 106
      utils.py

+ 154 - 0
tree/tree.py

@@ -0,0 +1,154 @@
+#列表函数
+def BinaryTree(r):
+    return [r, [], []]
+
+#插入左子树
+def insertLeft(root, newBranch):
+    t = root.pop(1)
+    if len(t) > 1:
+        root.insert(1, [newBranch, t, []])
+    else:
+        root.insert(1, [newBranch, [], []])
+    return root
+
+#插入右子树
+def insertRight(root, newBranch):
+    t = root.pop(2)
+    if len(t) > 1:
+        root.insert(2, [newBranch, [], t])
+    else:
+        root.insert(2, [newBranch, [], []])
+    return root
+
+#树的访问函数
+def getRootVal(root):
+    return root[0]
+
+def setRootVal(root, newval):
+    root[0] = newval
+
+def getLeftChild(root):
+    return root[1]
+
+def getRightChild(root):
+    return root[2]
+
+
+# 二叉树的访问函数
+def getLeftChild(self):
+    return self.leftChild
+
+def getRightChild(self):
+    return self.rightChild
+
+def setRootValue(self, obj):
+    self.key = obj
+
+def getRootValue(self):
+    return self.key
+
+
+# 定义节点
+class BinaryTree:
+    def __init__(self, rootObj):
+        self.key = rootObj
+        self.leftChild = None    # 左子节点也是树
+        self.rightChild = None    # 右子节点也是树
+
+# 插入左子节点
+def insertLeft(self, newNode):
+    if self.leftChild == None:
+        self.leftChild = newNode
+    else:
+        t = BinaryTree(newNode)
+        t.leftChild = self.leftChild
+        self.leftChild = t
+
+# 插入右子节点
+def insertRight(self, newNode):
+    if self.rightChild == None:
+        self.rightChild = newNode
+    else:
+        t = BinaryTree(newNode)
+        t.rightChild = self.rightChild
+        self.rightChild = t
+
+# 树的遍历
+
+# 前序
+def preorder(tree):
+    if tree:
+        print(tree.getRootVal())
+        preorder(tree.getLeftChild())
+        preorder(tree.getRightChild())
+
+# 中序
+def inorder(tree):
+    if tree:
+        inorder(tree.getLeftChild())
+        print(tree.getRootVal())
+        inorder(tree.getRightChild())
+
+# 后序
+def postorder(tree):
+    if tree:
+        postorder(tree.getLeftChild())
+        postorder(tree.getRightChild())
+        print(tree.getRootVal())
+
+# 树
+# class Bnode(object):
+#     def __init__(self, data, left=None, right=None):
+#         self.data = data
+#         self.left = left
+#         self.right = right
+#
+# class Btree(object):
+#     def __init__(self, root=None):
+#         self.root = root
+#
+#     @classmethod
+#     def bulid(cls, list):
+#         node_dict = {}
+#         # 制作节点
+#         for item in list:
+#             data = item['data']
+#             node_dict[data] = Bnode(data)
+#         # 把节点组装成数
+#         for item in list:
+#             data = item['data']
+#             node = node_dict[data]
+#             if node['is_root']:
+#                 root = node
+#             node.left = node_dict.get(item['left'])
+#             node.right = node_dict.get(item['right'])
+#         return cls(root)
+#
+#     def preorder(self, subtree):
+#         # 先序
+#         if subtree is not None:
+#             print(subtree.data)
+#             self.preorder(subtree.left)
+#             self.preorder(subtree.right)
+#
+#     def reverse(self, subtree):
+#         # 后序
+#         if subtree is not None:
+#             subtree.left, subtree.right = subtree.right, subtree.left
+#             self.reverse(self, subtree.left)
+#             self.reverse(self, subtree.right)
+
+# 回溯通用模板
+# res = []
+#
+# def backtrack(路径, 选择列表):
+#     if 满足结束条件:
+#         res.append(路径)
+#         return
+#
+#     if 满足剪枝条件: return
+#
+#     for 选择 in 选择列表:
+#         做选择
+#         backtrack(路径, 选择列表)
+#         撤销选择

+ 6 - 29
tree/whole_arrang.py

@@ -1,30 +1,7 @@
-from typing import List
-from utils import Bnode, Btree
+def addBinary(a: str, b: str) -> str:
+    a_num = int(a, 2)
+    b_num = int(b, 2)
+    result = a_num + b_num
+    return (bin(result)[2:])
 
-def permute(nums: List[int]) -> List[List[int]]:
-    node_list = []
-    for i in range(len(List)):
-        node = Bnode(List[i], None, None)
-        node_list.append(node)
-
-    if len(node_list) > 0:
-        for i in range(int(len(List)/2) - 1):
-
-            def array_to_bitree(array):
-                # 判断arr是否为空
-                if len(array) == 0:
-                    return BiTNode(array[0])
-                mid = len(array) // 2  # 有序数组的中间元素的下标
-                # print(mid)
-                # start=0 # 数组第一个元素的下标
-                # end=-1 # 数组最后一个元素的下标
-                if len(array) > 0:
-                    # 将中间元素作为二叉树的根
-                    root = BiTNode(array[mid])
-                    # 如果左边的元素个数不为零,则递归调用函数,生成左子树
-                    if len(array[:mid]) > 0:
-                        root.left_child = arrayToBiTree(array[:mid])
-                    # 如果右边的元素个数不为零,则递归调用函数,生成左子树
-                    if len(array[mid + 1:]) > 0:
-                        root.right_child = arrayToBiTree(array[mid + 1:])
-                return root
+print(addBinary('1010', '1011'))

+ 0 - 106
utils.py

@@ -35,110 +35,4 @@ def list_to_linklist(arr):
         p = p.next
     return head
 
-# 树
-# class Bnode(object):
-#     def __init__(self, data, left=None, right=None):
-#         self.data = data
-#         self.left = left
-#         self.right = right
-#
-# class Btree(object):
-#     def __init__(self, root=None):
-#         self.root = root
-#
-#     @classmethod
-#     def bulid(cls, list):
-#         node_dict = {}
-#         # 制作节点
-#         for item in list:
-#             data = item['data']
-#             node_dict[data] = Bnode(data)
-#         # 把节点组装成数
-#         for item in list:
-#             data = item['data']
-#             node = node_dict[data]
-#             if node['is_root']:
-#                 root = node
-#             node.left = node_dict.get(item['left'])
-#             node.right = node_dict.get(item['right'])
-#         return cls(root)
-#
-#     def preorder(self, subtree):
-#         # 先序
-#         if subtree is not None:
-#             print(subtree.data)
-#             self.preorder(subtree.left)
-#             self.preorder(subtree.right)
-#
-#     def reverse(self, subtree):
-#         # 后序
-#         if subtree is not None:
-#             subtree.left, subtree.right = subtree.right, subtree.left
-#             self.reverse(self, subtree.left)
-#             self.reverse(self, subtree.right)
 
-#定义二叉树结点类型
-class BiTNode:
-  def __init__(self,root):
-    self.root = root
-    self.left_child = None
-    self.right_child = None
-
-#前序遍历
-def print_tree_pre_order(root):
-    # 先判断二叉树是否为空
-    if root is None:
-        return root
-    # 先根
-    print(root.data)
-    # 再左
-    if root.left_child is not None:
-        print_tree_pre_order(root.left_child)
-    # 再右
-    if root.right_child is not None:
-        print_tree_pre_order(root.right_child)
-
-#中序遍历二叉树
-def print_tree_mid_order(root):
-    # 先判断二叉树是否为空,当左右节点都为空时
-    if root is None:
-        return
-    # 中序遍历 左根右
-    # 遍历左子树
-    if root.left_child is not None:
-        print_tree_mid_order(root.left_child)
-    # 遍历根节点
-    print(root.data)
-    # 遍历右子树
-    if root.right_child is not None:
-        print_tree_mid_order(root.right_child)
-
-#后序遍历
-def print_tree_after_order(root):
-    # 先判断二叉树是否为空
-    if root is None:
-        return root
-    # 再左
-    if root.left_child is not None:
-        print_tree_after_order(root.left_child)
-    # 再右
-    if root.right_child is not None:
-        print_tree_after_order(root.right_child)
-    # 先根
-    print(root.data)
-
-def array_to_bitree(array):
-    # 判断arr是否为空
-    if len(array) == 0:
-        return BiTNode(array[0])
-    mid = len(array) // 2  # 有序数组的中间元素的下标
-    if len(array) > 0:
-        # 将中间元素作为二叉树的根
-        root = BiTNode(array[mid])
-        # 如果左边的元素个数不为零,则递归调用函数,生成左子树
-        if len(array[:mid]) > 0:
-            root.left_child = array_to_bitree(array[:mid])
-        # 如果右边的元素个数不为零,则递归调用函数,生成左子树
-        if len(array[mid + 1:]) > 0:
-            root.right_child = array_to_bitree(array[mid + 1:])
-    return root