mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-22 16:31:15 -04:00
Update and rename bst_inorder_iterator.py to bt_inorder_iterator.py
This commit is contained in:
parent
d50ed323fd
commit
ef073d11ca
1 changed files with 2 additions and 7 deletions
31
trees/bt_inorder_iterator.py
Normal file
31
trees/bt_inorder_iterator.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# author: bt3gl
|
||||
|
||||
class Node:
|
||||
def __init__(self, left=None, right=None):
|
||||
self.val = val
|
||||
self.left = left
|
||||
self.right = right
|
||||
|
||||
|
||||
class BST_Iterator:
|
||||
|
||||
def __init__(self, root):
|
||||
self.stack = []
|
||||
self.left_inorder(root)
|
||||
|
||||
def left_inorder(self, root):
|
||||
while root:
|
||||
self.stack.append(root)
|
||||
root = root.left
|
||||
|
||||
def next(self) -> int:
|
||||
top_node = self.stack.pop()
|
||||
if top_node.right:
|
||||
self.left_inorder(top_node.right)
|
||||
return top_node.val
|
||||
|
||||
def has_next(self) -> bool:
|
||||
return len(self.stack) > 0
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue