mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 17:34:49 +08:00
Added Whitespace and Docstring (#924)
* Added Whitespace and Docstring I modified the file to make Pylint happier and make the code more readable. * Beautified Code and Added Docstring I modified the file to make Pylint happier and make the code more readable. * Added DOCSTRINGS, Wikipedia link, and whitespace I added DOCSTRINGS and whitespace to make the code more readable and understandable. * Improved Formatting * Wrapped comments * Fixed spelling error for `movement` variable * Added DOCSTRINGs * Improved Formatting * Corrected whitespace to improve readability. * Added docstrings. * Made comments fit inside an 80 column layout.
This commit is contained in:
@ -1,14 +1,18 @@
|
||||
# Tree_sort algorithm
|
||||
# Build a BST and in order traverse.
|
||||
"""
|
||||
Tree_sort algorithm.
|
||||
|
||||
Build a BST and in order traverse.
|
||||
"""
|
||||
|
||||
|
||||
class node():
|
||||
# BST data structure
|
||||
def __init__(self, val):
|
||||
self.val = val
|
||||
self.left = None
|
||||
self.right = None
|
||||
|
||||
def insert(self,val):
|
||||
self.left = None
|
||||
self.right = None
|
||||
|
||||
def insert(self, val):
|
||||
if self.val:
|
||||
if val < self.val:
|
||||
if self.left is None:
|
||||
@ -23,24 +27,27 @@ class node():
|
||||
else:
|
||||
self.val = val
|
||||
|
||||
|
||||
def inorder(root, res):
|
||||
# Recursive travesal
|
||||
# Recursive travesal
|
||||
if root:
|
||||
inorder(root.left,res)
|
||||
inorder(root.left, res)
|
||||
res.append(root.val)
|
||||
inorder(root.right,res)
|
||||
inorder(root.right, res)
|
||||
|
||||
|
||||
def tree_sort(arr):
|
||||
# Build BST
|
||||
if len(arr) == 0:
|
||||
return arr
|
||||
root = node(arr[0])
|
||||
for i in range(1,len(arr)):
|
||||
for i in range(1, len(arr)):
|
||||
root.insert(arr[i])
|
||||
# Traverse BST in order.
|
||||
# Traverse BST in order.
|
||||
res = []
|
||||
inorder(root,res)
|
||||
inorder(root, res)
|
||||
return res
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(tree_sort([10,1,3,2,9,14,13]))
|
||||
print(tree_sort([10, 1, 3, 2, 9, 14, 13]))
|
||||
|
Reference in New Issue
Block a user