Beispiel #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()
Beispiel #2
0
 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)
Beispiel #3
0
    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)
Beispiel #4
0
 def _insert(self, value, node):
     if value < node.value:
         if node.left is None:
             node.left = Node(value)
         else:
             self._insert(value, node.left)
     elif value > node.value:
         if node.right is None:
             node.right = Node(value)
         else:
             self._insert(value, node.right)
     else:
         print("The value is already in the tree")
         return None
Beispiel #5
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")
Beispiel #6
0
 def insert(self, value):
     if self.root is None:
         self.root = Node(value)
     else:
         self._insert(value, self.root)
Beispiel #7
0
 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)