No history yet

Non Linear Structures

Beyond Sequential Data

Arrays are predictable. You store items one after another in a neat, contiguous block of memory. This sequential layout makes it fast to access any element if you know its index, but it comes at a cost. Inserting or deleting an item in the middle of a large array requires shifting every subsequent element, which can be painfully slow.

To overcome this, we can break free from sequential memory and organize data based on relationships. This leads us to non-linear structures, where elements are connected via links, or , rather than being physically next to each other.

The Linked List Chain

A linked list is a chain of nodes. Each node contains two things: the data it's storing and a pointer to the next node in the sequence. The list has a starting point, called the head, and the last node points to null, signaling the end of the chain.

Because nodes don't have to be next to each other in memory, adding a new element is simple. We just create a new node anywhere in memory and update the previous node's pointer to link to it. No shifting required.

The simplest version is a singly linked list, where traversal is one-way. A more flexible version is the doubly linked list, where each node has a pointer to the next node and the previous node. This extra pointer uses more memory but allows you to move backward and forward through the list. This is perfect for features like a web browser's back button or an editor's undo/redo functionality.

The main trade-off with linked lists is search time. To find an element, you must start at the head and visit each node one by one until you find what you're looking for. This is an O(n)O(n) operation, meaning the search time grows linearly with the number of items. For insertions and deletions, linked lists shine with O(1)O(1) performance, provided you already know where the change needs to happen.

Trees for Faster Searching

What if we need fast insertions and fast searches? This is where tree structures come in. A Binary Search Tree (BST) is a node-based structure that imposes a strict ordering rule.

Lesson image

For any given node, all values in its left subtree are less than the node's value, and all values in its right subtree are greater.

This rule makes searching incredibly efficient. Instead of checking every element, you start at the root and make a simple decision at each node: go left if your target is smaller, or go right if it's larger. With each step, you discard roughly half of the remaining nodes. This reduces the average search time from O(n)O(n) to O(logn)O(\log n), a massive improvement for large datasets.

Walking Through a Tree

Unlike linear structures, there are multiple ways to visit, or traverse, every node in a tree. The three most common recursive methods are defined by when we process the current node's data relative to its children:

TraversalOrder of OperationsCommon Use Case
In-order1. Visit left subtree
2. Process current node
3. Visit right subtree
Retrieves data in sorted order.
Pre-order1. Process current node
2. Visit left subtree
3. Visit right subtree
Creating a copy of the tree.
Post-order1. Visit left subtree
2. Visit right subtree
3. Process current node
Deleting the tree from memory.

Each traversal method serves a different purpose. Visiting nodes in-order on a BST will always give you the elements in ascending sorted order. Post-order is useful for deleting a tree because you can safely delete a parent node only after its children have been deleted.

Let's test your understanding of these non-linear data structures.

Quiz Questions 1/6

What is the primary drawback of using an array when you need to frequently insert or delete elements from the middle of the collection?

Quiz Questions 2/6

You are implementing the history feature in a web browser, allowing users to go backward and forward through visited pages. Which data structure is most suitable for this task?

Choosing the right data structure involves understanding these performance trade-offs. Linked lists offer fast insertions at the cost of slow searches, while BSTs provide a balanced approach for dynamic datasets.