Python Library for Self-Balancing BST
Here we will see simulating the library framework TreeSet which is available in Java on Python. There are situations that arise to disallow duplicate entries especially in any management software where an object is uniquely identified by its uniqueness. In this post, let us see how to do that in Pythonic way.
In our implementation, “TreeSet” class is a Binary-tree set like the Java TreeSet. The TreeSet will be sorted automatically when adding/removing elements. Duplicate elements will not be added.
The functionalities that have been included are :
- Inserting an element into the TreeSet.
- Inserting multiple elements into the TreeSet.
- Fetching the ceil value from the TreeSet.
- Fetching the floor value from the TreeSet.
- Deleting an element from the TreeSet.
- Deleting all the elements from the TreeSet.
- Fetching the shallow copy of the TreeSet.
- Popping an element from the TreeSet.
treeSet.floor(5) : 3
treeSet.ceiling(4) : 7
treeSet.floor(2) : 1
treeSet.ceiling(3) : 3
treeSet.floor(10) : 10
treeSet : [1, 3, 7, 10]
treeSet : [10, 20, 30, 70]
Adding 40 to the above, it is arranged in sorted order : [10, 20, 30, 40, 70]
After removing 70 [10, 20, 30, 40]
After removing 50 [10, 20, 30, 40]
After adding many elements : [10, 20, 30, 40, 50, 60]
60 60 50 40 30 20
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course