@@ -68,50 +68,120 @@ $ pip install datastructpy
68
68
69
69
## Usage
70
70
71
- ### Example usage:
71
+ ### Example Usage
72
72
73
73
``` python
74
- from datastructpy.non_linear.trees.binary_search_tree import BinarySearchTree
75
-
76
- # Create a Binary Search Tree from a list of elements
77
- elements = [10 , 5 , 15 , 8 ]
78
- bst = BinarySearchTree.list_to_tree(elements)
79
-
80
- # Check the structure of the tree
81
- print (" Tree Structure After Creation:" )
82
- print (bst.root.key) # Output: 10
83
- print (bst.root.left.key) # Output: 5
84
- print (bst.root.right.key) # Output: 15
85
- print (bst.root.left.right.key) # Output: 8
86
-
87
- # Insert new nodes into the BST
88
- print (" Inserting New Elements:" )
89
- bst.insert(12 ) # Insert into right subtree of 10
90
- bst.insert(2 ) # Insert into left subtree of 5
91
- print (bst.root.right.left.key) # Output: 12 (left child of 15)
92
- print (bst.root.left.left.key) # Output: 2 (left child of 5)
93
-
94
- # Search for values in the BST
95
- print (" Searching for Keys:" )
96
- print (bst.search(8 ) is not None ) # Output: True (8 exists in the tree)
97
- print (bst.search(20 ) is None ) # Output: True (20 does not exist in the tree)
98
-
99
- # Delete a node
100
- print (" Deleting Nodes:" )
101
- bst.delete(5 ) # Delete the left child of the root
74
+ from datastructpy import BinarySearchTree
75
+
76
+ # Build a BST from a list of values.
77
+ bst = BinarySearchTree.list_to_tree([10 , 5 , 15 , 8 ])
78
+
79
+ # After creation, the tree looks like:
80
+ #
81
+ # 10
82
+ # / \
83
+ # 5 15
84
+ # \
85
+ # 8
86
+ #
87
+ print (" Initial Tree:" )
88
+ print (" Root:" , bst.root.key)
89
+ print (" Left Child:" , bst.root.left.key)
90
+ print (" Right Child:" , bst.root.right.key)
91
+ print (" Right of Left Child:" , bst.root.left.right.key)
92
+ ```
93
+ ``` text
94
+ Initial Tree:
95
+ Root: 10
96
+ Left Child: 5
97
+ Right Child: 15
98
+ Right of Left Child: 8
99
+ ```
100
+ ``` python
101
+ # Insert new elements.
102
+ bst.insert(12 ) # Inserts into left subtree of 15.
103
+ bst.insert(2 ) # Inserts into left subtree of 5.
104
+
105
+ # Now the tree is updated to:
106
+ #
107
+ # 10
108
+ # / \
109
+ # 5 15
110
+ # / \ /
111
+ # 2 8 12
112
+ #
113
+ print (" \n After Insertion:" )
114
+ print (" New left of 5:" , bst.root.left.left.key)
115
+ print (" New left of 15:" , bst.root.right.left.key)
116
+ ```
117
+ ``` text
118
+ After Insertion:
119
+ New left of 5: 2
120
+ New left of 15: 12
121
+ ```
122
+ ``` python
123
+ # Search for keys.
124
+ print (" \n Search Results:" )
125
+ print (" 8 found:" , bst.search(8 ) is not None )
126
+ print (" 20 found:" , bst.search(20 ) is not None )
127
+ ```
128
+ ``` text
129
+ Search Results:
130
+ 8 found: True
131
+ 20 found: False
132
+ ```
133
+ ``` python
134
+ # Delete node with key 5.
135
+ # The in-order successor of 5 (which is 8) replaces it.
136
+ bst.delete(5 )
137
+ print (" \n After Deleting 5:" )
102
138
if bst.root.left:
103
- print (bst.root.left.key) # Output: 8 (5 replaced by its in-order successor )
139
+ print (" New left child of 10: " , bst.root.left.key )
104
140
else :
105
- print (bst.root.left) # Output: None (if no successor is present)
106
-
107
- bst.delete(10 ) # Delete the root
108
- print (bst.root.key) # Output: 15 (new root after deletion)
109
-
110
- # Final structure of the tree
111
- print (" Final Tree Structure:" )
112
- print (bst.root.key) # Output: 15
113
- print (bst.root.left.key) # Output: 8
114
- print (bst.root.right.left.key) # Output: 12
141
+ print (" Left child is None" )
142
+ # The tree now becomes:
143
+ # 10
144
+ # / \
145
+ # 8 15
146
+ # / /
147
+ # 2 12
148
+ ```
149
+ ``` text
150
+ After Deleting 5:
151
+ New left child of 10: 8
152
+ ```
153
+ ``` python
154
+ # Delete the root (10). The in-order successor of 10 (which is 12) replaces it.
155
+ bst.delete(10 )
156
+ print (" \n After Deleting 10:" )
157
+ print (" New Root:" , bst.root.key)
158
+ ```
159
+ ``` text
160
+ After Deleting 10:
161
+ New Root: 12
162
+ ```
163
+ ``` python
164
+ # Final structure.
165
+ print (" \n Final Tree Structure:" )
166
+ print (" Root:" , bst.root.key)
167
+ print (" Left Child:" , bst.root.left.key)
168
+ if bst.root.right:
169
+ print (" Right Child:" , bst.root.right.key)
170
+ else :
171
+ print (" Right Child: None" )
172
+ # The final tree is:
173
+ #
174
+ # 12
175
+ # / \
176
+ # 8 15
177
+ # /
178
+ # 2
179
+ ```
180
+ ``` text
181
+ Final Tree Structure:
182
+ Root: 12
183
+ Left Child: 8
184
+ Right Child: 15
115
185
```
116
186
117
187
## Running Tests
0 commit comments