def _create_min_bst(self, array, start, end):
     if end < start:
         return None
     mid = (start + end) // 2
     node = Node(array[mid])
     node.left = self._create_min_bst(array, start, mid - 1)
     node.right = self._create_min_bst(array, mid + 1, end)
     return node
from Balanced_Trees.Binary_Search_Trees.Node import Node
from Stacks.Stack_Implementation import Stack
root = Node(1)
root.leftChild = Node(2)
root.rightChild = Node(3)
root.leftChild.leftChild = Node(4)
root.leftChild.rightChild = Node(5)
root.leftChild.leftChild.leftChild = Node(8)
root.leftChild.rightChild.leftChild = Node(9)
root.rightChild.leftChild = Node(6)
root.rightChild.rightChild = Node(7)
root.rightChild.rightChild.rightChild = Node(10)



def zizag(root):
    if root is None:
        return

    my_stack1 = Stack()
    my_stack1.push(root)
    my_stack2 = Stack()

    while not my_stack1.isEmpty() or not my_stack2.isEmpty():
        while not my_stack1.isEmpty():
            root = my_stack1.pop()
            print root.data
            if root.leftChild is not None:
                my_stack2.push(root.leftChild)
            if root.rightChild is not None:
from Balanced_Trees.Binary_Search_Trees.Node import Node
from Stacks.Stack_Implementation import Stack
from Queue import Queue

root = Node(10)
root.leftChild = Node(20)
root.rightChild = Node(30)
root.leftChild.leftChild = Node(40)
root.leftChild.rightChild = Node(50)
root.rightChild.leftChild = Node(60)
root.rightChild.rightChild = Node(70)


def print_reverse_level_order(root):
    if root is None:
        return None

    my_stack = Stack()
    my_queue1 = Queue()
    my_queue2 = Queue()
    my_queue1.put(root)

    while not my_queue1.empty() or not my_queue2.empty():
        while not my_queue1.empty():
            root = my_queue1.get()

            if root.rightChild:
                my_queue2.put(root.rightChild)

            if root.leftChild:
                my_queue2.put(root.leftChild)
Ejemplo n.º 4
0
from Balanced_Trees.Binary_Search_Trees.Node import Node

root = Node(10)
root.leftChild = Node(8)
root.rightChild = Node(11)
root.leftChild.leftChild = Node(7)
root.leftChild.rightChild = Node(9)
root.rightChild.rightChild = Node(12)

my_list = []


def in_order(root):
    helper(root, my_list)
    return my_list


def helper(root, my_list):
    if root is None:
        return
    if root:
        in_order(root.leftChild)
        my_list.append(root.data)
        in_order(root.rightChild)


original_val = root.data
k = 2
print in_order(root)[len(my_list) - k]