Add some cool queue, stacks, strings, math, bit manipulation examples (#35)

This commit is contained in:
Dr. Marina Souza, PhD 2023-07-16 17:35:14 -07:00 committed by GitHub
parent f3ee2cdf52
commit 0f455a0322
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 932 additions and 13 deletions

View file

@ -0,0 +1,105 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
class Node(object):
def __init__(self, value):
self.value = value
self.right = None
self.left = None
def add(self, value):
new_node = Node(value)
if not self.value:
self.value = new_node
elif not self.left:
self.left = new_node
elif not self. right:
self.right = new_node
else:
self.left = self.left.add(value)
return self # without this, it doesn't add!
def search(self, item):
if self.value == item:
return True
found = False
if (self.left and self.left.search(item)) or \
(self.right and self.right.search(item)):
found = True
return found
def preorder(self):
yield self.value
if self.left:
for node in self.left.preorder():
yield node
if self.right:
for node in self.right.preorder():
yield node
def postorder(self):
yield self.value
if self.left:
for node in self.left.postorder():
yield node
if self.right:
for node in self.right.postorder():
yield node
def inorder(self):
yield self.value
if self.left:
for node in self.left.inorder():
yield node
if self.right:
for node in self.right.inorder():
yield node
class BinaryTree(object):
def __init__(self):
self.root = None
def add(self, value):
if not self.root:
self.root = Node(value)
else:
self.root.add(value)
def search(self, item):
if self.root:
return self.root.search(item)
def preorder(self):
if self.root:
return list(self.root.preorder())
def inorder(self):
if self.root:
return list(self.root.inorder())
def postorder(self):
if self.root:
return list(self.root.postorder())
if __name__ == '__main__':
print("\n\n🌳🌳🌳 Testing BinaryTree 🌳🌳🌳")
bt = BinaryTree()
array1 = [4, 1, 4, 6, 7, 9, 10, 5, 11, 5]
print(f'\n🟡 Adding {array1} to the tree...')
for i in array1:
bt.add(i)
print(f"🟢 Print the tree preorder: {bt.preorder()}")
print(f"🟢 Print the tree inorder: {bt.inorder()}")
print(f"🟢 Print the tree postorder: {bt.postorder()}")
print(f'\n🟢 Search for node 5: {bt.search(5)}')
print(f'❌ Search for node 15: {bt.search(15)}')

View file

@ -0,0 +1,93 @@
## trees
<br>
### `Tree.py`
<br>
```python
> python3 Trees.py
🌴🌴🌴 Testing SimpleTree 🌴🌴🌴
a
b
d
e
c
h
g
🌳🌳🌳 Testing BinaryTree 🌳🌳🌳
🟡 Adding [4, 1, 4, 6, 7, 9, 10, 5, 11, 5] to the tree...
🟢 Printing the tree in preorder...
4
1
6
9
5
5
11
10
7
4
🟢 Searching for node 5: True
❌ Searching for node 15: False
❌ Is root a leaf? False
🟢 Is root full? True
❌ Is the tree balanced? False
❌ Is the tree a binary search tree? False
🎄🎄🎄 Testing BinarySearchTree 🎄🎄🎄
🟡 Adding [4, 1, 4, 6, 7, 9, 10, 5, 11, 5] to the tree...
❌ Item 4 not added as BSTs do not support repetition.
❌ Item 5 not added as BSTs do not support repetition.
🟢 Printing the tree in preorder:
4
1
6
5
7
9
10
11
🟢 Searching for node 5: True
❌ Searching for node 15: False
❌ Is root a leaf? False
🟢 Is root full? True
🟢 Largest node? 11
🟢 Smallest node? 1
❌ Is the tree balanced? False
🟢 Is the tree a binary search tree? True
```
<br>
### `BinaryTree.py`
<br>
* a clean implementation adapted from the class above.
```python
> python3 BinaryTree.py
🌳🌳🌳 Testing BinaryTree 🌳🌳🌳
🟡 Adding [4, 1, 4, 6, 7, 9, 10, 5, 11, 5] to the tree...
🟢 Print the tree preorder: [4, 1, 6, 9, 5, 5, 11, 10, 7, 4]
🟢 Print the tree inorder: [4, 1, 6, 9, 5, 5, 11, 10, 7, 4]
🟢 Print the tree postorder: [4, 1, 6, 9, 5, 5, 11, 10, 7, 4]
🟢 Search for node 5: True
❌ Search for node 15: False
```

View file

@ -294,5 +294,3 @@ if __name__ == '__main__':
print(f'🟢 Smallest node? {bst.smallest_node(bst.root)}')
print(f'❌ Is the tree balanced? {bst.is_balanced(bst.root)}')
print(f'🟢 Is the tree a binary search tree? {bst.is_binary_search_tree(bst.root)}')