0% found this document useful (0 votes)
0 views2 pages

Programming Project 6.55

The document contains a Python implementation of a Linked Binary Tree class with methods for insertion, searching, and traversal (in-order, pre-order, post-order, and breadth-first). The class supports binary search tree operations but has placeholders for the insertion and search methods. The main section demonstrates inserting values into the tree and searching for specific values.

Uploaded by

c.am.usp.am.m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views2 pages

Programming Project 6.55

The document contains a Python implementation of a Linked Binary Tree class with methods for insertion, searching, and traversal (in-order, pre-order, post-order, and breadth-first). The class supports binary search tree operations but has placeholders for the insertion and search methods. The main section demonstrates inserting values into the tree and searching for specific values.

Uploaded by

c.am.usp.am.m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

# Use a given base code

import queue

class LinkedBinaryTree:
class _Node:
__slots__ = '_el', '_l', '_r'

def __init__(self, element, left=None, right=None):


self._el = element
self._l = left
self._r = right

def __init__(self):
self._root = None
self._size = 0

def __len__(self):
return self._size

def height(self):
return self._height(self._root)

def _height(self, p):


if not p:
return 0
return 1 + max(self._height(p._l), self._height(p._r))

def _in_order(self, p):


if p:
yield from self._in_order(p._l)
yield p._el
yield from self._in_order(p._r)

def _pre_order(self, p):


if p:
yield p._el
yield from self._pre_order(p._l)
yield from self._pre_order(p._r)

def _post_order(self, p):


if p:
yield from self._post_order(p._l)
yield from self._post_order(p._r)
yield p._el

def breadth_first(self):
q = queue.Queue()
q.put(self._root)
while not q.empty():
p = q.get()
if p:
print(p._el)
if p._l:
q.put(p._l)
if p._r:
q.put(p._r)
def bst_insert(self, key):
if self._root is None:
self._root = self._Node(key)
else:
self._bst_insert(self._root, key)
self._size += 1

def _bst_insert(self, node, key):


# Your code here
pass

def bst_search(self, key):


return self._bst_search(self._root, key)

def _bst_search(self, node, key):


# Your code here
pass

def print_in_order(self):
# Your code here
pass

if __name__ == "__main__":
tree = LinkedBinaryTree()
for value in [7, 3, 2, 5, 6, 9, 1, 4, 8, 10]:
tree.bst_insert(value)

print("Search for 4:", tree.bst_search(4)) # true


print("Search for 2:", tree.bst_search(2)) # false

print("In-order traversal:")
tree.print_in_order()

You might also like