diff --git a/Data Structures/BST/binary tree to bst.py b/Data Structures/BST/binary tree to bst.py new file mode 100644 index 0000000..6403c6d --- /dev/null +++ b/Data Structures/BST/binary tree to bst.py @@ -0,0 +1,51 @@ +class Node: + def __init__(self, data): + self.data = data + self.left = self.right = None + + +def inorder(root): + if root: + inorder(root.left) + print(root.data,end=" ") + inorder(root.right) + + + +def insert(node, root): + if (root == None): + root = node + else: + if(node.data < root.data): + if root.left is None: + root.left = node + else: + insert(node, root.left) + if (node.data > root.data): + if root.right is None: + root.right = node + else: + insert(node, root.right) + + +def postorder(root,arr): + if root: + postorder(root.left,arr) + postorder(root.right,arr) + arr.append(root.data) + + +if __name__ == '__main__': + root = Node(10) + root.left = Node(30) + root.right = Node(15) + root.left.left = Node(20) + root.right.right = Node(5) + + arr=[] + postorder(root,arr) + mainroot=Node(arr[0]) + arr.pop(0) + for i in arr: + insert(Node(i),mainroot) + inorder(mainroot) \ No newline at end of file diff --git a/Data Structures/BST/find path.py b/Data Structures/BST/find path.py new file mode 100644 index 0000000..0bbeb56 --- /dev/null +++ b/Data Structures/BST/find path.py @@ -0,0 +1,36 @@ +class Node: + def __init__(self, data): + self.data = data + self.left = self.right = None + + +def inorder(root): + if root: + inorder(root.left) + print(root.data,end=" ") + inorder(root.right) + +def findPath(root, path, i=0): + if i >= len(path): + return True + if root.data == path[i]: + return (findPath(root.left, path, i + 1) or findPath(root.right, path, i + 1)) + else: + return False + + +if __name__ == '__main__': + root = Node(1) + root.left = Node(2) + root.right = Node(3) + root.left.left = Node(4) + root.left.right = Node(5) + + inorder(root) + print() + path=[1, 2, 5] + if(findPath(root, path)): + print("Path Found") + else: + print("No Such Path") + diff --git a/Data Structures/BST/insert delete node.py b/Data Structures/BST/insert delete node.py new file mode 100644 index 0000000..b8b7045 --- /dev/null +++ b/Data Structures/BST/insert delete node.py @@ -0,0 +1,93 @@ +class Node: + def __init__(self, data): + self.data = data + self.left = self.right = None + + +def insert(node, root): + if root is None: + root = node + else: + if node.data < root.data: + if root.left is None: + root.left = node + else: + insert(node, root.left) + if node.data > root.data: + if root.right is None: + root.right = node + else: + insert(node, root.right) + + +def isleaf(node): + if node.left is None and node.right is None: + return True + else: + return False + + +def hasonechild(node): + if node.left != None and node.right == None: + return 1 + elif node.left == None and node.right != None: + return 2 + else: + return 0 + + +def hastwochild(node): + if node.left is not None and node.right is not None: + return True + else: + return False + + +def inordersuccessor(root): + min = root + while root.left: + min = root.left + root = root.left + return min + + +def delete(data, root): + if root is None: + return None + elif root.data > data: + root.left = delete(data, root.left) + elif root.data < data: + root.right = delete(data, root.right) + else: + if isleaf(root): + root = None + elif hasonechild(root) == 1: + root = root.left + elif hasonechild(root) == 2: + root = root.right + elif hastwochild(root): + min = inordersuccessor(root.right) + root.data = min.data + root.right = delete(min.data, root.right) + return root + + +def inorder(root): + if root: + inorder(root.left) + print(root.data, end=" ") + inorder(root.right) + + +if __name__ == '__main__': + root = Node(50) + root.left = Node(40) + root.right = Node(70) + root.right.left = Node(60) + root.right.right = Node(80) + print("before delete:") + inorder(root) + print() + print("after delete:") + delete(50, root) + inorder(root) \ No newline at end of file diff --git a/Data Structures/BST/is bst.py b/Data Structures/BST/is bst.py new file mode 100644 index 0000000..eac95a1 --- /dev/null +++ b/Data Structures/BST/is bst.py @@ -0,0 +1,38 @@ +class Node: + def __init__(self, data): + self.data = data + self.left = self.right = None + + +def isbst(root): + if(root == None): + return True + if(isless(root.data, root.left) and isgreat(root.data, root.right) and isbst(root.left) and isbst(root.right)): + return True + else: + return False + +def isless(data,root): + if root is None: + return True + if(root.data < data and isless(data,root.left) and isless(data,root.right)): + return True + else: + return False + +def isgreat(data,root): + if root is None: + return True + if(root.data > data and isgreat(data,root.left) and isgreat(data,root.right)): + return True + else: + return False + +if __name__ == '__main__': + root=Node(2) + root.left=Node(1) + root.right=Node(3) + if(isbst(root)): + print("Yes") + else: + print("No") \ No newline at end of file diff --git a/Data Structures/BST/lowest common ancestor.py b/Data Structures/BST/lowest common ancestor.py new file mode 100644 index 0000000..e641372 --- /dev/null +++ b/Data Structures/BST/lowest common ancestor.py @@ -0,0 +1,37 @@ +class Node: + def __init__(self, data): + self.data = data + self.left = self.right = None + + +def inorder(root): + if root: + inorder(root.left) + print(root.data,end=" ") + inorder(root.right) + + +def lca(root,n1,n2): + if root is None: + return None + if root.data > n1 and root.data > n2 : + return(lca(root.left,n1,n2)) + if root.data < n1 and root.data < n2 : + return(lca(root.right,n1,n2)) + + return root + + + +if __name__ == '__main__': + root = Node(20) + root.left = Node(8) + root.right = Node(22) + root.left.left = Node(4) + root.left.right = Node(12) + root.left.right.left = Node(10) + root.left.right.right = Node(14) + + inorder(root) + print() + print(lca(root,10,22).data) \ No newline at end of file diff --git a/Data Structures/BST/tree traversal.py b/Data Structures/BST/tree traversal.py new file mode 100644 index 0000000..ac3baeb --- /dev/null +++ b/Data Structures/BST/tree traversal.py @@ -0,0 +1,57 @@ +class node: + def __init__(self, data): + self.data = data + self.left = self.right = None + +def inorder(root): + if root: + inorder(root.left) + print(root.data,end=" ") + inorder(root.right) + + +def preorder(root): + if root: + print(root.data,end=" ") + preorder(root.left) + preorder(root.right) + + +def postorder(root): + if root: + postorder(root.left) + postorder(root.right) + print(root.data,end=" ") + + +def levelorder(root): + if root is None: + return + q = [] + q.append(root) + while (len(q)>0): + temp = q.pop(0) + if temp: + print(temp.data,end=" ") + q.append(temp.left) + q.append(temp.right) + else: + print("N",end=" ") + + + +if __name__=='__main__': + root = node(1) + root.left = node(2) + root.right = node(3) + root.left.left = node(4) + root.left.right = node(5) + root.right.right = node(7) + + inorder(root) + print() + preorder(root) + print() + postorder(root) + print() + levelorder(root) \ No newline at end of file