Published in

0xCODE

# Binary Search Tree Operations In Data Structures

`N < 50 -> LS `
1. Start by evaluating the key value from the root.
2. If it is less than the root, then recurse to the left.
3. If it is greater than the root, then recurse to the right.
4. Finally, insert the node where you recurse.
`# Create a class that represents an individual node in a BSTclass Node:def __init__(self, key):self.left = Noneself.right = Noneself.val = key# Function to insert a new node with the given keydef insert(root, key):if root is None:return Node(key)else:if root.val == key:return rootelif root.val < key:root.right = insert(root.right, key)else:root.left = insert(root.left, key)return root# Function to do inorder tree traversaldef inorder(root):if root:inorder(root.left)print(root.val)inorder(root.right)# Test the functionsr = Node(50)r = insert(r, 30)r = insert(r, 20)r = insert(r, 40)r = insert(r, 70)r = insert(r, 60)r = insert(r, 80)# Print the node valuesinorder(r)`
`20 30 40 50 60 70 80`
`   50 /     \30     70/ \    / \20 40 60 80`
1. Start from R(50)
2. Look under Left Subtree N(30)
3. From parent N(40)
4. Move from left to write (unsorted) to search for node value (O(n)) starting from N(15) … until N(10).
`R(50) -> N(30) -> N(40) -> N(15) -> N(22) -> N(10)`
`R(50) -> N(30) -> N(40) -> N(45) -> N(10)`
`R(50) -> N(30) -> N(40) -> N(10)`

--

--

## More from 0xCODE

Essays and Articles on software engineering, development and computer science.

## Get the Medium app

Editor HD-PRO, DevOps, Cybersecurity, Blockchain, Software Development, Engineering, Photography, Technology