if r is None: return None q = Queue() s = Stack() q.enqueue(r) while not q.is_empty(): n = q.dequeue() s.push(n) if n.rightChild: q.enqueue(n.rightChild) if n.leftChild: q.enqueue(n.leftChild) while not s.is_empty(): print str(s.pop().value) n = BinaryTree(1) n.insertLeft(2) n.insertRight(3) nl = n.getLeftChild() nl.insertLeft(4) nl.insertRight(5) nr = n.getRightChild() nr.insertLeft(6) nr.insertRight(7) rev_level_order(n)
from GeneralPractice.DataStructurs.Queues.queue import Queue from GeneralPractice.DataStructurs.Trees.BinarySearchTree import Tree, Node from GeneralPractice.DataStructurs.Trees.BinaryTree import BinaryTree bst = Tree() bst.insert(10) bst.insert(5) bst.insert(13) bst.insert(18) bst.insert(15) bst.insert(11) bst.insert(7) bst.insert(3) bst.level_order_trav() bt = BinaryTree("A") bt.insertLeft("B") bt.insertRight("C") l = bt.getLeftChild() l.insertLeft("D") l.insertRight("E") r = bt.getRightChild() r.insertLeft("F") r.insertRight("G") q = Queue()