SlideShare a Scribd company logo
1 of 13
Download to read offline
1)(JAVA) Extend the Binary Search Tree ADT to include a public method leafCount that returns
the number of leaf nodes in the tree.
Thank You in Advanced.
Below, I also have attached the BinarySearchTree and the Interface File.
BinarySearchTree:
//----------------------------------------------------------------------------
// BinarySearchTree.java by Dale/Joyce/Weems Chapter 8
//
// Defines all constructs for a reference-based BST
//----------------------------------------------------------------------------
package ch08.trees;
import ch05.queues.*;
import ch03.stacks.*;
import support.BSTNode;
public class BinarySearchTree>
implements BSTInterface
{
protected BSTNode root; // reference to the root of this BST
boolean found; // used by remove
// for traversals
protected LinkedUnbndQueue inOrderQueue; // queue of info
protected LinkedUnbndQueue preOrderQueue; // queue of info
protected LinkedUnbndQueue postOrderQueue; // queue of info
public BinarySearchTree()
// Creates an empty BST object.
{
root = null;
}
public boolean isEmpty()
// Returns true if this BST is empty; otherwise, returns false.
{
return (root == null);
}
private int recSize(BSTNode tree)
// Returns the number of elements in tree.
{
if (tree == null)
return 0;
else
return recSize(tree.getLeft()) + recSize(tree.getRight()) + 1;
}
public int size()
// Returns the number of elements in this BST.
{
return recSize(root);
}
public int size2()
// Returns the number of elements in this BST.
{
int count = 0;
if (root != null)
{
LinkedStack> hold = new LinkedStack>();
BSTNode currNode;
hold.push(root);
while (!hold.isEmpty())
{
currNode = hold.top();
hold.pop();
count++;
if (currNode.getLeft() != null)
hold.push(currNode.getLeft());
if (currNode.getRight() != null)
hold.push(currNode.getRight());
}
}
return count;
}
private boolean recContains(T element, BSTNode tree)
// Returns true if tree contains an element e such that
// e.compareTo(element) == 0; otherwise, returns false.
{
if (tree == null)
return false; // element is not found
else if (element.compareTo(tree.getInfo()) < 0)
return recContains(element, tree.getLeft()); // Search left subtree
else if (element.compareTo(tree.getInfo()) > 0)
return recContains(element, tree.getRight()); // Search right subtree
else
return true; // element is found
}
public boolean contains (T element)
// Returns true if this BST contains an element e such that
// e.compareTo(element) == 0; otherwise, returns false.
{
return recContains(element, root);
}
private T recGet(T element, BSTNode tree)
// Returns an element e from tree such that e.compareTo(element) == 0;
// if no such element exists, returns null.
{
if (tree == null)
return null; // element is not found
else if (element.compareTo(tree.getInfo()) < 0)
return recGet(element, tree.getLeft()); // get from left subtree
else
if (element.compareTo(tree.getInfo()) > 0)
return recGet(element, tree.getRight()); // get from right subtree
else
return tree.getInfo(); // element is found
}
public T get(T element)
// Returns an element e from this BST such that e.compareTo(element) == 0;
// if no such element exists, returns null.
{
return recGet(element, root);
}
private BSTNode recAdd(T element, BSTNode tree)
// Adds element to tree; tree retains its BST property.
{
if (tree == null)
// Addition place found
tree = new BSTNode(element);
else if (element.compareTo(tree.getInfo()) <= 0)
tree.setLeft(recAdd(element, tree.getLeft())); // Add in left subtree
else
tree.setRight(recAdd(element, tree.getRight())); // Add in right subtree
return tree;
}
public void add (T element)
// Adds element to this BST. The tree retains its BST property.
{
root = recAdd(element, root);
}
private T getPredecessor(BSTNode tree)
// Returns the information held in the rightmost node in tree
{
while (tree.getRight() != null)
tree = tree.getRight();
return tree.getInfo();
}
private BSTNode removeNode(BSTNode tree)
// Removes the information at the node referenced by tree.
// The user's data in the node referenced by tree is no
// longer in the tree. If tree is a leaf node or has only
// a non-null child pointer, the node pointed to by tree is
// removed; otherwise, the user's data is replaced by its
// logical predecessor and the predecessor's node is removed.
{
T data;
if (tree.getLeft() == null)
return tree.getRight();
else if (tree.getRight() == null)
return tree.getLeft();
else
{
data = getPredecessor(tree.getLeft());
tree.setInfo(data);
tree.setLeft(recRemove(data, tree.getLeft()));
return tree;
}
}
private BSTNode recRemove(T element, BSTNode tree)
// Removes an element e from tree such that e.compareTo(element) == 0
// and returns true; if no such element exists, returns false.
{
if (tree == null)
found = false;
else if (element.compareTo(tree.getInfo()) < 0)
tree.setLeft(recRemove(element, tree.getLeft()));
else if (element.compareTo(tree.getInfo()) > 0)
tree.setRight(recRemove(element, tree.getRight()));
else
{
tree = removeNode(tree);
found = true;
}
return tree;
}
public boolean remove (T element)
// Removes an element e from this BST such that e.compareTo(element) == 0
// and returns true; if no such element exists, returns false.
{
root = recRemove(element, root);
return found;
}
private void inOrder(BSTNode tree)
// Initializes inOrderQueue with tree elements in inOrder order.
{
if (tree != null)
{
inOrder(tree.getLeft());
inOrderQueue.enqueue(tree.getInfo());
inOrder(tree.getRight());
}
}
private void preOrder(BSTNode tree)
// Initializes preOrderQueue with tree elements in preOrder order.
{
if (tree != null)
{
preOrderQueue.enqueue(tree.getInfo());
preOrder(tree.getLeft());
preOrder(tree.getRight());
}
}
private void postOrder(BSTNode tree)
// Initializes postOrderQueue with tree elements in postOrder order.
{
if (tree != null)
{
postOrder(tree.getLeft());
postOrder(tree.getRight());
postOrderQueue.enqueue(tree.getInfo());
}
}
public int reset(int orderType)
// Initializes current position for an iteration through this BST
// in orderType order. Returns current number of nodes in the BST.
{
int numNodes = size();
if (orderType == INORDER)
{
inOrderQueue = new LinkedUnbndQueue();
inOrder(root);
}
else
if (orderType == PREORDER)
{
preOrderQueue = new LinkedUnbndQueue();
preOrder(root);
}
if (orderType == POSTORDER)
{
postOrderQueue = new LinkedUnbndQueue();
postOrder(root);
}
return numNodes;
}
public T getNext (int orderType)
// Preconditions: The BST is not empty
// The BST has been reset for orderType
// The BST has not been modified since the most recent reset
// The end of orderType iteration has not been reached
//
// Returns the element at the current position on this BST for orderType
// and advances the value of the current position based on the orderType.
{
if (orderType == INORDER)
return inOrderQueue.dequeue();
else
if (orderType == PREORDER)
return preOrderQueue.dequeue();
else
if (orderType == POSTORDER)
return postOrderQueue.dequeue();
else return null;
}
}
BSTInterface:
//----------------------------------------------------------------------------
// BSTInterface.java by Dale/Joyce/Weems Chapter 8
//
// Interface for a class that implements a binary search tree (BST).
//
// The trees are unbounded and allow duplicate elements, but do not allow null
// elements. As a general precondition, null elements are not passed as
// arguments to any of the methods.
//
// The tree supports iteration through its elements in INORDER, PREORDER,
// and POSTORDER.
//----------------------------------------------------------------------------
package ch08.trees;
public interface BSTInterface>
{
// used to specify traversal order
static final int INORDER = 1;
static final int PREORDER = 2;
static final int POSTORDER = 3;
boolean isEmpty();
// Returns true if this BST is empty; otherwise, returns false.
int size();
// Returns the number of elements in this BST.
boolean contains (T element);
// Returns true if this BST contains an element e such that
// e.compareTo(element) == 0; otherwise, returns false.
boolean remove (T element);
// Removes an element e from this BST such that e.compareTo(element) == 0
// and returns true; if no such element exists, returns false.
T get(T element);
// Returns an element e from this BST such that e.compareTo(element) == 0;
// if no such element exists, returns null.
void add (T element);
// Adds element to this BST. The tree retains its BST property.
int reset(int orderType);
// Initializes current position for an iteration through this BST
// in orderType order. Returns current number of nodes in the BST.
T getNext (int orderType);
// Preconditions: The BST is not empty
// The BST has been reset for orderType
// The BST has not been modified since the most recent reset
// The end of orderType iteration has not been reached
//
// Returns the element at the current position on this BST for orderType
// and advances the value of the current position based on the orderType.
}
Solution
import ch05.queues.*;
import ch03.stacks.*;
import support.BSTNode;
public category BinarySearchTree>
implements BSTInterface
relevance the basis of this BST
boolean found; // utilized by take away
// for traversals
protected LinkedUnbndQueue inOrderQueue; // queue of information
protected LinkedUnbndQueue preOrderQueue; // queue of information
protected LinkedUnbndQueue postOrderQueue; // queue of information
public BinarySearchTree()
// Creates Associate in Nursing empty BST object.
public mathematician isEmpty()
// Returns true if this BST is empty; otherwise, returns false.
private int recSize(BSTNode tree)
// Returns the quantity of components in tree.
public int size()
// Returns the quantity of components during this BST.
public int size2()
// Returns the quantity of components during this BST.
}
return count;
}
private mathematician recContains(T part, BSTNode tree)
// Returns true if tree contains a part e such
// e.compareTo(element) == zero; otherwise, returns false.
{
if (tree == null)
return false; // part isn't found
else if (element.compareTo(tree.getInfo()) < 0)
return recContains(element, tree.getLeft()); // Search left subtree
else if (element.compareTo(tree.getInfo()) > 0)
return recContains(element, tree.getRight()); // Search right subtree
else
return true; // part is found
}
public mathematician contains (T element)
// Returns true if this BST contains a part e such
// e.compareTo(element) == zero; otherwise, returns false.
private T recGet(T part, BSTNode tree)
// Returns a part e from tree such e.compareTo(element) == zero;
// if no such part exists, returns null.
{
if (tree == null)
return null; // part isn't found
else if (element.compareTo(tree.getInfo()) < 0)
return recGet(element, tree.getLeft()); // get from left subtree
else
if (element.compareTo(tree.getInfo()) > 0)
return recGet(element, tree.getRight()); // get from right subtree
else
return tree.getInfo(); // part is found
}
public T get(T element)
// Returns a part e from this BST such e.compareTo(element) == 0;
// if no such part exists, returns null.
private BSTNode recAdd(T part, BSTNode tree)
// Adds part to tree; tree retains its BST property.
public void add (T element)
// Adds part to the current BST. The tree retains its BST property.
private T getPredecessor(BSTNode tree)
// Returns the knowledge command within the right node in tree
private BSTNode removeNode(BSTNode tree)
// Removes the knowledge at the node documented by tree.
// The user's information within the node documented by tree is not any
// longer within the tree. If tree could be a leaf node or has solely
// a non-null kid pointer, the node pointed to by tree is
// removed; otherwise, the user's information is replaced by its
// logical forerunner and therefore the predecessor's node is removed.
}
private BSTNode recRemove(T part, BSTNode tree)
// Removes a part e from tree such e.compareTo(element) == 0
// and returns true; if no such part exists, returns false.
return tree;
}
public mathematician take away (T element)
// Removes a part e from this BST such e.compareTo(element) == 0
// and returns true; if no such part exists, returns false.
private void inOrder(BSTNode tree)
// Initializes inOrderQueue with tree components in inOrder order.
}
private void preOrder(BSTNode tree)
// Initializes preOrderQueue with tree components in preOrder order.
}
private void postOrder(BSTNode tree)
// Initializes postOrderQueue with tree components in postOrder order.
}
public int reset(int orderType)
// Initializes current position for Associate in Nursing iteration through this BST
// in orderType order. Returns current variety of nodes within the BST.
else
if (orderType == PREORDER)
if (orderType == POSTORDER)
return numNodes;
}
public T getNext (int orderType)
// Preconditions: The BST isn't empty
// The BST has been reset for orderType
// The BST has not been changed since the foremost recent reset
// the tip of orderType iteration has not been reached
//
// Returns the part at the present position on this BST for orderType
// and advances the worth of the present position supported the orderType.
come back null;
}
}
BSTInterface:
//----------------------------------------------------------------------------
// BSTInterface.java by Dale/Joyce/Weems Chapter eight
//
// Interface for a category that implements a binary search tree (BST).
//
// The trees square measure boundless and permit duplicate components, however don't enable
null
// components. As a general precondition, null components don't seem to be passed as
// arguments to any of the strategies.
//
// The tree supports iteration through its components in INORDER, PREORDER,
// and POSTORDER.
//----------------------------------------------------------------------------
package ch08.trees;
public interface BSTInterface>
accustomed specify traversal order
static final int INORDER = 1;
static final int PREORDER = 2;
static final int POSTORDER = 3;
boolean isEmpty();
// Returns true if this BST is empty; otherwise, returns false.
int size();
// Returns the quantity of components during this BST.
boolean contains (T element);
// Returns true if this BST contains a part e such
// e.compareTo(element) == 0; otherwise, returns false.
boolean take away (T element);
// Removes a part e from this BST such e.compareTo(element) == 0
// and returns true; if no such part exists, returns false.
T get(T element);
// Returns a part e from this BST such e.compareTo(element) == 0;
// if no such part exists, returns null.
void add (T element);
// Adds part to the current BST. The tree retains its BST property.
int reset(int orderType);
// Initializes current position for Associate in Nursing iteration through this BST
// in orderType order. Returns current variety of nodes within the BST.
T getNext (int orderType);
// Preconditions: The BST isn't empty
// The BST has been reset for orderType
// The BST has not been changed since the foremost recent reset
// the tip of orderType iteration has not been reached
//
// Returns the part at the present position on this BST for orderType
// and advances the worth of the present position supported the orderType.
}

More Related Content

Similar to Extend BST to count leaf nodes

Help to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdfHelp to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdfcontact32
 
Required to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxRequired to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxdebishakespeare
 
On the code which has a class in which I implementing a binary tree .pdf
On the code which has a class in which I implementing a binary tree .pdfOn the code which has a class in which I implementing a binary tree .pdf
On the code which has a class in which I implementing a binary tree .pdfwasemanivytreenrco51
 
For the code below complete the preOrder() method so that it perform.pdf
For the code below complete the preOrder() method so that it perform.pdfFor the code below complete the preOrder() method so that it perform.pdf
For the code below complete the preOrder() method so that it perform.pdfxlynettalampleyxc
 
MAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdfMAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdfadityastores21
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docxVictorXUQGloverl
 
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdfAssignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdfFootageetoffe16
 
Add these three functions to the class binaryTreeType (provided).W.pdf
Add these three functions to the class binaryTreeType (provided).W.pdfAdd these three functions to the class binaryTreeType (provided).W.pdf
Add these three functions to the class binaryTreeType (provided).W.pdfindiaartz
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfsiennatimbok52331
 
For this project, write a program that stores integers in a binary.docx
For this project, write a program that stores integers in a binary.docxFor this project, write a program that stores integers in a binary.docx
For this project, write a program that stores integers in a binary.docxbudbarber38650
 
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdfangelsfashion1
 
Please fix my errors class Iterator public Construc.pdf
Please fix my errors   class Iterator  public  Construc.pdfPlease fix my errors   class Iterator  public  Construc.pdf
Please fix my errors class Iterator public Construc.pdfkitty811
 
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docxAssg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docxfestockton
 
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdfarihantmobileselepun
 
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdfObjective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdfsivakumar19831
 
Implement the following specification of UnsortedType using circular.pdf
Implement the following specification of UnsortedType using circular.pdfImplement the following specification of UnsortedType using circular.pdf
Implement the following specification of UnsortedType using circular.pdfudit652068
 
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdfUsing c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdffashiongallery1
 
Implement the sequence class from Section 3.2 of the textbook. The d.pdf
Implement the sequence class from Section 3.2 of the textbook. The d.pdfImplement the sequence class from Section 3.2 of the textbook. The d.pdf
Implement the sequence class from Section 3.2 of the textbook. The d.pdfinfo961251
 
This is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdfThis is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdffcaindore
 
Please write in C++ and should be able to compile and debug.Thank yo.pdf
Please write in C++ and should be able to compile and debug.Thank yo.pdfPlease write in C++ and should be able to compile and debug.Thank yo.pdf
Please write in C++ and should be able to compile and debug.Thank yo.pdfajaycosmeticslg
 

Similar to Extend BST to count leaf nodes (20)

Help to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdfHelp to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdf
 
Required to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxRequired to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docx
 
On the code which has a class in which I implementing a binary tree .pdf
On the code which has a class in which I implementing a binary tree .pdfOn the code which has a class in which I implementing a binary tree .pdf
On the code which has a class in which I implementing a binary tree .pdf
 
For the code below complete the preOrder() method so that it perform.pdf
For the code below complete the preOrder() method so that it perform.pdfFor the code below complete the preOrder() method so that it perform.pdf
For the code below complete the preOrder() method so that it perform.pdf
 
MAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdfMAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdf
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
 
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdfAssignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
 
Add these three functions to the class binaryTreeType (provided).W.pdf
Add these three functions to the class binaryTreeType (provided).W.pdfAdd these three functions to the class binaryTreeType (provided).W.pdf
Add these three functions to the class binaryTreeType (provided).W.pdf
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
 
For this project, write a program that stores integers in a binary.docx
For this project, write a program that stores integers in a binary.docxFor this project, write a program that stores integers in a binary.docx
For this project, write a program that stores integers in a binary.docx
 
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 
Please fix my errors class Iterator public Construc.pdf
Please fix my errors   class Iterator  public  Construc.pdfPlease fix my errors   class Iterator  public  Construc.pdf
Please fix my errors class Iterator public Construc.pdf
 
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docxAssg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
 
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
 
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdfObjective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
 
Implement the following specification of UnsortedType using circular.pdf
Implement the following specification of UnsortedType using circular.pdfImplement the following specification of UnsortedType using circular.pdf
Implement the following specification of UnsortedType using circular.pdf
 
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdfUsing c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
 
Implement the sequence class from Section 3.2 of the textbook. The d.pdf
Implement the sequence class from Section 3.2 of the textbook. The d.pdfImplement the sequence class from Section 3.2 of the textbook. The d.pdf
Implement the sequence class from Section 3.2 of the textbook. The d.pdf
 
This is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdfThis is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdf
 
Please write in C++ and should be able to compile and debug.Thank yo.pdf
Please write in C++ and should be able to compile and debug.Thank yo.pdfPlease write in C++ and should be able to compile and debug.Thank yo.pdf
Please write in C++ and should be able to compile and debug.Thank yo.pdf
 

More from petercoiffeur18

John, a sociologist, will be focusing on how larger societal institu.pdf
John, a sociologist, will be focusing on how larger societal institu.pdfJohn, a sociologist, will be focusing on how larger societal institu.pdf
John, a sociologist, will be focusing on how larger societal institu.pdfpetercoiffeur18
 
Implement the ListArray ADT-Implement the following operations.pdf
Implement the ListArray ADT-Implement the following operations.pdfImplement the ListArray ADT-Implement the following operations.pdf
Implement the ListArray ADT-Implement the following operations.pdfpetercoiffeur18
 
In what ways do humans effect the environment Explain in 200 words.pdf
In what ways do humans effect the environment Explain in 200 words.pdfIn what ways do humans effect the environment Explain in 200 words.pdf
In what ways do humans effect the environment Explain in 200 words.pdfpetercoiffeur18
 
i need a taking turn method for a player vs computer battleship game.pdf
i need a taking turn method for a player vs computer battleship game.pdfi need a taking turn method for a player vs computer battleship game.pdf
i need a taking turn method for a player vs computer battleship game.pdfpetercoiffeur18
 
I am trying to change this code from STRUCTS to CLASSES, the members.pdf
I am trying to change this code from STRUCTS to CLASSES, the members.pdfI am trying to change this code from STRUCTS to CLASSES, the members.pdf
I am trying to change this code from STRUCTS to CLASSES, the members.pdfpetercoiffeur18
 
how internal resources and capabilities can be a source of sustainab.pdf
how internal resources and capabilities can be a source of sustainab.pdfhow internal resources and capabilities can be a source of sustainab.pdf
how internal resources and capabilities can be a source of sustainab.pdfpetercoiffeur18
 
For an organism that is growing using glucose as the electron donor, .pdf
For an organism that is growing using glucose as the electron donor, .pdfFor an organism that is growing using glucose as the electron donor, .pdf
For an organism that is growing using glucose as the electron donor, .pdfpetercoiffeur18
 
Exercise 5.6.28. For each of the following descriptions of a function.pdf
Exercise 5.6.28. For each of the following descriptions of a function.pdfExercise 5.6.28. For each of the following descriptions of a function.pdf
Exercise 5.6.28. For each of the following descriptions of a function.pdfpetercoiffeur18
 
Discuss concepts associated with mercantilism demonstrating key poli.pdf
Discuss concepts associated with mercantilism demonstrating key poli.pdfDiscuss concepts associated with mercantilism demonstrating key poli.pdf
Discuss concepts associated with mercantilism demonstrating key poli.pdfpetercoiffeur18
 
C++ Caesar Cipher project. Write your codes for the following functi.pdf
C++ Caesar Cipher project. Write your codes for the following functi.pdfC++ Caesar Cipher project. Write your codes for the following functi.pdf
C++ Caesar Cipher project. Write your codes for the following functi.pdfpetercoiffeur18
 
any idea#includeiostream using stdcout; using stdendl; .pdf
any idea#includeiostream using stdcout; using stdendl; .pdfany idea#includeiostream using stdcout; using stdendl; .pdf
any idea#includeiostream using stdcout; using stdendl; .pdfpetercoiffeur18
 
a) Use Newton’s Polynomials for Evenly Spaced data to derive the O(h.pdf
a) Use Newton’s Polynomials for Evenly Spaced data to derive the O(h.pdfa) Use Newton’s Polynomials for Evenly Spaced data to derive the O(h.pdf
a) Use Newton’s Polynomials for Evenly Spaced data to derive the O(h.pdfpetercoiffeur18
 
A radio station has a power output of 200 watts. What is the intensit.pdf
A radio station has a power output of 200 watts. What is the intensit.pdfA radio station has a power output of 200 watts. What is the intensit.pdf
A radio station has a power output of 200 watts. What is the intensit.pdfpetercoiffeur18
 
21) What are the fundamental particles of lepton and quarks How man.pdf
21) What are the fundamental particles of lepton and quarks How man.pdf21) What are the fundamental particles of lepton and quarks How man.pdf
21) What are the fundamental particles of lepton and quarks How man.pdfpetercoiffeur18
 
Case 1-A Transition to SupervisorTristan came in on the ground fl.pdf
Case 1-A Transition to SupervisorTristan came in on the ground fl.pdfCase 1-A Transition to SupervisorTristan came in on the ground fl.pdf
Case 1-A Transition to SupervisorTristan came in on the ground fl.pdfpetercoiffeur18
 
x , y Midterm Exsm-201840x Exam C httpsbb-montgomerycollege.blac.pdf
x , y Midterm Exsm-201840x Exam C httpsbb-montgomerycollege.blac.pdfx , y Midterm Exsm-201840x Exam C httpsbb-montgomerycollege.blac.pdf
x , y Midterm Exsm-201840x Exam C httpsbb-montgomerycollege.blac.pdfpetercoiffeur18
 
Why would a cell want to express genes in an operon Why notSol.pdf
Why would a cell want to express genes in an operon Why notSol.pdfWhy would a cell want to express genes in an operon Why notSol.pdf
Why would a cell want to express genes in an operon Why notSol.pdfpetercoiffeur18
 
When discussing the epidemiology of virus infections, the CDC often .pdf
When discussing the epidemiology of virus infections, the CDC often .pdfWhen discussing the epidemiology of virus infections, the CDC often .pdf
When discussing the epidemiology of virus infections, the CDC often .pdfpetercoiffeur18
 
What relationship is there between gobalization and development.pdf
What relationship is there between gobalization and development.pdfWhat relationship is there between gobalization and development.pdf
What relationship is there between gobalization and development.pdfpetercoiffeur18
 
what is the number between 4.5 and 4.75 on the number linew.pdf
what is the number between 4.5 and 4.75 on the number linew.pdfwhat is the number between 4.5 and 4.75 on the number linew.pdf
what is the number between 4.5 and 4.75 on the number linew.pdfpetercoiffeur18
 

More from petercoiffeur18 (20)

John, a sociologist, will be focusing on how larger societal institu.pdf
John, a sociologist, will be focusing on how larger societal institu.pdfJohn, a sociologist, will be focusing on how larger societal institu.pdf
John, a sociologist, will be focusing on how larger societal institu.pdf
 
Implement the ListArray ADT-Implement the following operations.pdf
Implement the ListArray ADT-Implement the following operations.pdfImplement the ListArray ADT-Implement the following operations.pdf
Implement the ListArray ADT-Implement the following operations.pdf
 
In what ways do humans effect the environment Explain in 200 words.pdf
In what ways do humans effect the environment Explain in 200 words.pdfIn what ways do humans effect the environment Explain in 200 words.pdf
In what ways do humans effect the environment Explain in 200 words.pdf
 
i need a taking turn method for a player vs computer battleship game.pdf
i need a taking turn method for a player vs computer battleship game.pdfi need a taking turn method for a player vs computer battleship game.pdf
i need a taking turn method for a player vs computer battleship game.pdf
 
I am trying to change this code from STRUCTS to CLASSES, the members.pdf
I am trying to change this code from STRUCTS to CLASSES, the members.pdfI am trying to change this code from STRUCTS to CLASSES, the members.pdf
I am trying to change this code from STRUCTS to CLASSES, the members.pdf
 
how internal resources and capabilities can be a source of sustainab.pdf
how internal resources and capabilities can be a source of sustainab.pdfhow internal resources and capabilities can be a source of sustainab.pdf
how internal resources and capabilities can be a source of sustainab.pdf
 
For an organism that is growing using glucose as the electron donor, .pdf
For an organism that is growing using glucose as the electron donor, .pdfFor an organism that is growing using glucose as the electron donor, .pdf
For an organism that is growing using glucose as the electron donor, .pdf
 
Exercise 5.6.28. For each of the following descriptions of a function.pdf
Exercise 5.6.28. For each of the following descriptions of a function.pdfExercise 5.6.28. For each of the following descriptions of a function.pdf
Exercise 5.6.28. For each of the following descriptions of a function.pdf
 
Discuss concepts associated with mercantilism demonstrating key poli.pdf
Discuss concepts associated with mercantilism demonstrating key poli.pdfDiscuss concepts associated with mercantilism demonstrating key poli.pdf
Discuss concepts associated with mercantilism demonstrating key poli.pdf
 
C++ Caesar Cipher project. Write your codes for the following functi.pdf
C++ Caesar Cipher project. Write your codes for the following functi.pdfC++ Caesar Cipher project. Write your codes for the following functi.pdf
C++ Caesar Cipher project. Write your codes for the following functi.pdf
 
any idea#includeiostream using stdcout; using stdendl; .pdf
any idea#includeiostream using stdcout; using stdendl; .pdfany idea#includeiostream using stdcout; using stdendl; .pdf
any idea#includeiostream using stdcout; using stdendl; .pdf
 
a) Use Newton’s Polynomials for Evenly Spaced data to derive the O(h.pdf
a) Use Newton’s Polynomials for Evenly Spaced data to derive the O(h.pdfa) Use Newton’s Polynomials for Evenly Spaced data to derive the O(h.pdf
a) Use Newton’s Polynomials for Evenly Spaced data to derive the O(h.pdf
 
A radio station has a power output of 200 watts. What is the intensit.pdf
A radio station has a power output of 200 watts. What is the intensit.pdfA radio station has a power output of 200 watts. What is the intensit.pdf
A radio station has a power output of 200 watts. What is the intensit.pdf
 
21) What are the fundamental particles of lepton and quarks How man.pdf
21) What are the fundamental particles of lepton and quarks How man.pdf21) What are the fundamental particles of lepton and quarks How man.pdf
21) What are the fundamental particles of lepton and quarks How man.pdf
 
Case 1-A Transition to SupervisorTristan came in on the ground fl.pdf
Case 1-A Transition to SupervisorTristan came in on the ground fl.pdfCase 1-A Transition to SupervisorTristan came in on the ground fl.pdf
Case 1-A Transition to SupervisorTristan came in on the ground fl.pdf
 
x , y Midterm Exsm-201840x Exam C httpsbb-montgomerycollege.blac.pdf
x , y Midterm Exsm-201840x Exam C httpsbb-montgomerycollege.blac.pdfx , y Midterm Exsm-201840x Exam C httpsbb-montgomerycollege.blac.pdf
x , y Midterm Exsm-201840x Exam C httpsbb-montgomerycollege.blac.pdf
 
Why would a cell want to express genes in an operon Why notSol.pdf
Why would a cell want to express genes in an operon Why notSol.pdfWhy would a cell want to express genes in an operon Why notSol.pdf
Why would a cell want to express genes in an operon Why notSol.pdf
 
When discussing the epidemiology of virus infections, the CDC often .pdf
When discussing the epidemiology of virus infections, the CDC often .pdfWhen discussing the epidemiology of virus infections, the CDC often .pdf
When discussing the epidemiology of virus infections, the CDC often .pdf
 
What relationship is there between gobalization and development.pdf
What relationship is there between gobalization and development.pdfWhat relationship is there between gobalization and development.pdf
What relationship is there between gobalization and development.pdf
 
what is the number between 4.5 and 4.75 on the number linew.pdf
what is the number between 4.5 and 4.75 on the number linew.pdfwhat is the number between 4.5 and 4.75 on the number linew.pdf
what is the number between 4.5 and 4.75 on the number linew.pdf
 

Recently uploaded

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Recently uploaded (20)

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 

Extend BST to count leaf nodes

  • 1. 1)(JAVA) Extend the Binary Search Tree ADT to include a public method leafCount that returns the number of leaf nodes in the tree. Thank You in Advanced. Below, I also have attached the BinarySearchTree and the Interface File. BinarySearchTree: //---------------------------------------------------------------------------- // BinarySearchTree.java by Dale/Joyce/Weems Chapter 8 // // Defines all constructs for a reference-based BST //---------------------------------------------------------------------------- package ch08.trees; import ch05.queues.*; import ch03.stacks.*; import support.BSTNode; public class BinarySearchTree> implements BSTInterface { protected BSTNode root; // reference to the root of this BST boolean found; // used by remove // for traversals protected LinkedUnbndQueue inOrderQueue; // queue of info protected LinkedUnbndQueue preOrderQueue; // queue of info protected LinkedUnbndQueue postOrderQueue; // queue of info public BinarySearchTree() // Creates an empty BST object. { root = null; } public boolean isEmpty() // Returns true if this BST is empty; otherwise, returns false. { return (root == null); }
  • 2. private int recSize(BSTNode tree) // Returns the number of elements in tree. { if (tree == null) return 0; else return recSize(tree.getLeft()) + recSize(tree.getRight()) + 1; } public int size() // Returns the number of elements in this BST. { return recSize(root); } public int size2() // Returns the number of elements in this BST. { int count = 0; if (root != null) { LinkedStack> hold = new LinkedStack>(); BSTNode currNode; hold.push(root); while (!hold.isEmpty()) { currNode = hold.top(); hold.pop(); count++; if (currNode.getLeft() != null) hold.push(currNode.getLeft()); if (currNode.getRight() != null) hold.push(currNode.getRight()); } } return count; } private boolean recContains(T element, BSTNode tree)
  • 3. // Returns true if tree contains an element e such that // e.compareTo(element) == 0; otherwise, returns false. { if (tree == null) return false; // element is not found else if (element.compareTo(tree.getInfo()) < 0) return recContains(element, tree.getLeft()); // Search left subtree else if (element.compareTo(tree.getInfo()) > 0) return recContains(element, tree.getRight()); // Search right subtree else return true; // element is found } public boolean contains (T element) // Returns true if this BST contains an element e such that // e.compareTo(element) == 0; otherwise, returns false. { return recContains(element, root); } private T recGet(T element, BSTNode tree) // Returns an element e from tree such that e.compareTo(element) == 0; // if no such element exists, returns null. { if (tree == null) return null; // element is not found else if (element.compareTo(tree.getInfo()) < 0) return recGet(element, tree.getLeft()); // get from left subtree else if (element.compareTo(tree.getInfo()) > 0) return recGet(element, tree.getRight()); // get from right subtree else return tree.getInfo(); // element is found } public T get(T element) // Returns an element e from this BST such that e.compareTo(element) == 0; // if no such element exists, returns null.
  • 4. { return recGet(element, root); } private BSTNode recAdd(T element, BSTNode tree) // Adds element to tree; tree retains its BST property. { if (tree == null) // Addition place found tree = new BSTNode(element); else if (element.compareTo(tree.getInfo()) <= 0) tree.setLeft(recAdd(element, tree.getLeft())); // Add in left subtree else tree.setRight(recAdd(element, tree.getRight())); // Add in right subtree return tree; } public void add (T element) // Adds element to this BST. The tree retains its BST property. { root = recAdd(element, root); } private T getPredecessor(BSTNode tree) // Returns the information held in the rightmost node in tree { while (tree.getRight() != null) tree = tree.getRight(); return tree.getInfo(); } private BSTNode removeNode(BSTNode tree) // Removes the information at the node referenced by tree. // The user's data in the node referenced by tree is no // longer in the tree. If tree is a leaf node or has only // a non-null child pointer, the node pointed to by tree is // removed; otherwise, the user's data is replaced by its // logical predecessor and the predecessor's node is removed. { T data;
  • 5. if (tree.getLeft() == null) return tree.getRight(); else if (tree.getRight() == null) return tree.getLeft(); else { data = getPredecessor(tree.getLeft()); tree.setInfo(data); tree.setLeft(recRemove(data, tree.getLeft())); return tree; } } private BSTNode recRemove(T element, BSTNode tree) // Removes an element e from tree such that e.compareTo(element) == 0 // and returns true; if no such element exists, returns false. { if (tree == null) found = false; else if (element.compareTo(tree.getInfo()) < 0) tree.setLeft(recRemove(element, tree.getLeft())); else if (element.compareTo(tree.getInfo()) > 0) tree.setRight(recRemove(element, tree.getRight())); else { tree = removeNode(tree); found = true; } return tree; } public boolean remove (T element) // Removes an element e from this BST such that e.compareTo(element) == 0 // and returns true; if no such element exists, returns false. { root = recRemove(element, root); return found; }
  • 6. private void inOrder(BSTNode tree) // Initializes inOrderQueue with tree elements in inOrder order. { if (tree != null) { inOrder(tree.getLeft()); inOrderQueue.enqueue(tree.getInfo()); inOrder(tree.getRight()); } } private void preOrder(BSTNode tree) // Initializes preOrderQueue with tree elements in preOrder order. { if (tree != null) { preOrderQueue.enqueue(tree.getInfo()); preOrder(tree.getLeft()); preOrder(tree.getRight()); } } private void postOrder(BSTNode tree) // Initializes postOrderQueue with tree elements in postOrder order. { if (tree != null) { postOrder(tree.getLeft()); postOrder(tree.getRight()); postOrderQueue.enqueue(tree.getInfo()); } } public int reset(int orderType) // Initializes current position for an iteration through this BST // in orderType order. Returns current number of nodes in the BST. { int numNodes = size(); if (orderType == INORDER)
  • 7. { inOrderQueue = new LinkedUnbndQueue(); inOrder(root); } else if (orderType == PREORDER) { preOrderQueue = new LinkedUnbndQueue(); preOrder(root); } if (orderType == POSTORDER) { postOrderQueue = new LinkedUnbndQueue(); postOrder(root); } return numNodes; } public T getNext (int orderType) // Preconditions: The BST is not empty // The BST has been reset for orderType // The BST has not been modified since the most recent reset // The end of orderType iteration has not been reached // // Returns the element at the current position on this BST for orderType // and advances the value of the current position based on the orderType. { if (orderType == INORDER) return inOrderQueue.dequeue(); else if (orderType == PREORDER) return preOrderQueue.dequeue(); else if (orderType == POSTORDER) return postOrderQueue.dequeue(); else return null; }
  • 8. } BSTInterface: //---------------------------------------------------------------------------- // BSTInterface.java by Dale/Joyce/Weems Chapter 8 // // Interface for a class that implements a binary search tree (BST). // // The trees are unbounded and allow duplicate elements, but do not allow null // elements. As a general precondition, null elements are not passed as // arguments to any of the methods. // // The tree supports iteration through its elements in INORDER, PREORDER, // and POSTORDER. //---------------------------------------------------------------------------- package ch08.trees; public interface BSTInterface> { // used to specify traversal order static final int INORDER = 1; static final int PREORDER = 2; static final int POSTORDER = 3; boolean isEmpty(); // Returns true if this BST is empty; otherwise, returns false. int size(); // Returns the number of elements in this BST. boolean contains (T element); // Returns true if this BST contains an element e such that // e.compareTo(element) == 0; otherwise, returns false. boolean remove (T element); // Removes an element e from this BST such that e.compareTo(element) == 0 // and returns true; if no such element exists, returns false. T get(T element); // Returns an element e from this BST such that e.compareTo(element) == 0;
  • 9. // if no such element exists, returns null. void add (T element); // Adds element to this BST. The tree retains its BST property. int reset(int orderType); // Initializes current position for an iteration through this BST // in orderType order. Returns current number of nodes in the BST. T getNext (int orderType); // Preconditions: The BST is not empty // The BST has been reset for orderType // The BST has not been modified since the most recent reset // The end of orderType iteration has not been reached // // Returns the element at the current position on this BST for orderType // and advances the value of the current position based on the orderType. } Solution import ch05.queues.*; import ch03.stacks.*; import support.BSTNode; public category BinarySearchTree> implements BSTInterface relevance the basis of this BST boolean found; // utilized by take away // for traversals protected LinkedUnbndQueue inOrderQueue; // queue of information protected LinkedUnbndQueue preOrderQueue; // queue of information protected LinkedUnbndQueue postOrderQueue; // queue of information public BinarySearchTree() // Creates Associate in Nursing empty BST object. public mathematician isEmpty() // Returns true if this BST is empty; otherwise, returns false. private int recSize(BSTNode tree)
  • 10. // Returns the quantity of components in tree. public int size() // Returns the quantity of components during this BST. public int size2() // Returns the quantity of components during this BST. } return count; } private mathematician recContains(T part, BSTNode tree) // Returns true if tree contains a part e such // e.compareTo(element) == zero; otherwise, returns false. { if (tree == null) return false; // part isn't found else if (element.compareTo(tree.getInfo()) < 0) return recContains(element, tree.getLeft()); // Search left subtree else if (element.compareTo(tree.getInfo()) > 0) return recContains(element, tree.getRight()); // Search right subtree else return true; // part is found } public mathematician contains (T element) // Returns true if this BST contains a part e such // e.compareTo(element) == zero; otherwise, returns false. private T recGet(T part, BSTNode tree) // Returns a part e from tree such e.compareTo(element) == zero; // if no such part exists, returns null. { if (tree == null) return null; // part isn't found else if (element.compareTo(tree.getInfo()) < 0) return recGet(element, tree.getLeft()); // get from left subtree else if (element.compareTo(tree.getInfo()) > 0) return recGet(element, tree.getRight()); // get from right subtree
  • 11. else return tree.getInfo(); // part is found } public T get(T element) // Returns a part e from this BST such e.compareTo(element) == 0; // if no such part exists, returns null. private BSTNode recAdd(T part, BSTNode tree) // Adds part to tree; tree retains its BST property. public void add (T element) // Adds part to the current BST. The tree retains its BST property. private T getPredecessor(BSTNode tree) // Returns the knowledge command within the right node in tree private BSTNode removeNode(BSTNode tree) // Removes the knowledge at the node documented by tree. // The user's information within the node documented by tree is not any // longer within the tree. If tree could be a leaf node or has solely // a non-null kid pointer, the node pointed to by tree is // removed; otherwise, the user's information is replaced by its // logical forerunner and therefore the predecessor's node is removed. } private BSTNode recRemove(T part, BSTNode tree) // Removes a part e from tree such e.compareTo(element) == 0 // and returns true; if no such part exists, returns false. return tree; } public mathematician take away (T element) // Removes a part e from this BST such e.compareTo(element) == 0 // and returns true; if no such part exists, returns false. private void inOrder(BSTNode tree) // Initializes inOrderQueue with tree components in inOrder order. } private void preOrder(BSTNode tree) // Initializes preOrderQueue with tree components in preOrder order. } private void postOrder(BSTNode tree) // Initializes postOrderQueue with tree components in postOrder order.
  • 12. } public int reset(int orderType) // Initializes current position for Associate in Nursing iteration through this BST // in orderType order. Returns current variety of nodes within the BST. else if (orderType == PREORDER) if (orderType == POSTORDER) return numNodes; } public T getNext (int orderType) // Preconditions: The BST isn't empty // The BST has been reset for orderType // The BST has not been changed since the foremost recent reset // the tip of orderType iteration has not been reached // // Returns the part at the present position on this BST for orderType // and advances the worth of the present position supported the orderType. come back null; } } BSTInterface: //---------------------------------------------------------------------------- // BSTInterface.java by Dale/Joyce/Weems Chapter eight // // Interface for a category that implements a binary search tree (BST). // // The trees square measure boundless and permit duplicate components, however don't enable null // components. As a general precondition, null components don't seem to be passed as // arguments to any of the strategies. // // The tree supports iteration through its components in INORDER, PREORDER, // and POSTORDER. //---------------------------------------------------------------------------- package ch08.trees; public interface BSTInterface>
  • 13. accustomed specify traversal order static final int INORDER = 1; static final int PREORDER = 2; static final int POSTORDER = 3; boolean isEmpty(); // Returns true if this BST is empty; otherwise, returns false. int size(); // Returns the quantity of components during this BST. boolean contains (T element); // Returns true if this BST contains a part e such // e.compareTo(element) == 0; otherwise, returns false. boolean take away (T element); // Removes a part e from this BST such e.compareTo(element) == 0 // and returns true; if no such part exists, returns false. T get(T element); // Returns a part e from this BST such e.compareTo(element) == 0; // if no such part exists, returns null. void add (T element); // Adds part to the current BST. The tree retains its BST property. int reset(int orderType); // Initializes current position for Associate in Nursing iteration through this BST // in orderType order. Returns current variety of nodes within the BST. T getNext (int orderType); // Preconditions: The BST isn't empty // The BST has been reset for orderType // The BST has not been changed since the foremost recent reset // the tip of orderType iteration has not been reached // // Returns the part at the present position on this BST for orderType // and advances the worth of the present position supported the orderType. }