Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
CSCI30_Project
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Temujin
CSCI30_Project
Commits
b6b4a537
Commit
b6b4a537
authored
Dec 06, 2019
by
Temujin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed comments
parent
62e7d090
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
40 deletions
+23
-40
family_tree.py
family_tree.py
+23
-40
No files found.
family_tree.py
View file @
b6b4a537
"""Family Tree project for CSCI 30."""
"""Family Tree project for CSCI 30."""
"""
Problems to consider --
Handling value errors.
Questions:
Total nodes of the tree?
11 generations = 2^11 - 1
Tree setup procedure:
1 get all the nodes and edges, check if they're valid.
2 Make sure there's only one root.
3 Find and set the root
4 Set the generations
5 run precompute method
6 done
"""
class
FamilyTree
:
class
FamilyTree
:
...
@@ -32,6 +14,16 @@ class FamilyTree:
...
@@ -32,6 +14,16 @@ class FamilyTree:
value = a Person object, acting as the node
value = a Person object, acting as the node
"""
"""
"""
Tree setup procedure:
1 get all the nodes and edges, check if they're valid.
2 Make sure there's only one root.
3 Find and set the root
4 Set the generations
5 run precompute method
6 done
"""
ordinal
=
[
"first"
,
"second"
,
"third"
,
"fourth"
,
"fifth"
,
ordinal
=
[
"first"
,
"second"
,
"third"
,
"fourth"
,
"fifth"
,
"sixth"
,
"seventh"
,
"eighth"
,
"ninth"
,
"tenth"
]
"sixth"
,
"seventh"
,
"eighth"
,
"ninth"
,
"tenth"
]
times
=
[
"once"
,
"twice"
,
"thrice"
,
"four times"
,
"five times"
,
times
=
[
"once"
,
"twice"
,
"thrice"
,
"four times"
,
"five times"
,
...
@@ -148,10 +140,7 @@ class FamilyTree:
...
@@ -148,10 +140,7 @@ class FamilyTree:
self
.
dfs
(
rc
.
get_name
(),
*
funcs
)
self
.
dfs
(
rc
.
get_name
(),
*
funcs
)
def
find_lca
(
self
,
name1
,
name2
):
def
find_lca
(
self
,
name1
,
name2
):
"""Find the lowest common ancestor of two nodes.
"""Find the lowest common ancestor of two nodes."""
TODO: when one is the ancestor of the other
"""
return
self
.
slow_lca
(
name1
,
name2
)
return
self
.
slow_lca
(
name1
,
name2
)
def
slow_lca
(
self
,
name1
,
name2
):
def
slow_lca
(
self
,
name1
,
name2
):
...
@@ -171,11 +160,10 @@ class FamilyTree:
...
@@ -171,11 +160,10 @@ class FamilyTree:
while
p2
.
get_generation
()
<
p1
.
get_generation
():
while
p2
.
get_generation
()
<
p1
.
get_generation
():
p1
=
p1
.
get_parent
()
p1
=
p1
.
get_parent
()
#
Keep moving
up at the same time until they're equal
#
Move
up at the same time until they're equal
while
p1
is
not
p2
:
while
p1
is
not
p2
:
p1
=
p1
.
get_parent
()
p1
=
p1
.
get_parent
()
p2
=
p2
.
get_parent
()
p2
=
p2
.
get_parent
()
return
p1
return
p1
def
fast_lca
(
self
,
a
,
b
):
def
fast_lca
(
self
,
a
,
b
):
...
@@ -185,13 +173,6 @@ class FamilyTree:
...
@@ -185,13 +173,6 @@ class FamilyTree:
"""
"""
return
return
def
get_degree
(
self
,
p1
,
p2
,
lca
):
"""Find the degree between two people."""
distance_p1
=
lca
.
get_generation
()
-
p1
.
get_generation
()
distance_p2
=
lca
.
get_generation
()
-
p2
.
get_generation
()
deg
=
min
[
distance_p1
-
1
,
distance_p2
-
1
]
return
deg
def
relationship
(
self
,
p1
,
p2
):
def
relationship
(
self
,
p1
,
p2
):
"""Determine the relationship between two distinct people in the tree.
"""Determine the relationship between two distinct people in the tree.
...
@@ -223,16 +204,11 @@ class FamilyTree:
...
@@ -223,16 +204,11 @@ class FamilyTree:
else
:
else
:
return
self
.
GREAT
(
furthest
)
+
"grand"
+
self
.
TERM
(
"nibling"
,
d1
,
d2
,
p2
.
sex
)
return
self
.
GREAT
(
furthest
)
+
"grand"
+
self
.
TERM
(
"nibling"
,
d1
,
d2
,
p2
.
sex
)
elif
nearest
==
0
:
elif
nearest
==
0
:
## TODO: PARENT RELATIONSHIP
if
furthest
==
1
:
if
furthest
==
1
:
return
self
.
TERM
(
"child"
,
d1
,
d2
,
p2
.
sex
)
return
self
.
TERM
(
"child"
,
d1
,
d2
,
p2
.
sex
)
else
:
else
:
return
self
.
GREAT
(
furthest
)
+
"grand"
+
self
.
TERM
(
"child"
,
d1
,
d2
,
p2
.
sex
)
return
self
.
GREAT
(
furthest
)
+
"grand"
+
self
.
TERM
(
"child"
,
d1
,
d2
,
p2
.
sex
)
pass
def
ORDINAL
(
self
,
n
):
def
ORDINAL
(
self
,
n
):
"""Return the index in the ordinal table."""
"""Return the index in the ordinal table."""
return
self
.
ordinal
[
n
]
return
self
.
ordinal
[
n
]
...
@@ -242,7 +218,7 @@ class FamilyTree:
...
@@ -242,7 +218,7 @@ class FamilyTree:
return
self
.
times
[
n
]
return
self
.
times
[
n
]
def
great
(
self
,
n
):
def
great
(
self
,
n
):
"""Return a string of n 'greats' separated by
-
."""
"""Return a string of n 'greats' separated by
'-'
."""
return
"-"
.
join
([
"great"
]
*
n
)
return
"-"
.
join
([
"great"
]
*
n
)
def
TERM
(
self
,
kind
,
d1
,
d2
,
sex
):
def
TERM
(
self
,
kind
,
d1
,
d2
,
sex
):
...
@@ -370,7 +346,7 @@ def main():
...
@@ -370,7 +346,7 @@ def main():
ftree
=
FamilyTree
()
ftree
=
FamilyTree
()
A
=
Person
(
"A"
,
69
,
True
)
A
=
Person
(
"A"
,
69
,
True
)
B
=
Person
(
"B"
,
69
,
True
)
B
=
Person
(
"B"
,
69
,
True
)
C
=
Person
(
"C"
,
69
,
Tru
e
)
C
=
Person
(
"C"
,
69
,
Fals
e
)
D
=
Person
(
"D"
,
69
,
True
)
D
=
Person
(
"D"
,
69
,
True
)
E
=
Person
(
"E"
,
666
,
False
)
E
=
Person
(
"E"
,
666
,
False
)
ftree
.
insert_person
(
A
)
ftree
.
insert_person
(
A
)
...
@@ -395,8 +371,15 @@ def main():
...
@@ -395,8 +371,15 @@ def main():
print
(
"The lca of {} and {} is {}"
.
format
(
B
,
D
,
ftree
.
find_lca
(
"B"
,
"D"
)))
print
(
"The lca of {} and {} is {}"
.
format
(
B
,
D
,
ftree
.
find_lca
(
"B"
,
"D"
)))
print
(
"The lca of {} and {} is {}"
.
format
(
A
,
B
,
ftree
.
find_lca
(
"A"
,
"B"
)))
print
(
"The lca of {} and {} is {}"
.
format
(
A
,
B
,
ftree
.
find_lca
(
"A"
,
"B"
)))
print
(
"relationship (A, B): {}"
.
format
(
ftree
.
relationship
(
A
,
B
)))
# parent/child
print
(
"relationship (B, C): {}"
.
format
(
ftree
.
relationship
(
B
,
C
)))
print
(
"relationship {} is {}'s: {}"
.
format
(
B
,
A
,
ftree
.
relationship
(
A
,
B
)))
print
(
"relationship {} is {}'s: {}"
.
format
(
A
,
B
,
ftree
.
relationship
(
B
,
A
)))
# sibling
print
(
"relationship {} is {}'s: {}"
.
format
(
B
,
C
,
ftree
.
relationship
(
C
,
B
)))
print
(
"relationship {} is {}'s: {}"
.
format
(
C
,
B
,
ftree
.
relationship
(
B
,
C
)))
# nibling
print
(
"relationship {} is {}'s: {}"
.
format
(
D
,
C
,
ftree
.
relationship
(
C
,
D
)))
print
(
"relationship {} is {}'s: {}"
.
format
(
C
,
D
,
ftree
.
relationship
(
D
,
C
)))
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment