mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-22 15:53:50 +08:00
Rearrange the chapters.
Start to translate codes from Java to Python.
This commit is contained in:
9
codes/python/include/__init__.py
Normal file
9
codes/python/include/__init__.py
Normal file
@ -0,0 +1,9 @@
|
||||
import copy
|
||||
import math
|
||||
import random
|
||||
import functools
|
||||
import collections
|
||||
from typing import List
|
||||
from .linked_list import ListNode, list_to_linked_list, linked_list_to_list, get_list_node
|
||||
from .binary_tree import TreeNode, list_to_tree, tree_to_list, get_tree_node
|
||||
from .print_util import print_matrix, print_linked_list, print_tree
|
BIN
codes/python/include/__pycache__/__init__.cpython-38.pyc
Normal file
BIN
codes/python/include/__pycache__/__init__.cpython-38.pyc
Normal file
Binary file not shown.
BIN
codes/python/include/__pycache__/binary_tree.cpython-38.pyc
Normal file
BIN
codes/python/include/__pycache__/binary_tree.cpython-38.pyc
Normal file
Binary file not shown.
BIN
codes/python/include/__pycache__/linked_list.cpython-38.pyc
Normal file
BIN
codes/python/include/__pycache__/linked_list.cpython-38.pyc
Normal file
Binary file not shown.
BIN
codes/python/include/__pycache__/print_util.cpython-38.pyc
Normal file
BIN
codes/python/include/__pycache__/print_util.cpython-38.pyc
Normal file
Binary file not shown.
82
codes/python/include/binary_tree.py
Normal file
82
codes/python/include/binary_tree.py
Normal file
@ -0,0 +1,82 @@
|
||||
'''
|
||||
File: binary_tree.py
|
||||
Created Time: 2021-12-11
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import collections
|
||||
|
||||
class TreeNode:
|
||||
"""Definition for a binary tree node
|
||||
"""
|
||||
def __init__(self, val=0, left=None, right=None):
|
||||
self.val = val
|
||||
self.left = left
|
||||
self.right = right
|
||||
|
||||
def list_to_tree(arr):
|
||||
"""Generate a binary tree with a list
|
||||
|
||||
Args:
|
||||
arr ([type]): [description]
|
||||
|
||||
Returns:
|
||||
[type]: [description]
|
||||
"""
|
||||
if not arr:
|
||||
return
|
||||
i = 1
|
||||
root = TreeNode(int(arr[0]))
|
||||
queue = collections.deque()
|
||||
queue.append(root)
|
||||
while queue:
|
||||
node = queue.popleft()
|
||||
if arr[i] != None:
|
||||
node.left = TreeNode(int(arr[i]))
|
||||
queue.append(node.left)
|
||||
i += 1
|
||||
if arr[i] != None:
|
||||
node.right = TreeNode(int(arr[i]))
|
||||
queue.append(node.right)
|
||||
i += 1
|
||||
return root
|
||||
|
||||
def tree_to_list(root):
|
||||
"""Serialize a tree into an array
|
||||
|
||||
Args:
|
||||
root ([type]): [description]
|
||||
|
||||
Returns:
|
||||
[type]: [description]
|
||||
"""
|
||||
if not root: return []
|
||||
queue = collections.deque()
|
||||
queue.append(root)
|
||||
res = []
|
||||
while queue:
|
||||
node = queue.popleft()
|
||||
if node:
|
||||
res.append(node.val)
|
||||
queue.append(node.left)
|
||||
queue.append(node.right)
|
||||
else: res.append(None)
|
||||
return res
|
||||
|
||||
def get_tree_node(root, val):
|
||||
"""Get a tree node with specific value in a binary tree
|
||||
|
||||
Args:
|
||||
root ([type]): [description]
|
||||
val ([type]): [description]
|
||||
|
||||
Returns:
|
||||
[type]: [description]
|
||||
"""
|
||||
if not root:
|
||||
return
|
||||
if root.val == val:
|
||||
return root
|
||||
left = get_tree_node(root.left, val)
|
||||
right = get_tree_node(root.right, val)
|
||||
return left if left else right
|
57
codes/python/include/linked_list.py
Normal file
57
codes/python/include/linked_list.py
Normal file
@ -0,0 +1,57 @@
|
||||
'''
|
||||
File: linked_list.py
|
||||
Created Time: 2021-12-11
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
class ListNode:
|
||||
"""Definition for a singly-linked list node
|
||||
"""
|
||||
def __init__(self, val=0, next=None):
|
||||
self.val = val
|
||||
self.next = next
|
||||
|
||||
def list_to_linked_list(arr):
|
||||
"""Generate a linked list with a list
|
||||
|
||||
Args:
|
||||
arr ([type]): [description]
|
||||
|
||||
Returns:
|
||||
[type]: [description]
|
||||
"""
|
||||
dum = head = ListNode(0)
|
||||
for a in arr:
|
||||
node = ListNode(a)
|
||||
head.next = node
|
||||
head = head.next
|
||||
return dum.next
|
||||
|
||||
def linked_list_to_list(head):
|
||||
"""Serialize a linked list into an array
|
||||
|
||||
Args:
|
||||
head ([type]): [description]
|
||||
|
||||
Returns:
|
||||
[type]: [description]
|
||||
"""
|
||||
arr = []
|
||||
while head:
|
||||
arr.append(head.val)
|
||||
head = head.next
|
||||
return arr
|
||||
|
||||
def get_list_node(head, val):
|
||||
"""Get a list node with specific value from a linked list
|
||||
|
||||
Args:
|
||||
head ([type]): [description]
|
||||
val ([type]): [description]
|
||||
|
||||
Returns:
|
||||
[type]: [description]
|
||||
"""
|
||||
while head and head.val != val:
|
||||
head = head.next
|
||||
return head
|
73
codes/python/include/print_util.py
Normal file
73
codes/python/include/print_util.py
Normal file
@ -0,0 +1,73 @@
|
||||
'''
|
||||
File: print_util.py
|
||||
Created Time: 2021-12-11
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
from .binary_tree import TreeNode, tree_to_list
|
||||
from .linked_list import ListNode, linked_list_to_list
|
||||
|
||||
def print_matrix(mat):
|
||||
"""Print a matrix
|
||||
|
||||
Args:
|
||||
mat ([type]): [description]
|
||||
"""
|
||||
pstr = []
|
||||
for arr in mat:
|
||||
pstr.append(' ' + str(arr))
|
||||
|
||||
print('[\n' + ',\n'.join(pstr) + '\n]')
|
||||
|
||||
def print_linked_list(head):
|
||||
"""Print a linked list
|
||||
|
||||
Args:
|
||||
head ([type]): [description]
|
||||
"""
|
||||
arr = linked_list_to_list(head)
|
||||
print(' -> '.join([str(a) for a in arr]))
|
||||
|
||||
|
||||
class Trunk:
|
||||
def __init__(self, prev=None, str=None):
|
||||
self.prev = prev
|
||||
self.str = str
|
||||
|
||||
def showTrunks(p):
|
||||
if p is None:
|
||||
return
|
||||
showTrunks(p.prev)
|
||||
print(p.str, end='')
|
||||
|
||||
def print_tree(root, prev=None, isLeft=False):
|
||||
"""Print a binary tree
|
||||
This tree printer is borrowed from TECHIE DELIGHT
|
||||
https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
Args:
|
||||
root ([type]): [description]
|
||||
prev ([type], optional): [description]. Defaults to None.
|
||||
isLeft (bool, optional): [description]. Defaults to False.
|
||||
"""
|
||||
if root is None:
|
||||
return
|
||||
|
||||
prev_str = ' '
|
||||
trunk = Trunk(prev, prev_str)
|
||||
print_tree(root.right, trunk, True)
|
||||
|
||||
if prev is None:
|
||||
trunk.str = '———'
|
||||
elif isLeft:
|
||||
trunk.str = '/———'
|
||||
prev_str = ' |'
|
||||
else:
|
||||
trunk.str = '\———'
|
||||
prev.str = prev_str
|
||||
|
||||
showTrunks(trunk)
|
||||
print(' ' + str(root.val))
|
||||
if prev:
|
||||
prev.str = prev_str
|
||||
trunk.str = ' |'
|
||||
print_tree(root.left, trunk, False)
|
Reference in New Issue
Block a user