0% found this document useful (0 votes)
16 views19 pages

Dsa Manual - 1

A Binary Search Tree (BST) is a binary tree where each node has a left subtree with values less than the node's key and a right subtree with values greater than the node's key, with no duplicates allowed. The document outlines functions for creating a BST, inserting nodes, finding the height of the tree, finding the minimum value, mirroring the tree, and searching for a value within the BST. Each function is defined with its respective logic for performing these operations.

Uploaded by

Sankalp Gajbhe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views19 pages

Dsa Manual - 1

A Binary Search Tree (BST) is a binary tree where each node has a left subtree with values less than the node's key and a right subtree with values greater than the node's key, with no duplicates allowed. The document outlines functions for creating a BST, inserting nodes, finding the height of the tree, finding the minimum value, mirroring the tree, and searching for a value within the BST. Each function is defined with its respective logic for performing these operations.

Uploaded by

Sankalp Gajbhe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

A Binary Search Tree is a special

type of binary tree where each node


follows these rules:

Left Subtree contains values less


than the node’s key.

Right Subtree contains values greater


than the node’s key.

No duplicate nodes.

Both left and right subtrees are also


BSTs.
1. create BST
Function insert(node, key):
if node is None:
return new Node(key)
if key < node.key:
node.left = insert(node.left, key)
else:
node.right = insert(node.right, key)
return node

2.insert new node


insert(root, new_key)

3.Find Number of Nodes in Longest Path from Root


(Tree Height)

Function height(node):
if node is None:
return 0
return 1 + max(height(node.left), height(node.right))
4. Find Minimum Data Value in the Tree
Function findMin(node):
while node.left is not None:
node = node.left
return node.key

5.Mirror the Tree

Function mirror(node):
if node is None:
return
swap node.left and node.right
mirror(node.left)
mirror(node.right)

6.Search a Value in the BST


Function search(node, key):
if node is None or node.key == key:
return node
if key < node.key:
return search(node.left, key)
else:
return search(node.right, key)

You might also like