# Circular Doubly Linked List: Iterativeby Isai Damier, Android Engineer @ Google

```  #=====================================================================
# Author: Isai Damier
# Title: BST to Circular Doubly Linked List (Iterative)
# Project: geekviewpoint
# Package: algorithms
#
# Time Complexity of Solution:
#   Best = Average = Worst = O(n).
#
# Description: Convert a BST into a circular doubly LinkedList.
#
# Technical Details: This algorithm illustrates the importance of knowing
#   how to traverse a BST iteratively. The code is elegant and simple.
#
#=====================================================================

class BST( object ):

def __init__( self ):
self.root = None

def getRoot( self ):
return self.root

stack = LifoQueue()
n = self.root

# inorder loop
while n is not None or not stack.empty():
# traverse left
if n is not None:
stack.put( n )
n = n.left
else: # visit
n = stack.get()
else:
tail.right = n # link right
n.left = tail # link left
tail = tail.right # reassign tail
# traverse right
n = n.right

# make circular
```
```import unittest
from algorithms.BST import BST
from cStringIO import StringIO
import sys
class Test( unittest.TestCase ):
#======================================================================
# tapes:
# Output: void
#
#    function. The trick here is to avoid falling into an
#    endless loop as the tree is now a circular structure
#    with no definite end points.
#
# Technical Details: From a testing viewpoint
#     equivalent.
#======================================================================
bst = BST()

treeTape = [200, 100, 300, 50, 150, 250, 350, 25, 75, 125,
175, 225, 275, 325, 375, 35, 212, 312, 400]
# set expectation
ino = [35, 25, 75, 50, 125, 175, 150, 100, 212, 225, 275, 250,
312, 325, 400, 375, 350, 300, 200]
ino.sort() # mergesort: like inorder

for i in treeTape:

self.assertEquals( len( treeTape ), bst.size() ) # has correct size