Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are going to build a binary search tree to organize book information. The bo

ID: 3686128 • Letter: Y

Question

You are going to build a binary search tree to organize book information. The book information consists of a title, author, and year of publication. The tree will be initialized by reading in the information from a file. The information is line oriented and will consist of the name of the book, the author (last name first), and the year of publication. Once your tree is built you are to do an inorder traversal and print out all of the information. To test the tree you are to do a series of searches by book title. The search strings will also be line oriented and come from a file. You are to keep report on whether each title is found and how many comparisons were required to find it. If a node is not found you should still be reporting on the number of comparisons required to come to this decision.

The input file will be named books.txt and will contain the information as described. A sample file will be posted with this project. The search file will be named searches.txt and a sample will also be posted with this file.

Work in small parts. Build the tree first and see that searches work. Start small. A single node should be easy to test.

C++ plz

Explanation / Answer

public class Node { private object data; private Node left, right; #region Constructors public Node() : this(null) {} public Node(object data) : this(data, null, null) {} public Node(object data, Node left, Node right) { this.data = data; this.left = left; this.right = right; } #endregion #region Public Properties public object Value { get { return data; } set { data = value; } } public Node Left { get { return left; } set { left = value; } } public Node Right { get { return right; } set { right = value; } } #endregion } public class BinaryTree { private Node root; public BinaryTree() { root = null; } #region Public Methods public virtual void Clear() { root = null; } #endregion #region Public Properties public Node Root { get { return root; } set { root = value; } } #endregion } BinaryTree btree = new BinaryTree(); btree.Root = new Node("BOONAME1"); btree.Root.Left = new Node("BOONAME2"); btree.Root.Right = new Node("BOONAME3"); btree.Root.Left.Left = new Node("BOOK NAME4"); btree.Root.Right.Right = new Node(5); btree.Root.Left.Left.Right = new Node("BOOKNAME6"); btree.Root.Right.Right.Right = new Node("BOOKNAME7"); btree.Root.Right.Right.Right.Right = new Node("BOOKNAME8"); protected virtual string PreorderTraversal(Node current, string separator) { if (current != null) { StringBuilder sb = new StringBuilder(); sb.Append(current.Value.ToString()); sb.Append(separator); sb.Append(PreorderTraversal(current.Left, separator)); sb.Append(PreorderTraversal(current.Right, separator)); return sb.ToString(); } else return String.Empty; } public virtual Node Search(IComparable data) { return SearchHelper(root, data); } protected virtual Node SearchHelper(Node current, IComparable data) { if (current == null) return null; // node was not found else { int result = current.Value.CompareTo(data); if (result == 0) // they are equal - we found the data return current; else if (result > 0) { // current.Value > n.Value // therefore, if the data exists it is in current's left subtree return SearchHelper(current.Left, data); } else // result < 0 { // current.Value current // therefore, the parent's left subtree is now current's Left subtree parent.Left = current.Left; else if (result < 0) // parent.Value 0) // parent.Value > current // therefore, the parent's left subtree is now current's right subtree parent.Left = current.Right; else if (result < 0) // parent.Value 0) // parent.Value > current // therefore, the parent's left subtree is now current's right subtree parent.Left = leftmost; else if (result < 0) // parent.Value
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote