BST.java Flashcards

(8 cards)

1
Q

list all the methods and attribute(s) of this class

A

*attributes:
-private Node root;
*public BinarySearxhTree() //constructor
*public void addNode(int n)
*public String toString();
*private void inOrderRecursuve(Node ptr, List<Integer> list)</Integer>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

attribute for this class

A

private Node root;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

public BinarySearchTree()

A

root = null;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

private Node addNode(int n)
(create a new node and list scenarios)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Scenario 1: Tree is empty

A

if (root == null) {
root = temp;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Scenario 2

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

public String toString()

A

List<Integer> list = new ArrayList<>;
inOrderRecursive(root,list);
return list.toString();</Integer>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

public void inOrderRecursive(Node ptr, List<Integer> list)</Integer>

A

if(ptr == null) {
return;
}
inOrderResursive(ptr.left, list);
list.add(ptr.n)
inOrderRecusrive(ptr.right, list)(

How well did you know this?
1
Not at all
2
3
4
5
Perfectly