Chapter 27 - Binary Search Trees Flashcards
In a binary tree, how is the length of a path measured?
The length of a path is the number of the edges (lines between nodes) in the path.
How is the depth of a node in a binary tree defined?
The depth of a node is the length of the path from the root to the node.
What is a leaf in the binary tree?
A node without children is called a leaf.
What is the height of a binary tree?
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.
What is the special property of a binary search tree?
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 can a binary tree be presented/implemented?
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 can you define a node class for a binary tree?
class TreeNode[E extends Comparable[E» {
- protected E element;
- protected TreeNode[E> left;
- protected TreeNode[E> right;
- public TreeNode(E e) {
- element = e;
- }
}
What is the algorithm for searching for an element in a BST?
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
What is the algorithm for inserting an element into a BST?
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.
What is meant by “tree traversal”?
Three traversal is the process of visiting each node in the tree exactly once.
Describe inorder traversal of a BST
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.
Describe postorder traversal of a BST
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.
Describe preorder traversal of a BST
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.
Describe depth-first traversal of a BST
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.
Describe breadth-first traversal of a BST
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.