FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-algorithms/algorithms/tree/bst/height.py at master · plumnx/python-algorithms · GitHub
plumnx
/
python-algorithms
Public
forked from
keon/algorithms
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-algorithms
/
algorithms
/
tree
/
bst
/
height.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
60 lines (49 loc) · 1.41 KB
Breadcrumbs
python-algorithms
/
algorithms
/
tree
/
bst
/
height.py
Copy path
File metadata and controls
60 lines (49 loc) · 1.41 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
"""
Write a function height returns the height of a tree. The height is defined to
be the number of levels. The empty tree has height 0, a tree of one node has
height 1, a root node with one or two leaves as children has height 2, and so on
For example: height of tree is 4
9
/
\
6 12
/ \ /
\
3 8 10 15
/
\
7 18
height = 4
"""
import
unittest
from
bst
import
Node
from
bst
import
bst
def
height
(
root
):
if
root
is
None
:
return
0
else
:
return
1
+
max
(
height
(
root
.
left
),
height
(
root
.
right
))
"""
The tree is created for testing:
9
/
\
6 12
/ \ /
\
3 8 10 15
/
\
7 18
count_left_node = 4
"""
class
TestSuite
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
tree
=
bst
()
self
.
tree
.
insert
(
9
)
self
.
tree
.
insert
(
6
)
self
.
tree
.
insert
(
12
)
self
.
tree
.
insert
(
3
)
self
.
tree
.
insert
(
8
)
self
.
tree
.
insert
(
10
)
self
.
tree
.
insert
(
15
)
self
.
tree
.
insert
(
7
)
self
.
tree
.
insert
(
18
)
def
test_height
(
self
):
self
.
assertEqual
(
4
,
height
(
self
.
tree
.
root
))
if
__name__
==
'__main__'
:
unittest
.
main
()
Back
|
FazBrowse Home
|
New Git URL