首页 > 其他 > 详细

Introduction to Binary Trees and Binary Search Trees

时间:2019-07-13 21:40:58      阅读:109      评论:0      收藏:0      [点我收藏+]


Lab 8
Objectives
Introduction to Binary Trees and Binary Search Trees
Practice with implementing an interface with both reference and array based implementations
Practice with extending a class and overriding methods
Part I – implementing a BinaryTree interface
The following is a UML representation of a BinaryTree abstract data type. We have provided you with
the interface BinaryTree.java and the classes ArrayBasedBinaryTree.java,
RefBasedBinaryTree.java and TreeNode.java. The methods in ArrayBasedBinaryTree and
RefBasedBinaryTree have been left as stubs for you to complete.
1. Start by completing the implementation of ArrayBasedBinaryTree
A small main is included in the class that will allow you to test in isolation by compiling and running:
javac ArrayBasedBinaryTree.java
java ArrayBasedBinaryTree
When you are complete, it should insert the given elements and have the expected output for the calls
to the traversal and toString methods.
Tips:
- The calculation of the indices of the left and right subtree is dependent on the index of the root
ie. if root is 0, left is 2i+1 and right is 2i+2, if root is 1, left is 2i and right is 2i+1

代写Binary Trees作业、代做Java程序语言作业、代写TreeNode留学生作业
convince yourself of this by drawing a tree of height 3 and numbering the elements in a level order
(top to bottom, left to right) starting at 0 and then repeat with a numbering starting at 1
- The traversal methods are the simplest to write recursively – you will need helper methods that
takes the index of a tree element as a parameter much like the recursive list methods you wrote.
BinaryTree
<<interface>>
+ insert(): void
+ inOrder(): void
+ preOrder(): void
+ postOrder(): void
+ toString(): String
RefBasedBinaryTree
# root: TreeNode
+ RefBasedBinaryTree()
+ insert(Integer): void
# height(int): void
+ toString(): String
ArrayBasedBinaryTree
# CAPACITY: int
# data: Integer[]
# root: int
# size: int
+ ArrayBasedBinaryTree()
+ insert(Integer): void
# getLeft(int): int
# getRight(int): int
+ toString(): String
TreeNode
# data: Integer
# left: TreeNode
# right: TreeNode
+ TreeNode(Integer)
+ getValue(): Integer
+ setValue(Integer): void
+ getLeft(): TreeNode
+ setLeft(TreeNode): void
+ getRight(): TreeNode
+ setRight(TreeNode): void
ArrayBasedBinarySearchTree
+ ArrayBasedBinaryTree()
+ insert(Integer)
RefBasedBinarySearchTree
+ RefBasedBinarySearchTree()
+ insert(Integer): void
CHECK POINT – get your lab TA to check off after you have completed this. They will want to see you
compile and run ArrayBasedBinaryTree.java
2. Next complete the implementation of RefBasedBinaryTree
Again, a small main is included in the class allowing you to compile and run:
javac RefBasedBinaryTree.java
java RefBasedBinaryTree
When you are complete it should insert the given elements and have the expected output for the calls to
the traversal and toString methods.
NOTE: the insertion algorithm is not the same as the ArrayBasedBinaryTree implementation so
the traversals will not have the same output.
Take the time to understand what the insert method is doing by hand-drawing the tree that will be
created by the calls to insert in the main method. Use this drawing to ensure your traversal and
toString methods are correct.
Tips:
- The traversal methods are the simplest to write recursively – you will need helper methods that
take a TreeNode as a parameter much like the recursive list methods you wrote.
CHECK POINT – get your lab TA to check off after you have completed this. They will want to see your
hand-drawn tree and see you compile and run RefBasedBinaryTree.java
Part II – Extending BinaryTree
In this part of the lab you will implement the ArrayBasedBinarySearchTree.java and
RefBasedBinarySearchTree.java that will extend the ArrayBasedBinaryTree.java and
RefBasedBinaryTree.java respectively (as shown in the UML).
RECALL: A Binary Search Tree maintains the invariant that for every element in the tree, every element in
its left subtree must be smaller than the parent and every element in its right subtree must be larger than
the parent
1. Create a new class for that ArrayBasedBinarySearchTree.java that extends
ArrayBasedBinarySearchTree.java
2. Understand which methods you will inherit from the super class
3. Implement the required methods that you will override from the super class (insertion will be much
different as the insert must maintain the invariant of the Binary Search Tree)
4. Add a main to the class to test this class
5. Repeat steps 1-4 for RefBasedBinarySearchTree.java
CHECK POINT – get your lab TA to check off after you have completed this. They will want to see the
methods you implemented in ArrayBasedBinarySearchTree and RefBasedBinarySearchTree
and see you run the main in each.

因为专业,所以值得信赖。如有需要,请加QQ:99515681 或邮箱:99515681@qq.com

微信:codehelp

Introduction to Binary Trees and Binary Search Trees

原文:https://www.cnblogs.com/pythonhea/p/11182149.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!