Program Code to Copy
class Node:
def __init__(self):
self.left = None
self.right = None
self.data = None
def __init__(self, data):
self.left = None
self.right = None
self.data = data
# Insert Node
def insert(self, data):
if self.data:
if data < self.data:
if self.left is None:
self.left = Node(data)
else:
self.left.insert(data)
elif data > self.data:
if self.right is None:
self.right = Node(data)
else:
self.right.insert(data)
else:
self.data = data
def stringMethod(self, root) :
res = ""
if root.data is None:
res = "Empty Tree"
return res
else :
res1 = "\nPreorder: " + str(self.preorderTraversal(root)) + "\n"
res2 = "Inorder: " + str(self.inorderTraversal(root)) + "\n"
res3 = "Postorder: " + str(self.postorderTraversal(root)) + "\n"
res = res1 + res2 + res3
return res
def find(self, node, x):
if (node == None):
return False
if (node.data == x):
return True
""" then recur on left subtree """
res1 = self.find(node.left, x)
""" now recur on right subtree """
res2 = self.find(node.right, x)
return res1 or res2
#***************Helper Methods****************************
# Print the Tree
def PrintTree(self):
if self.left:
self.left.PrintTree()
print( self.data),
if self.right:
self.right.PrintTree()
# Preorder traversal
# Root -> Left -> Right
def preorderTraversal(self, root):
res = ""
if root is not None:
res = res + str(root.data) + " "
res = res + self.preorderTraversal(root.left)
res = res + self.preorderTraversal(root.right)
else:
res = res + 'N' + " "
return res
# Inorder traversal
# Left -> Root -> Right
def inorderTraversal(self, root):
res = ""
if root:
res = res + self.inorderTraversal(root.left)
res = res + str(root.data) + " "
res = res + self.inorderTraversal(root.right)
else :
res = res + 'N' + " "
return res
# Postorder traversal
# Root -> Left -> Right
def postorderTraversal(self, root):
res = ""
if root:
res = res + self.postorderTraversal(root.left)
res = res + self.postorderTraversal(root.right)
res = res + str(root.data) + " "
else:
res = res + 'N' + " "
return res
print("Test Bed for Binary Search Tree Class")
test = input("Enter test Number (1-6):\n")
root = Node(None)
data = [6, 8, 4, 3, 7, 5, 9]
print("Make a Balanced Tree.")
print("Inserting: ", end = " ")
print(data)
for i in range (0, len(data)):
root.insert(data[i])
print(root.stringMethod(root))
print(root.find(root,5))
Program Screenshots
Program Output
Empty Tree with "find" method returning False.
Tree with data
in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree...
In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...
C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...
Binary Search tree Implementation of a BST class that include the following operations: - Insertion, Search, Deletion - Traversals: Inorder, Preorder, Postorder using c++
LANGUAGE: C++ Write a class to create the binary tree (insert, delete, search, exit) and display the output using inorder, preorder and postorder tree traversal methods.
Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...
Binary Search Trees (a) 5 pointsl Insert 5, 12, 7, 1, 6, 3, 13, 2, 10, 11 into an empty binary search tree in the given order. Show the resulting BST after every insertion. (b) 5 points) What are the preorder, inorder, and postorder traversals of the BST you have after (a)? (c) 5 points Delete 2, 7, 5, 6, 11 from the BST you have after (a) in the given order Show the resulting BST after every deletion.
1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the number of leaf nodes in the tree. 2) Extend the Binary Search Tree ADT to include a public method singleParent-Count that returns the number of nodes in the tree that have only one child. 3) The Binary search tree ADT is extended to include a boolean method similarTrees that receives references to two binary trees and determines whether the shapes of the trees are...
IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implementing a simple class that represents a "node” in a binary search tree, as follows. public class MyTreeNode<t extends Comparable<T>> { public T data; public MyTreeNode<T> leftchild; public MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for the simple binary search tree interface. public interface...
java A University would like to implement its students registery as a binary search tree, calledStudentBST. Write an Student node class, called StudentNode, to hold the following information about an Student: - id (as a int) - gpa (as double) StudentNode should have constructors and methods (getters, setters, and toString()) to manage Write the StudentBST class, which is a binary search tree to hold objects of the class StudentNode. The key in each node is the id. : import.java.ArrayList; public...
Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate insert operation in binary search tree. Please fill in the missing part for the insert method to make the program work. The expected output should be as follows. 20 30 40 50 60 70 80 Part B: Find Lowest Common Ancestor (LCA) of a Binary Search Tree. According to WikiPedia definition , The lowest common ancestor is defined between two nodes v and...