GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Data-Structures/BinaryTreeInPython/binaryTree.py at master · pareksha/Data-Structures

Expand file tree

/

binaryTree.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
115 lines (98 loc) · 4.48 KB
/

binaryTree.py

Copy path

File metadata and controls

115 lines (98 loc) · 4.48 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
class TreeNode(object):
def __init__(self, data=None):
self.data = data
self.right_child = None
self.left_child = None
class BST(object):
def __init__(self, root_value):
self.root = TreeNode(root_value)
def insert(self, root, value):
if root is None:
return TreeNode(value)
elif root.data >= value:
root.left_child = self.insert(root.left_child, value)
return root
else:
root.right_child = self.insert(root.right_child, value)
return root
def print_preorder(self, root):
if root is None:
return
print(root.data, end=' ')
self.print_preorder(root.left_child)
self.print_preorder(root.right_child)
def print_inorder(self, root):
if root is None:
return
self.print_inorder(root.left_child)
print(root.data, end=' ')
self.print_inorder(root.right_child)
def print_postorder(self, root):
if root is None:
return
self.print_postorder(root.left_child)
self.print_postorder(root.right_child)
print(root.data, end=' ')
def find_height(self, root):
if root is None:
return -1
else:
return max(self.find_height(root.left_child), self.find_height(root.right_child)) + 1
def find_minimum(self, root):
if root.left_child is None:
return root.data
else:
return self.find_minimum(root.left_child)
def find_maximum(self, root):
if root.right_child is None:
return root.data
else:
return self.find_maximum(root.right_child)
def find_depth(self, value, root, depth=0):
if root is None:
return 'Node with value ' + str(value) + ' not present in the BST with root ' + str(root)
elif root.data == value:
return depth
elif value < root.data:
return self.find_depth(value, root.left_child, depth=depth + 1)
else:
return self.find_depth(value, root.right_child, depth=depth + 1)
def delete(self, value, root):
# Delete node with no left or right child
if root.right_child.data == value and root.right_child.right_child is None and root.right_child.left_child is None:
root.right_child = None
elif root.left_child.data == value and root.left_child.left_child is None and root.left_child.right_child is None:
root.left_child = None
# Delete node with both left and right child
elif root.right_child.data == value and root.right_child.left_child is not None and root.right_child.right_child is not None:
minimum_value = self.find_minimum(root.right_child.right_child)
root.right_child.data = minimum_value
print('Hey')
self.delete(minimum_value, root.right_child)
elif root.left_child.data == value and root.left_child.left_child is not None and root.left_child.right_child is not None:
minimum_value = self.find_minimum(root.left_child.right_child)
root.left_child.data = minimum_value
self.delete(minimum_value, root.left_child)
# Delete node with either left or right child
elif root.left_child.data == value:
node_to_be_deleted = root.left_child
if root.left_child.left_child is None and root.left_child.right_child is not None:
root.left_child = root.left_child.right_child
root.left_child.right_child = None
elif root.left_child.left_child is not None and root.left_child.right_child is None:
root.left_child = root.left_child.left_child
root.left_child.left_child = None
del node_to_be_deleted
elif root.right_child.data == value:
node_to_be_deleted = root.right_child
if root.right_child.left_child is None and root.right_child.right_child is not None:
root.right_child = root.right_child.right_child
root.right_child.right_child = None
elif root.right_child.left_child is not None and root.right_child.right_child is None:
root.right_child = root.right_child.left_child
root.right_child.left_child = None
del node_to_be_deleted
elif value < root.data:
self.delete(value, root.left_child)
elif value > root.data:
self.delete(value, root.right_child)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.