Binary Trees and Binary Search Trees Flashcards

1
Q

how many children can a node have?

A

two children(left and right)

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

what is a node with no children called?

A

leaf node

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

a strict binary tree is what…

A

one that can have either 2 or 0 children

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

what makes a complete binary tree?

A

a tree where all levels are filled except possible the last and all nodes are as left as possible

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

what is the maximum number of nodes for every level i?

A

i = 2^i

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

what is the max # of nodes in a binary tree with height h

A

2^(h+1) -1

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

what is the perfect height of a binary tree given nodes?

A

h = log base 2 (n+1) -1

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

when do you have a balanced binary tree?

A

when the difference between the heights of the left and right subtrees for every node is not more than k(mostly 1)

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

what is the height of a binary tree?

A

the # of edges in longest path from root to a leaf

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

what is the height of an empty tree?

A

-1

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

what is the height of a tree with 1 node?

A

0

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

what is a binary search tree?

A

a binary tree in which for each node, the value of all the nodes in the left subtree is lesser or equal to and the value of all the nodes in the right subtree is greater.

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

how do you create the node class for a BST?

A

protected static class Node {
public E data; // The information stored in this node.
public Node left; //Reference to the left child
public Node right; //Reference to the right child.

public Node(E data) {
            this.data = data;
            left = null;
            right = null;
        }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

how do you initiate the root node of BT?

A

protected Node root;

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

how do you set the root node of a class to be null?

A
public BinaryTree(){ //BinaryTree is the name of the class
root=null;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how do you set the root node of to equal this node?

A
public BinaryTree(Node root){
this.root=root;
}
17
Q

how do you set the root node of a BT to have data in it?

A
public BinaryTree(E data, BinaryTree leftTree, BinaryTree rightTree){
root = new Node<> data;
if(leftTree.root != null){
root.left=leftTree.root;
} else{
root.left=null;
}
if(rightTree!=null){
root.right=rightTree.root;
} else{
rightTree=null;
}
}
18
Q

how do you return the left subtree of a given tree?

A
public BinaryTree getLeftSubTree(){
if(root != null && root.left!=null){
return new BinaryTree(root.left);
}
else{
return null;
}
}
19
Q

how do you return the right subtree of a given Binary Tree?

A
public BinaryTree getRightSubTree(){
if(root != null && root.left !=null){
return new BinaryTree(root.right);
}
else{
return null;
}
}
20
Q

how do you return the data stored in the root node?

A
public E getData(){
if(root!=null){
return root.data;
}
else{
return null;
}
}
21
Q

how do you find out if a subtree is a leaf node(has no children)?

A
public boolean isLeaf(){
return if(root!=null && (root.right==null && root.left=null); // will return true if the node is a leaf node,, false otherwise
22
Q

what is preorder traversal?

A

pre order traversal is iterating through a node by going through its root, then its subtrees recursively

23
Q

How do you perform a recursive pre-order traversal on a binary tree?

A

public void preOrderTraverse(){
preOrder(root);
}

public void preOrder(Node node){
if(node==null){
return;
}
else{
System.out.println(node.data + " ");
preOrder(node.left);
preorder(node.right);
}
}
24
Q

what is post order traversal?

A

processing through all the subtrees, then processing through the root node

25
Q

how do you perform a post-order traversal?

A

public void postOrderTraverse(){
postOrder(root);
}

public void postOrder(Node node){
if(root==null){
return;
}
else{
postOrder(node.left);
postOrder(node.right);
System.out.Println(" " + node.data(;
}
}
26
Q

what is in order traversal?

A

processes all nodes of a tree by recursively processing the left subtree, then processing the root, and finally the right subtree.

27
Q

how do you perform an in order traversal?

A

public void inOrderTraverse(){
inOrder(root);
}

public void inOrder(Node node){
if(node==null){
return;
}
else{
inOrder(node.left);
System.out.println(" " + node.data);
inOrder(node.right);
28
Q

how do you find the height of a BT?

A
public int treeHeight(){
return height(root);
}
private int height(Node node){
if(node==null){
return 0;
}
else{
return (1+max(height(node.left), height(node.right));
}
}
29
Q

how do you count nodes in a binary tree?

A
public int nodeCount(){
return nodeCount(root);
}
private int nodeCount(Node node){
if(node==null){
return 0;
}
else{
return (1+nodeCount(node.left) + nodeCount(node.right));
}
}
30
Q

how do you insert a node into a binary tree?

A
public void insertNode(E item){
Node node = new Node(item);
Node parent=null, current;
int randNum=0;
Random rand = new Random();
if(root=null){
root=null;
}
else{
current=root;
while(current!=null){
parent=current;
randNum=rand.nextInt(2); //generates a random number between 0 or 1
if (randNum==0){
current=current.left;
}
else{
current = current.right;
}
//to link a new node as a leaf node
if(randNum==0){
parent=parent.left;
}
else{
parent=parent.right;
}
}