Implement a [binary search tree](https://en.wikipedia.org/wiki/Binary_search_tre
ID: 3571509 • Letter: I
Question
Implement a [binary search tree](https://en.wikipedia.org/wiki/Binary_search_tree) that supports insert, search, and [traversal](https://en.wikipedia.org/wiki/Tree_traversal). ## Public API Your program must provide the following public API. ``` pub struct Tree<T> { ... } impl<T: Ord> Tree<T> { /// Creates an empty tree pub fn new() -> Self { unimplemented!() } /// Returns `false` if `key` already exists in the tree, and `true` otherwise. pub fn insert(&mut self, key: T) -> bool { unimplemented!() } /// Returns `true` if `key` exists in the tree, and `false` otherwise. pub fn find(&self, key: &T) -> bool { unimplemented!() } /// Returns the preorder traversal of the tree. pub fn preorder(&self) -> Vec<&T> { unimplemented!() } /// Returns the inorder traversal of the tree. pub fn inorder(&self) -> Vec<&T> { unimplemented!() } /// Returns the postorder traversal of the tree. pub fn postorder(&self) -> Vec<&T> { unimplemented!() } } ```
Explanation / Answer
public void preorder(TreeNode root) information
System.out.printf("%d ",root.data);
preorder(root.left);
preorder(root.right);
}
}
public void preorderIter(TreeNode root)
}
}
import java.util.Stack;
public category BinaryTree {
public static category TreeNode
}
// algorithmic answer
public void preorder(TreeNode root) information
System.out.printf("%d ",root.data);
preorder(root.left);
preorder(root.right);
}
}
// repetitious answer
public void preorderIter(TreeNode root)
}
}
public static void main(String[] args)
making a binary tree
TreeNode rootNode=createBinaryTree();
System.out.println("Using algorithmic solution:");
bi.preorder(rootNode);
System.out.println();
System.out.println("-------------------------");
System.out.println("Using repetitious solution:");
bi.preorderIter(rootNode);
}
public static TreeNode createBinaryTree()
come rootNode;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.