Ejemplo n.º 1
0
class BST(object):
    def __init__(self):
        self.rootNode = None

    def insert(self, data):
        if not self.rootNode:  #rootnode is None.hence constructor o BST.Node will be called
            self.rootNode = Node(data)
        else:  #rootnode is not None
            self.rootNode.insert(data)

    def remove(self, data):
        if self.rootNode:
            if self.rootNode.data == data:
                tempNode = Node(None)
                tempNode.leftchild = self.rootNode
                self.rootNode.remove(data, tempNode)
            else:
                self.rootNode.remove(data, None)

    def getMax(self):
        if self.rootNode:
            return self.rootNode.getMax()

    def getMin(self):
        if self.rootNode:
            return self.rootNode.getMin()

    def traversInOrder(self):
        if self.rootNode:
            self.rootNode.traversInOrder()
Ejemplo n.º 2
0
class bst(object):
    def __init__(self):
        self.rootNode = None

    def insert(self, data):

        if not self.rootNode:
            self.rootNode = Node(data)
            print("bst:root node is", data)
        else:
            self.rootNode.insert(data)
            print('bst: node added is', data)

    def inOrderTraverse(self):

        if self.rootNode is not None:
            print('bst:calling inOrder')
            self.rootNode.InOrderTraverse()

    def preOrderTraverse(self):

        if self.rootNode is not None:
            #print('bst:calling preOrder')
            self.rootNode.preOrderTraverse()

    def postOrderTraverse(self):

        if self.rootNode is not None:
            #print('bst:calling postOrder')
            self.rootNode.postOrderTraverse()

    def max_node(self):

        if self.rootNode is not None:
            print("bst: max -->", self.rootNode.max_node())
            #print(r)

    def remove_node(self, data):

        if self.rootNode is not None:

            #print("bst.rootN 1", self.rootNode.data)
            rN = self.rootNode.remove_node(data, None)
            if rN is not None:
                self.rootNode = rN
                print("Root of the Tree is : ", self.rootNode.data)

        else:

            print("Tree is empty")