Yahoo India Web Search

Search results

  1. Feb 22, 2024 · Learn about binary search tree, a data structure for storing and organizing sorted data. Find definitions, applications, operations, traversals, problems, quizzes and videos on BST.

    • Search Operation. The algorithm depends on the property of BST that if each left subtree has values below root and each right subtree has values above the root.
    • Insert Operation. Inserting a value in the correct position is similar to searching because we try to maintain the rule that the left subtree is lesser than root and the right subtree is larger than root.
    • Deletion Operation. There are three cases for deleting a node from a binary search tree. Case I. In the first case, the node to be deleted is the leaf node.
    • Python, Java and C/C++ Examples. Python. Java. C. C++ # Binary Search Tree operations in Python # Create a node class Node: def __init__(self, key): self.key = key self.left = None self.right = None # Inorder traversal def inorder(root): if root is not None: # Traverse left inorder(root.left) # Traverse root print(str(root.key) + "->", end=' ') # Traverse right inorder(root.right) # Insert a node def insert(node, key): # Return a new node if the tree is empty if node is None: return Node(key) # Traverse to the right place and insert the node if key < node.key: node.left = insert(node.left, key) else: node.right = insert(node.right, key) return node # Find the inorder successor def minValueNode(node): current = node # Find the leftmost leaf while(current.left is not None): current = current.left return current # Deleting a node def deleteNode(root, key): # Return if the tree is empty if root is None: return root # Find the node to be deleted if key < root.key: root.left = deleteNode(root.left, key) elif(key > root.key): root.right = deleteNode(root.right, key) else: # If the node is with only one child or no child if root.left is None: temp = root.right root = None return temp elif root.right is None: temp = root.left root = None return temp # If the node has two children, # place the inorder successor in position of the node to be deleted temp = minValueNode(root.right) root.key = temp.key # Delete the inorder successor root.right = deleteNode(root.right, temp.key) return root root = None root = insert(root, 8) root = insert(root, 3) root = insert(root, 1) root = insert(root, 6) root = insert(root, 7) root = insert(root, 10) root = insert(root, 14) root = insert(root, 4) print("Inorder traversal: ", end=' ') inorder(root) print("\nDelete 10") root = deleteNode(root, 10) print("Inorder traversal: ", end=' ') inorder(root)
  2. Searching in Binary search tree. Searching means to find or locate a specific element or node in a data structure. In Binary search tree, searching a node is easy because elements in BST are stored in a specific order. The steps of searching a node in Binary Search tree are listed as follows -

  3. Jun 24, 2024 · Binary Search Tree is a data structure used in computer science for organizing and storing data in a sorted manner. Binary search tree follows all properties of binary tree and its left child contains values less than the parent node and the right child contains values greater than the parent node. This hierarchical structure allows for ...

  4. O (n) Fig. 1: A binary search tree of size 9 and depth 3, with 8 at the root. In computer science, a binary search tree ( BST ), also called an ordered or sorted binary tree, is a rooted binary tree data structure with the key of each internal node being greater than all the keys in the respective node's left subtree and less than the ones in ...

  5. Nov 16, 2019 · Learn what a binary search tree (BST) is, how it works, and how to perform basic operations on it. See examples, diagrams, and code implementation of BST in C.

  6. People also ask

  7. May 28, 2024 · Learn how to create, insert, delete, search, and traverse a binary search tree in C++. A binary search tree is a binary tree where each node has only smaller or larger values in its subtree.

  1. People also search for