Python implementation of the Binary Search Tree definition
In Python, a Binary Search Tree is a kind of ordered or sorted tree whose internal nodes are connected to one another in such a manner that they are able to interact with one another without any interruptions. The idea of a binary search tree is accompanied by the fact that nodes are arranged and organised in such a way that allows for the addition, fast lookup, and removal of data items to be used for manipulation, which is then followed by the creation of lookup tables and dynamic sets. This fact is included with the binary search tree concept. When determining whether a subtree will be on a node’s left or right, the key and value combination plays a significant role. Syntax:
The following is a flowchart of the syntax for the binary search Tree in Python:
follows:
class A_node:
class="token punctuation">(self, key),
ken comment">#Write the driver program & print with a logger to understand the flow.
How does Python’s Binary Search tree really work?
The following describes how the Binary Search Tree works, however to further demonstrate it, let’s look at an example. If we suppose that the binary tree as shown below and that it is organised in such a manner that there is no ordering nor sequence preserved, then we need to figure out how a tree that contains so many different numbers would compare and arrange itself.
A binary tree is a data structure that is based on a node-based binary tree and includes the following characteristics:
characteristic
- The left subtree of a node contains nodes with keys lesser than the node’s key.
- Then, the right subtree of a node contains a key greater than the node’s key.
- The left and right subtree must have a binary search tree where the duplicate nodes are not all allowed.
- All the above three nodes must have keys for operations like insertion, deletion search of minimum and maximum.
Due to the fact that a Binary tree is a data structure, it is useful for storing information or numerical values in an ordered fashion.
manner.
- The order present is proportional to half of the remaining values that makes it entirely less for computation or manipulation when it comes to maintaining the time complexities religiously.
- This is far better than linear time which is basically used for storing data i.e., the items stored in the form of key and value which makes it quite slower than corresponding operations present on the hash table.
- Binary search tree is a rooted binary tree whose internal nodes mainly makes comparison study for storing the keys after making comparisons which distinguish a lot of sets, multisets, and items present in the tree.
- Basically, the comparisons are made, and a total preorder is maintained with a three-way comparison subroutine which is part of the actual routine.
- The time complexities associated with each of the traversals have some significance in the sense it depends on how the elements are getting rotated left to right or vice versa like the worst case, best case, or average case.
- Duplicates are also allowed but then again restrictions are there with certain scenarios due to which sometimes they are not present or allowed.
Examples
Let’s talk about some instances of using the Python Binary Search Tree.
Example #1
This programme shows one of the utility functions of BST, which involves searching for a way to make the key-value pair return some value that satisfies a certain requirement .
constraint.
def search_keyval (root_0, keyval):
Explanation:
if root_0 is None or root_0.keyval == key:
return root_0
if root_0.keyval < key:
return search (root_0.right,key)
return search_keyval (root_0.left,key)
Python was used to create this one-of-a-kind search function, whose purpose is to verify that the component of the BST that must be operational in order to fulfil the requirements is in fact there. Because of this, the subtree will become more organised with the appropriate components in the overall tree.
Example #2
This programme shows how to insert a key together with its value wherever in the BST in order to execute manipulations, and the results of such manipulations are shown in the output.
below.
class Node_def:
Output:
def __init__(self_0, key_1):
self_0.left = None
self_0.right = None
self_0.val = key_1
def insert_val(root_0, key_1):
if root_0 is None:
return Node_def(key_1)
else:
if root_0.val == key_1:
return root_0
elif root_0.val < key_1:
root_0.right = insert_val(root_0.right, key_1)
else:
root_0.left = insert_val(root_0.left, key_1)
return root_0
def inorder_val(root_0):
if root_0:
inorder_val(root_0.left)
print(root_0.val)
inorder_val(root_0.right)
r_9 = Node_def(50)
r_9 = insert_val(r_9, 12)
r_9 = insert_val(r_9, 20)
r_9 = insert_val(r_9, 2)
r_9 = insert_val(r_9, 15)
r_9 = insert_val(r_9, 60)
r_9 = insert_val(r_9, 76)
inorder_val(r_9)
Explanation of the picture @@@3
In the series of programmes that have been provided, insertions of elements are made in order to arrange them in a specific order. In addition, some comparisons are made in response to the requirements, and the entire process of comparison and calculation begins at the root and continues until it reaches its conclusion. There are several time difficulties involved with this, such as the fact that search and insertion are performed with comparison and search on heights and level wise, which is performed with the worst time complexity of O. (h).
Case #3
This programme displays the binary search tree, which depicts the items that are present in the whole tree even with subtrees that include elements in an ordered fashion, as demonstrated in the output of the programme.
below.
class Node_0:
Output:
def __init__(self_3, data_0):
self_3.left = None
self_3.right = None
self_3.node = data_0
def insert(self_3, data_0):
if self_3.node:
if data_0 < self_3.node: if self_3.left is None: self_3.left = Node_0(data_0) else: self_3.left.insert(data_0) elif data_0 > self_3.node:
if self_3.right is None:.node = data_0
def Print_Tree(self_3):
if self_3.left:
self_3.left.Print_Tree()
print(self_3.node, end=' ')
if self_3.right:
self_3.right.Print_Tree()
root_0 = Node_0(18)
root_0.insert(5)
root_0.insert(24)
root_0.insert(2)
root_0.insert(22)
root_0.insert(18)
root_0.insert(11)
root_0.Print_Tree()
This programme in the binary tree seeks to organise all of the elements inside it in a hierarchy such that the elements may be represented by each element in the order in which they are placed. This is done so that each element can have some value in it. The temporal complexity of the full binary search tree is then computed by Node 0 when it has completed its work.