DataStructure_Midterms_Reviewer
DataStructure_Midterms_Reviewer
// Additional methods or functionalities can be added here Understanding the Fundamentals of the Dynamic Stack Data
} Structure
Operator Precedence
• Operators:
o + for addition
o - for subtraction
Popular/Conventional Terms Associated with the Queue
o * for multiplication
Data Structure
o / for division
o $ for exponentiation
Conventional Terms
• Precedence Levels (from highest to lowest):
• Front: Reference to the first (oldest) element in the
o Exponentiation
queue.
o Multiplication / Division
• Rear: Reference to the last (newest) element in the
o Addition / Subtraction
queue.
General Rule • Head: Alternative term for the front of the queue.
• Consecutive operators with the same precedence are • Tail: Alternative term for the rear of the queue.
applied from left to right, except for exponentiation, • Enqueue: Operation for adding an element to the
which is applied from right to left. queue (at the tail/rear).
• Dequeue: Operation for removing an element from
Exponentiation in Java the queue (from the head/front).
• Java does not have a built-in exponentiation
operator. Instead, exponentiation is implemented as
a method.
Template for the Node of a Linked Representation of the
Queue
QueueNode Class Definition
Examples of Infix to Postfix Conversion
public class QueueNode<T> {
Example 1 T info;
• Infix: A + B * C QueueNode<T> link;
o Conversion Steps:
public QueueNode(T info, QueueNode<T> link) {
▪ A + B * C → A + BC* → ABC*+
o Postfix: ABC*+ this.info = info;
Example 2 this.link = link;
}
• Infix: (A + B) * C
}
o Conversion Steps:
▪ (A + B) * C → AB+ * C → AB+C*
o Postfix: AB+C*
Linked Representation of the Queue Topological Sorting – An Application
Characteristics Overview
• A queue is empty if both front and rear are null. • Input: Partially ordered elements.
• Overflow occurs only when the program runs out of • Output: A list of elements where no element is listed
memory. with its predecessor that has not yet appeared in the
output.
Application: Simulation
• Simulation: A technique where one system models
the behavior of another, often used when real-world
experimentation is too costly or hazardous.
Queuing System
• Consists of servers and queues of objects waiting for
service.
o Server: Provides the service (e.g., bank
teller, grocery cashier).
o Customer: Receives the service.
o Service Time: Duration required to serve a Binary Tree
customer (also known as transaction time). Definition
• A binary tree is a collection of nodes that is either:
o Empty, or
o Consists of:
Lesson 3: Binary Tree ▪ A root node.
Overview of Binary Trees ▪ Two binary trees:
• Definition and Related Concepts: A binary tree is a ▪ Left Subtree: A binary tree
hierarchical structure with at most two children per on the left side.
node. ▪ Right Subtree: A binary
• Properties: tree on the right side.
o Each node contains a value and references Characteristics
to its children. • The left subtree itself is a binary tree.
• The right subtree itself is also a binary tree.
Types of Binary Trees
• Various classifications based on structure and
properties.
Representation
• Can be represented using linked structures or arrays.
Understanding Graphs
• A graph G=(V,E)G = (V, E)G=(V,E) consists of:
o A finite, non-empty set of vertices VVV.
o A set of edges EEE connecting the vertices.
Binary Tree
Key Concepts
5 Examples of Graphs Definitions
• Level of a Node: The distance of the node from the
root node.
• Height (or Depth) of a Tree: The level of the
bottommost nodes; it is the length of the longest
path from the root to any leaf node.
• External Node: A node without children (leaf node);
all other nodes are considered internal.
• Proper Binary Tree: A binary tree in which each node
has either zero or two children.
• Right (Left) Skewed Binary Tree: A tree in which Complete Binary Tree
every node has no left (right) subtrees, resulting in a • Definition: A complete binary tree is formed when
tree with the greatest depth. zero or more nodes are deleted from a full binary
tree in reverse-level order.
o This means that all levels of the tree are
fully filled except possibly for the last level,
which is filled from left to right.
Pre-Order Traversal
Full Binary Tree Steps for Pre-Order Traversal:
• Definition: A full binary tree is a strictly binary tree 1. Base Case: If the binary tree is empty, do nothing
where: (the traversal is complete).
o All terminal (external) nodes lie at the 2. Visit the Root: Process the root node.
bottom-most level. 3. Traverse the Left Subtree: Perform a pre-order
o It has the maximum number of nodes for a traversal on the left subtree.
given depth. 4. Traverse the Right Subtree: Perform a pre-order
traversal on the right subtree.
}
Inorder Traversal }
Steps for Inorder Traversal:
1. Base Case: If the binary tree is empty, do nothing
(the traversal is complete). 2. Tree Class
2. Traverse the Left Subtree: Perform an inorder • Description: A template for a binary tree (BST) that
traversal on the left subtree. involves instances of TreeNode.
3. Visit the Root: Process the root node.
4. Traverse the Right Subtree: Perform an inorder public class Tree<T> {
traversal on the right subtree. private TreeNode<T> root;
public Tree() {
this.root = null;
}
3. TreeTest Class
• Description: A main class that involves an instance of
the Tree class.
Classes
1. TreeNode Class
• Description: A template for a node in a binary tree
(Binary Search Tree).
/**
* Constructor to initialize the node with given data,
* and set left and right children to null.
*/
public TreeNode(T data) {
this.data = data; // Fix constructor to set instance variable
leftNode = null;
rightNode = null;
}
/**
* Setter method for the leftNode field.
*/
public void setLeft(TreeNode<T> left) { BST Operations and AVL Trees
leftNode = left; Link Representation of BST
} • In a Binary Search Tree (BST):
o If the BST is empty, the left and right
/** pointers of bstHead point to itself.
* Setter method for the rightNode field. o Otherwise, the right pointer points to the
*/ root of the tree.
public void setRight(TreeNode<T> right) {
rightNode = right;
}
BST Operations
/** Searching for a Key
* Setter method for the data field. When searching for a value kkk in a BST, three conditions are
*/ possible:
public void setData(T d) { • Successful Search: kkk equals the value at the
data = d; current node.
} • Search Left Subtree: If k<k <k< value at the current
node.
/**
• Search Right Subtree: If k>k >k> value at the current
* Getter method for the leftNode field.
node.
*/
public TreeNode<T> getLeftNode() {
Deletion of an Existing Key
return leftNode;
When deleting a key from a BST, the BST property must be
}
maintained. There are two main cases for deletion:
/**
Case 1: Node ddd is an External Node (Leaf)
* Getter method for the rightNode field.
• Action: Update the child pointer of the parent ppp:
*/
o If ddd is a left child, set p.left=nullp.left =
public TreeNode<T> getRightNode() {
nullp.left=null.
return rightNode;
o If ddd is a right child, set p.right=nullp.right
}
= nullp.right=null.
/**
Case 2: Node ddd is Internal
* Getter method for the data field.
• The deletion process for an internal node is more
*/
complex and involves restructuring the tree to
public T getData() {
maintain the BST properties.
return data;
}
/**
Time Complexity of BST Operations
* Returns a string representation of the data.
• Average Case: O(log2n)O(\log_2 n)O(log2n) for
*/
search operations.
public String toString() {
• Worst Case: O(n)O(n)O(n) when the tree becomes
return data.toString();
unbalanced (e.g., resembling a linked list).
}
}
Balanced Binary Search Trees 2. Update the Balance Factor: Starting from the newly
inserted node, propagate the height increase
Importance of Balancing message up the tree to the root:
To ensure that searching takes O(log2n)O(\log_2 n)O(log2n), o If the message comes from the left subtree,
balance must be maintained in a BST. increment the balance by +1.
o If it comes from the right subtree,
Balance of a Node decrement the balance by -1.
• Defined as the height of the left subtree minus the
height of the right subtree. 3. Check Balance Factor: If the resulting balance factor
becomes +2 or -2, perform the appropriate rotation
(as described above).
AVL TREES
Left Rotation
1. Make the right child of the root the new root.
2. The former root becomes the left child of the new
root.
3. The left child of the former right child becomes the
new right child of the former root.
Right Rotation
1. Make the left child of the root the new root.
2. The former root becomes the right child of the new
root.