BST.java Flashcards
(8 cards)
list all the methods and attribute(s) of this class
*attributes:
-private Node root;
*public BinarySearxhTree() //constructor
*public void addNode(int n)
*public String toString();
*private void inOrderRecursuve(Node ptr, List<Integer> list)</Integer>
attribute for this class
private Node root;
public BinarySearchTree()
root = null;
private Node addNode(int n)
(create a new node and list scenarios)
Node temp = new Node(n);
*Scenario 1: Tree is empty = add node to root
*Scenario 2: Tree is not empty = add node by traversing the tree
Scenario 1: Tree is empty
if (root == null) {
root = temp;
}
Scenario 2
else {
Node ptr = root;
while(true) {
if(temp.n < ptr.n) {
if(ptr.left != null)
ptr = ptr.left;
else
ptr.left = temp;
return;
}
else if (do the same for the right)
else
return;
public String toString()
List<Integer> list = new ArrayList<>;
inOrderRecursive(root,list);
return list.toString();</Integer>
public void inOrderRecursive(Node ptr, List<Integer> list)</Integer>
if(ptr == null) {
return;
}
inOrderResursive(ptr.left, list);
list.add(ptr.n)
inOrderRecusrive(ptr.right, list)(