Essential Data Structure Operations and Java Implementations

Classified in Computers

Written on in English with a size of 3.95 KB

Inserting the First Element into a Tree Structure

Method to add the first element (root) to a Tree Structure:

public void insertFirstNode(Object item)
{
    // If the tree is empty, the new node becomes the root of the tree
    if (theRoot == null)
    {
        Node theNewNode = new Node(item);
        theRoot = theNewNode;
    }
}

Deleting an Element from a Linked List

Method to remove an element at a specific index in a Linked List:

public void remove(int index) {
    // Special case: removing at the head of the list
    if (index == 1) {
        head = head.getNext();
    } else {
        // Find the previous and current node
        setCurrent(index);
        prev.setNext(curr.getNext());
    }
    size = size - 1;
}

Steps for Inserting an Element into a Linked List

The following steps detail the process for inserting a new element into a Linked List (assuming a Doubly Linked List structure for steps 3–5):

  1. Set curr to the node at the position where you want to insert the new element.
  2. Create a newNode. Initially, its previous and next links are null.
  3. Copy the next link from the prev node (the node preceding the insertion point) into the next link of the newNode. Similarly, copy the previous link from the curr node into the previous link of the newNode.
  4. Change the next link in the prev node to point towards the newNode.
  5. Change the previous link in the curr node to point towards the newNode.

Single vs. Double Linked Lists: Key Differences

A Double Linked List (DLL) offers several advantages over a Single Linked List (SLL):

  • Traversal: DLL allows forward and backward traversal of the list, whereas SLL allows only forward traversal.
  • Access: DLL provides easy access to both the predecessor (using the prev link) and the successor (using the next link) of the current element. SLL allows access only to the next element.
  • Pointers: DLL typically uses both a head pointer and a last pointer (or tail pointer) to reference the first and last elements, respectively. SLL uses only a head pointer.

Recursive Method to Calculate Binary Tree Height

The following recursive method calculates the height of a binary tree, where height is defined as the maximum number of edges from the root to a leaf node (or 0 if the node is external/a leaf):

public int height (BTNode v) {
    if (v.isExternal()) {
        return 0;
    } else {
        return 1 + Math.max(height(v.getLeftChild()), height(v.getRightChild()));
    }
}

Understanding Stack Data Structure: LIFO Principle

The Last-In, First-Out (LIFO) principle describes the mechanism by which objects are inserted into and removed from a Stack data structure.

In a Stack:

  • Elements are inserted at the top (the Push operation).
  • Elements are removed from the top (the Pop operation).

This structure ensures that the element that was inserted last is the only element available to be removed first.

Searching an ArrayList for a Specific Element (Dublin)

Code snippet demonstrating how to search an ArrayList for the string "Dublin" and retrieve its index:

int index;
ArrayList list = new ArrayList();

if (list.contains("Dublin")) {
    index = list.indexOf("Dublin");
    System.out.println("The bus to Dublin was discovered at the index: " + index);
} else {
    System.out.println("The Dublin bus wasn't found.");
}

Related entries: