Binary Search Trees Flashcards

1
Q

In a binary tree, how is the length of a path measured?

A

The length of a path is the number of the edges (lines between nodes) in the path.

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

How is the depth of a node in a binary tree defined?

A

The depth of a node is the length of the path from the root to the node.

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

What is a leaf in the binary tree?

A

A node without children is called a leaf.

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

What is the height of a binary tree?

A

The height of an empty tree is 0. The height of a nonempty tree is the length of the path from the root node to its furthest leaf + 1. Therefore, height of a tree is either 0 or larger than 1.

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

What is the special property of a binary search tree?

A

A binary search tree (BST) has the property that for every node in the tree, the value of any node in its left subtree is less than the value of the node, and the value of any node in its right subtree is greater than the value of the node.

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

How can a binary tree be presented/implemented?

A

A binary heap can be represented using a single array. But a binary tree can also be represented using a set of nodes. Each node contains a value and two links named “left” and “right” that reference the left child and right child, respectively.

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

How can you define a node class for a binary tree?

A

class TreeNode[E extends Comparable[E» {

  • protected E element;
  • protected TreeNode[E> left;
  • protected TreeNode[E> right;
  • public TreeNode(E e) {
  • element = e;
  • }
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the algorithm for searching for an element in a BST?

A

You start from the root and scan down from it until a match is found or you arrive at an empty subtree:
Let “current” point to the root. Repeat following steps until current is null or the element matches current.element:
– If element is less than current.element, assign current.left to current
– Else if element is greater than current.element, assign current.right to current
– Else if element is equal to current.element, return true
– Else, current is null, the subtree is empty and the element is not in the tree

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

What is the algorithm for inserting an element into a BST?

A

If the tree is empty, create a root node with the new element. Otherwise, locate the parent node for the new element node. Create a new node for the element and link this node to its parent node. If the new element is less than the parent element, the node for the new element will be the left child of the parent. If the new element is greater than the parent element, the node for the new element will be the right child of the parent.
If the element is equal to a node, the element is not inserted into the list, because the BST does not keep duplicate elements.

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

What is meant by “tree traversal”?

A

Three traversal is the process of visiting each node in the tree exactly once.

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

Describe inorder traversal of a BST

A

With inorder traversal, the left subtree of the current node is visited first recursively, then the current node, and finally the right subtree of the current node recursively. The inorder traversal displays all the nodes in a BST in increasing order.

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

Describe postorder traversal of a BST

A

With postorder traversal, the left subtree of the current node is visited recursively first, then recursively the right subtree of the current node, and finally the current node itself. An application of postorder is to find the size of the directory in a file system.

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

Describe preorder traversal of a BST

A

With preorder traversal, the current node is visited first, then recursively the left subtree of the current node, and finally the right subtree of the current node recursively. An application of preorder is to print a structured document.

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

Describe depth-first traversal of a BST

A

Depth-first traversal is the same as preorder traversal. With preorder traversal, the current node is visited first, then recursively the left subtree of the current node, and finally the right subtree of the current node recursively. An application of preorder is to print a structured document.

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

Describe breadth-first traversal of a BST

A

With breadth-first traversal, the nodes are visited level by level. First the root is visited, then all the children of the root from left to right, then the grandchildren of the root from left to right, and so on.

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

If a set of elements is inserted into a BST in two different orders, will the two corresponding BSTs look the same? Will the inorder traversal be the same? Will the postorder traversal be the same? Will the preorder traversal be the same?

A

They will not look the same. The inorder traversal will be the same. The postorder and preorder traversal will not be the same.

17
Q

What is the time complexity of inserting an element into a BST?

A

O(n)

18
Q

To delete an element from a binary search tree, you need to first locate the node that contains the element and also its parent node. Let “current” point to the node that contains the element to be deleted in the binary search tree and “parent” point to the parent of the “current” node. How do you delete “current” if “current” has no left child?

A

The current node may be a left child or a right child of the parent node. There are two cases to consider. Case 1 is when current node does not have a left child:
In this case. simply connect the parent with the right child of the current node,
If the current node is a leaf, the parent is connected with null.

19
Q

To delete an element from a binary search tree, you need to first locate the node that contains the element and also its parent node. Let “current” point to the node that contains the element to be deleted in the binary search tree and “parent” point to the parent of the “current” node.
How do you delete “current” if “current” has no right child?

A

The current node may be a left child or a right child of the parent node. There are two cases to consider. Case 2 is when the current node has a left child:
Let rightMost point to the node that contains the largest element in the left subtree of the current node and parentOfRightMost point to the parent node of the rightMost node. Note that the rightMost node cannot have a right child but may have a left child. Replace the element value in the current node with the one in the rightMost node, connect the parentOfRightMost node with the left child of the rightMost node, and delete the rightMost node.
NOTE: If the left child of “current” does not have a right child, current.left points to the large element in the left subtree of current. In this case, rightMost is current.left and parentOfRightMost is current. You have to take care of this special case to reconnect the right child of rightMost with parentOfRightMost.

20
Q

What is an iterator?

A

An iterator is an object that provides a uniform way for traversing the elements in a container such as a list, set, binary tree etc.

21
Q

What is the benefit of being a subtype of Iterable[E>?

A

Being a subtype of Iterable, the elements of the container can be traversed using a for-each loop.

22
Q

What is Huffman Coding?

A

Huffman coding compresses data by using fewer bits to encode characters that occur more frequently in a text. The codes for the characters are constructed based on the occurrence of the characters in the text using a binary tree, called the Huffman coding tree.

23
Q

How do you build a Huffman coding tree?

A

To construct a Huffman coding tree, use an algorithm as follows:

  1. Begin with a forest of trees. Each tree contains a node for a character. The weight of the node is the frequency of the character in the text.
  2. Repeat the following action to combine trees until there is only one tree: Choose two trees with the smallest weight and create a new node as their parent. The weight of the new tree is the sum of the weight of the subtrees.
  3. For each interior node, assign its left edge a value 0 and right edge a value 1. All leaf nodes represent characters in the text.
24
Q

What is a greedy algorithm?

A

A greedy algorithm is often used in solving optimization problems. The algorithm makes the choice that is optimal locally in the hope that this choice will lead to a globally optimal solution.

25
Q

A ____ (with no duplicate elements) has the property that for every node in the tree the value of any node in its left subtree is less than the value of the node and the value of any node in its right subtree is greater than the value of the node.

A. binary tree
B. binary search tree
C. list
D. linked list

A

B. binary search tree

26
Q

The __ of a path is the number of the edges in the path.

A. length
B. depth
C. height
D. degree

A

A. length

27
Q

The ___ of a node is the length of the path from the root to the node.

A. length
B. depth
C. height
D. degree

A

B. depth

28
Q

The ___ of a nonempty tree is the length of the path from the root node to its furthest leaf + 1.

A. length
B. depth
C. height
D. degree

A

C. height

29
Q

The ___ is to visit the left subtree of the current node first, then the current node itself, and finally the right subtree of the current node.

A. inorder traversal
B. preorder traversal
C. postorder traversal
D. breadth-first traversal

A

A. inorder traversal

30
Q

The __ is to visit the current node first, then the left subtree of the current node, and finally the right subtree of the current node.

A. inorder traversal
B. preorder traversal
C. postorder traversal
D. breadth-first traversal

A

B. preorder traversal

31
Q

The __ is to visit the left subtree of the current node first, then the right subtree of the current node, and finally the current node itself.

A. inorder traversal
B. preorder traversal
C. postorder traversal
D. breadth-first traversal

A

C. postorder traversal

32
Q

In the implementation of BST, which of the following are true?

A. Node is defined as an inner class inside BST.
B. Node is defined as a static inner class inside BST because it does not reference any instance data fields in BST.
C. Node has a property named left that links to the left subtree and a property named right that links to the right subtree and a property named right
D. BST contains a property named root. If tree is empty, root is null.

A

All are true

33
Q

The time complexity for searching an element in a binary search tree is ___.

A. O(n)
B. O(logn)
C. O(nlogn)
D. O(n^2)

A

Worst time is O(n), average time is O(logn)

34
Q

A Huffman tree is constructed using the ___ algorithm.

A. dynamic programming
B. divide-and-conquer
C. greedy
D. back-tracking

A

C. greedy

35
Q

Analyze the following code:

LinkedList[TreeNode[E]] nodeList = new java.util.LinkedList();

for (TreeNode[E> node: nodeList)
{
-   nodeList.add(node.left);
-   nodeList.add(node.right);
-   nodeList.remove(node);
}
A

You can’t modify a Collection while iterating over it, except for Iterator.remove(). You can also add using ListIterator.add(E e), but this will only insert the item after the current item – you can’t choose where to insert it, e.g. the end of the list.

Your best option is to create another list and add stuff to that as you iterate, e.g. alternating between two lists like in the breadthFirstTraversal method in the BST class.

36
Q

What is a full binary tree?

A

A full binary tree (sometimes proper binary tree or 2-tree) is a tree in which every node other than the leaves has two children.
Note that this does NOT mean that all the leaves are at the same level.
If the binary tree is a full binary tree, and all the leaves are at the same level, it is called a perfect binary tree.

37
Q

What is a perfect binary tree?

A

A perfect binary tree is a full binary tree where all the leaves are at the same level.
(A full binary tree is a tree in which every node other than the leaves has two children),
The number of nodes in a perfect binary tree is equal to 2^(number of levels) - 1. E.g., a perfect binary tree with 4 levels has 2^4 - 1 = 15 nodes