Ejemplo n.º 1
0
def dfs_pre_order(tree: Tree):
    visit_list = list()
    stack = Stack()

    # start with root node, visit it and add it to the stack
    node = tree.get_root()
    state = State(node)
    stack.push(state)
    visit_list.append(node.get_value())

    while node:

        if node.has_left_child() and not state.is_visited_left():
            state.set_visited_left()
            node = node.get_left_child()
            state = State(node)
            stack.push(state)
            visit_list.append(node.get_value())
        elif node.has_right_child() and not state.is_visited_right():
            state.set_visited_right()
            node = node.get_right_child()
            state = State(node)
            stack.push(state)
            visit_list.append(node.get_value())
        else:
            stack.pop()
            if not stack.is_empty():
                state = stack.peek()
                node = state.get_node()
            else:
                node = None
    return visit_list
def breadth_first_search(tree: Tree):
    queue = Queue()
    visit_order = list()
    queue.enqueue(tree.get_root())
    while not queue.is_empty():
        node = queue.dequeue()
        visit_order.append(node.get_value())
        if node.has_left_child():
            queue.enqueue(node.get_left_child())
        if node.has_right_child():
            queue.enqueue(node.get_right_child())
    return visit_order
Ejemplo n.º 3
0
def dfs_pre_order_recursion(tree: Tree):
    visit_list = list()
    node = tree.get_root()

    # traverse
    def traverse(node):
        if node:
            # visit the node
            visit_list.append(node.get_value())

            # traverse left
            traverse(node.get_left_child())

            # traverse right
            traverse(node.get_right_child())

    traverse(node)

    return visit_list
Ejemplo n.º 4
0
from data_structures.trees.tree import Tree
from data_structures.trees.tree_node import Node
from data_structures.trees.state import State
from data_structures.stack.stack_linkedlist import Stack

# init tree
tree = Tree("apple")
tree.get_root().set_left_child(Node("banana"))
tree.get_root().set_right_child(Node("cherry"))
tree.get_root().get_left_child().set_left_child(Node("dates"))


def dfs_pre_order(tree: Tree):
    visit_list = list()
    stack = Stack()

    # start with root node, visit it and add it to the stack
    node = tree.get_root()
    state = State(node)
    stack.push(state)
    visit_list.append(node.get_value())

    while node:

        if node.has_left_child() and not state.is_visited_left():
            state.set_visited_left()
            node = node.get_left_child()
            state = State(node)
            stack.push(state)
            visit_list.append(node.get_value())
        elif node.has_right_child() and not state.is_visited_right():
Ejemplo n.º 5
0
from data_structures.trees.tree import Tree
from data_structures.trees.tree_node import Node

# init tree
tree = Tree(1)
tree.insert_with_loop(2)
tree.insert_with_loop(3)
tree.insert_with_loop(5)
tree.insert_with_loop(4)
tree.insert_with_loop(0)
print(tree)

print("\n=====================")
tree = Tree(1)
tree.insert_with_recursion(2)
tree.insert_with_recursion(3)
tree.insert_with_recursion(5)
tree.insert_with_recursion(4)
tree.insert_with_recursion(0)
print(tree)

print("\n=====================")
print(tree.search(0))
print(tree.search(10))

print("\n=delete a node ====================")
tree = Tree(5)
tree.insert_with_recursion(3)
tree.insert_with_recursion(4)
tree.insert_with_recursion(7)
tree.insert_with_recursion(6)