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;
}