Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lecture "Organising information: trees", exercise 2 #32

Open
essepuntato opened this issue Dec 12, 2022 · 2 comments
Open

Lecture "Organising information: trees", exercise 2 #32

essepuntato opened this issue Dec 12, 2022 · 2 comments
Labels

Comments

@essepuntato
Copy link
Contributor

essepuntato commented Dec 12, 2022

Write the pure iterative version of the function defined in the previous exercise in Python.

@ranacoskun
Copy link

def test_breadth_first_visit(root_node, expected):
    if expected == breadth_first_visit(root_node):
        return True
    else:
        return False
        
def breadth_first_visit(root_node):
    output_list = []
    queue = []
    queue.append(root_node)
    
    while queue != []:
        output_list.append(queue[0])
        queue.extend(queue[0].children)
        queue.pop(0)
    return output_list

elizabeth = Node("elizabeth")
charles = Node("charles", elizabeth)
anne = Node("anne", elizabeth)
william = Node("william", charles)
harry = Node("harry", charles)
george = Node("george", william)
charlotte = Node("charlotte", william)
louis = Node("louis", william)
archie = Node("archie", harry)

royals = [elizabeth,charles,anne,william,harry,george,charlotte,louis,archie]
print(test_breadth_first_visit(elizabeth, royals))

@n1kg0r
Copy link

n1kg0r commented Dec 16, 2022


from anytree import Node
from collections import deque


def test_breadth_first_visit(root_node, expected):
    return expected == breadth_first_visit(root_node)


def breadth_first_visit(root_node):
    result = list()
    
    if root_node is None:
        return 
    q = deque()
    q.append(root_node)

    while(len(q)):
        node = q.popleft()
        result.append(node)

        for child_node in node.children:
            q.append(child_node)

    return result


# Tests
book = Node("book")
chapter_1 = Node("chapter1", book)
chapter_2 = Node("chapter2", book)
paragraph_1 = Node("paragraph1", chapter_1)
text_1 = Node("text1", paragraph_1)
quotation_1 = Node("quotation1", paragraph_1)
text_2 = Node("text2", quotation_1)
text_3 = Node("text3", paragraph_1)
quotation_2 = Node("quotation2", paragraph_1)
text_4 = Node("text4", quotation_2)
paragraph_2 = Node("paragraph2", chapter_1)
text_5 = Node("text5", paragraph_2)
paragraph_3 = Node("paragraph3", chapter_1)
text_6 = Node("text6", paragraph_3)
text_7 = Node("text7", chapter_2)
text_8 = Node("text8", book)
bfv = [book,
       chapter_1, chapter_2, text_8,
       paragraph_1, paragraph_2, paragraph_3, text_7,
       text_1, quotation_1, text_3, quotation_2, text_5, text_6,
       text_2, text_4]

bfv_from_chapter_1 = [chapter_1, paragraph_1, paragraph_2, paragraph_3, text_1, quotation_1, text_3, quotation_2, text_5, text_6, text_2, text_4]


print(test_breadth_first_visit(book, bfv))
print(test_breadth_first_visit(chapter_1, bfv_from_chapter_1))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants