Question: 1 . Write a Java program implementing a Binary Tree which stores a set of integer numbers. ( Not have duplicate nodes ) 1 )
Write a Java program implementing a Binary Tree which stores a set of integer numbers. Not have
duplicate nodes
Define the BinaryTree interface
Define the Node class
Instance Variables
o E elementgenerics framework
o Node Parentpointer: reference to the parent node Selfreferential
o Node leftpointer: reference to the left child node Selfreferential
o Node rightpointer: reference to the right child node Selfreferential
Constructor
Methods
o E getElementReturn the value of this node
o Node getParentReturn the reference of the parent node
o Node getLeftReturn the reference of left child node
o NodegetRightReturn the reference of right child node
o setElementE e Set value to this node
o setParentNode vSet reference to the parent node
o setLeftNode vSet reference to left child node
o setRightNode vSet reference to right node
Define the class LinkedBinaryTree which implements BinaryTree Interface
Instance Variables
o Node root root of the tree.
o int size number of nodes in the tree
Constructor
Methods
o int sizereturns the number of nodes that are contained in the tree
o Boolean isEmptyreturns true if the tree does not contain any nodes
o Node rootreturns the reference of the root of the tree or null if empty
o Node parentNode vreturns the reference of the parent of node v or null if
v is the root
o int numChildrenNode vreturns the number of children of node v
o Boolean isInternalNode vreturns true if node v has at least one child
o Boolean isExternalNode vreturns true if node v does not have any children
o Boolean isRootNode vreturns true if node v is the root of the tree
o Node leftvreturns the reference of the left child of node v; error if v has no
left child
o Node rightvreturns the reference of the right child of node v; error if v has
no right child
o Node siblingvReturns the reference of the sibling of node vor null if v has
no sibling
o Boolean hasLeftvtest whether node v has a left child
o Boolean hasRightvtest whether node v has a right child
o Node addRootE ecreates a root for an empty tree, storing e as the element,
and returns the reference of that root; an error occurs if the tree is not empty
o Node addLeftNode v E ecreates a left child of node v storing element e
and returns the reference of the new node; an error occurs if v already has a
left child
o Node addRightNode v E ecreates a right child of node v storing element
e and returns the reference of the new node; an error occurs if v already has a
right child
o E setNode v E eReplaces the element stored at node v with element e and
returns the previously stored element.
o E removeNode vRemoves the node v replacing it with its child if any
and returns the element that had been stored at v; an error occurs if v has two
children
Define the class TestLinkedBinaryTree which tests all function if applicable of
LinkedBinaryTree
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
