From f57072e7ce21b2f44d0a052c1dbaa7cada5fbe76 Mon Sep 17 00:00:00 2001 From: Daniel Moix <dwm69@drexel.edu> Date: Tue, 29 Aug 2023 17:21:39 -0400 Subject: [PATCH] Adding final lecture --- cs171/Lect09/hello.py | 8 +++++ cs171/hello.py | 1 - cs172/Lect09/BSTree.py | 64 +++++++++++++++++++++++++++++++++++++ cs172/Lect09/Node..py | 21 ++++++++++++ cs172/Lect09/Node.py | 39 ++++++++++++++++++++++ cs172/{ => Lect09}/hello.py | 0 6 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 cs171/Lect09/hello.py delete mode 100644 cs171/hello.py create mode 100644 cs172/Lect09/BSTree.py create mode 100644 cs172/Lect09/Node..py create mode 100644 cs172/Lect09/Node.py rename cs172/{ => Lect09}/hello.py (100%) diff --git a/cs171/Lect09/hello.py b/cs171/Lect09/hello.py new file mode 100644 index 0000000..e2c4f5f --- /dev/null +++ b/cs171/Lect09/hello.py @@ -0,0 +1,8 @@ + + +def swap(list, i, j): + temp = list[i] + list[i] = list[j] + list[j] = temp + + \ No newline at end of file diff --git a/cs171/hello.py b/cs171/hello.py deleted file mode 100644 index 061448a..0000000 --- a/cs171/hello.py +++ /dev/null @@ -1 +0,0 @@ -print("Hello CS 171") diff --git a/cs172/Lect09/BSTree.py b/cs172/Lect09/BSTree.py new file mode 100644 index 0000000..d1af1ec --- /dev/null +++ b/cs172/Lect09/BSTree.py @@ -0,0 +1,64 @@ +from Node import Node + +class BSTree: + + def __init__(self): + self.__root = None + + def __str__(self): + return str(self.__root) + + def contains(self, value, parent="ROOT"): + if str(parent) == "ROOT": + parent = self.__root + + if parent is None: + return False + + if parent.getData() == value: + return True + elif value < parent.getData(): + return self.contains(value, parent.getLeft()) + else: + return self.contains(value, parent.getRight()) + + + def insert(self, value, parent="ROOT"): + + if self.__root is None: + self.__root = Node(value) + else: + if str(parent) == "ROOT": + parent = self.__root + + if value <= parent.getData(): + if(parent.getLeft() is None): + # Place it to the left + parent.setLeft(Node(value)) + else: + self.insert(value, parent.getLeft()) + else: + if(parent.getRight() is None): + # Place it to the right + parent.setRight(Node(value)) + else: + self.insert(value, parent.getRight()) + +if __name__ == "__main__": + + myTree = BSTree() + myTree.insert(8) + myTree.insert(6) + myTree.insert(7) + print(myTree.contains(5)) + +# root = Node(25) +# left = Node(10) +# right = Node(50) +# +# root.setLeft(left) +# root.setRight(right) +# +# print(root) +# print(root.getLeft()) +# print(root.getRight()) diff --git a/cs172/Lect09/Node..py b/cs172/Lect09/Node..py new file mode 100644 index 0000000..c1615c8 --- /dev/null +++ b/cs172/Lect09/Node..py @@ -0,0 +1,21 @@ +class Node: + + def __init__(self, data, left=None, right=None): + self.__data = data + self.__left = left + self.__right = right + + def setLeft(self, newLeft): + self.__left = newLeft + + def setRight(self, newRight): + self.__right = newRight + + def getLeft(self): + return self.__left + + def getRight(self): + return self.__right + + def getData(self): + return self.__data \ No newline at end of file diff --git a/cs172/Lect09/Node.py b/cs172/Lect09/Node.py new file mode 100644 index 0000000..f54d749 --- /dev/null +++ b/cs172/Lect09/Node.py @@ -0,0 +1,39 @@ +class Node: + + def __init__(self, data, left=None, right=None): + self.__data = data + self.__left = left + self.__right = right + + def setLeft(self, newLeft): + self.__left = newLeft + + def setRight(self, newRight): + self.__right = newRight + + def getLeft(self): + return self.__left + + def getRight(self): + return self.__right + + def getData(self): + return self.__data + + def __str__(self): + return str(self.__data) + + def __lt__(self, other): + return self.__data < other.getData() + + def __gt__(self, other): + return self.__data > other.getData() + + def __eq__(self, other): + return self.__data == other.getData() + + def __le__(self, other): + return self.__data <= other.getData() + + def __ge__(self, other): + return self.__data >= other.getData() \ No newline at end of file diff --git a/cs172/hello.py b/cs172/Lect09/hello.py similarity index 100% rename from cs172/hello.py rename to cs172/Lect09/hello.py -- GitLab