Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views13 pages

PASSCO

An AVL Tree is a self-balancing binary search tree that maintains a balance factor of -1, 0, or 1 at every node, ensuring O(log n) time complexity for operations. It uses rotations to rebalance itself after insertions or deletions, making it efficient for applications like databases and file systems. The document also discusses various tree structures, including ternary trees, red-black trees, and binary heaps, highlighting their properties and use cases.

Uploaded by

motteywisdom0574
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

PASSCO

An AVL Tree is a self-balancing binary search tree that maintains a balance factor of -1, 0, or 1 at every node, ensuring O(log n) time complexity for operations. It uses rotations to rebalance itself after insertions or deletions, making it efficient for applications like databases and file systems. The document also discusses various tree structures, including ternary trees, red-black trees, and binary heaps, highlighting their properties and use cases.

Uploaded by

motteywisdom0574
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

AVL Tree (Adelson-Velsky and Landis Tree) — [15 Marks]

An AVL Tree is a self-balancing binary search tree (BST) where the difference
between heights of left and right subtrees for any node is at most one. If, after an
insertion or deletion, the balance factor (height of left subtree - height of right
subtree) becomes less than -1 or more than 1, the tree performs rotations to
rebalance itself.

Key Properties:

 Binary Search Tree rules apply (left < root < right).
 Maintains a balance factor of -1, 0, or 1 at every node.
 Height-balanced: Guarantees O(log n) time complexity for search, insertion,
and deletion.
 Uses 4 types of rotations to maintain balance:
1. Left Rotation
2. Right Rotation
3. Left-Right Rotation
4. Right-Left Rotation

Advantages:

 Faster lookup times compared to unbalanced BSTs.


 Keeps operations efficient and predictable in time complexity.

Use Cases:

 Databases and file systems (e.g., in-memory indexing).


 Applications requiring frequent insertions/deletions.

ii. AVL Tree — Insertion and Deletion Example — [15 Marks]

Let’s construct an AVL tree by inserting the following values in order:

Initial Insertions: 30, 20, 40, 10

Then we will:

 Insert: 5
 Delete: 40
Step-by-Step:

Initial Tree (after inserting 30, 20, 40, 10)

markdown
CopyEdit
30
/ \
20 40
/
10

Insert 5

Inserting 5 under 10 makes the left subtree too tall (balance factor at 30 becomes
2), so we perform a Right Rotation at 30:

mathematica
CopyEdit
Before Rotation:

30
/ \
20 40
/
10
/
5

After Right Rotation at 30:

20
/ \
10 30
/ \
5 40

Delete 40

Deleting 40 causes no imbalance, so just remove it:


markdown
CopyEdit
20
/ \
10 30
/
5

This tree remains balanced, and no rotations are needed.

Let me know if you'd like a diagram image of these AVL tree transformations—I
can create it for you visually to include in your slides or coursework.

Sure! Here's a simplified version of a singly linked list class in C++ — clean and
easy to understand, especially good for beginners or short-answer questions:

cpp
CopyEdit
#include <iostream>
using namespace std;

// Node structure
class Node {
public:
int data;
Node* next;
};

// Singly Linked List class


class SinglyLinkedList {
public:
Node* head;

SinglyLinkedList() {
head = nullptr;
}

// Function to insert at the end


void insert(int val) {
Node* newNode = new Node();
newNode->data = val;
newNode->next = nullptr;

if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr)
temp = temp->next;
temp->next = newNode;
}
}

// Function to display the list


void display() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
};

// Main function to test


int main() {
SinglyLinkedList list;
list.insert(5);
list.insert(15);
list.insert(25);
list.display(); // Output: 5 -> 15 -> 25 -> NULL

return 0;
}
What's Included:

 A simple Node class.


 SinglyLinkedList class with basic insert and display functions.
 main() function to test the list.

Let me know if you want to add more operations like delete or search in the list!

4o

AVL Trees

Introduction:

AVL (Adelson-Velsky and Landis) trees are self-balancing binary search trees
(BSTs).

Properties:

 Height-balanced: Balance factor (left height - right height) must be -1, 0, or


1.

 Rotations (single or double) are used to maintain balance.

Insertion/Deletion:

 Afterinsertion or deletion, the tree may need to be rebalanced using


rotations.
Applications:

 Databases, memory-intensive systems where fast retrieval is required.

2. Ternary Trees

Introduction:

A ternary tree is a tree where each node can have up to three children.

Properties:

 Children typically labeled: left, middle, and right.

 Useful for storing associative data.

Insertion/Deletion:

 Logic similar to BST, but adds a middle path for equal values (often used in
string trees).

Applications:

 Auto-completion engines, spell checkers, and dictionaries.

3. Red-Black Trees

Introduction:
A type of self-balancing BST with color-coded nodes (red/black) to ensure
balance.

Properties:

 Every node is either red or black.

 No two red nodes in a row.

 Equal black height on all paths.

Insertion/Deletion:

 Uses rotations and color flips to maintain properties.

Applications:

 Used in Java’s TreeMap, Linux process scheduler, and other performance-


critical systems.

4. Splay Trees

Introduction:

A self-adjusting BST that brings the most recently accessed node to the root.

Properties:

 No explicit balance; adapts based on access patterns.


 Frequently accessed nodes become quicker to reach.

Insertion/Deletion:

 Splaying (rotating a node to the root) occurs after operations.

Applications:

 Caching, implementing access frequency optimization.

5. 2-3-4 Trees

Introduction:

A multiway search tree where nodes can have 2, 3, or 4 children.

Properties:

 Perfectly balanced (all leaves at the same level).

 Nodes hold multiple keys.

Insertion/Deletion:

 Insertion may cause node splitting.

 No need for rebalancing after deletion.

Applications:
 Filesystems, databases (precursor to B-trees).

6. B and B+ Trees

Introduction:

Generalized balanced trees optimized for storage systems (especially disks).

Properties:

 B-trees store keys and values in internal and leaf nodes.

 B+ trees store data only in leaf nodes; internal nodes act as guides.

Insertion/Deletion:

 Maintains order and balance via node splitting and merging.

Applications:

 Databases, filesystem indexing (e.g., NTFS, MySQL, Oracle).

7. Scapegoat Tree

Introduction:

A self-balancing BST without storing balance info; uses rebuilding instead.


Properties:

 Maintains balance by limiting tree height.

 Rebuilds entire unbalanced subtrees (not gradual like AVL or RBT).

Insertion/Deletion:

 Insertion may trigger subtree rebuilding.

 Deletion is lazy—rebuilding occurs if imbalance is detected.

Applications:

 Theoretical use, scenarios with unpredictable insert/delete patterns.

8. Treap

Introduction:

A combination of a binary search tree and a heap (BST + heap = “Treap”).

Properties:

 Each node has a key (BST order) and priority (heap order).

 Probabilistic balancing using random priorities.

Insertion/Deletion:

 Follow BST insert/delete with heap-like rotations based on priority.


Applications:

 Randomized algorithms, fast insert/delete in probabilistic settings.

9. Tango Tree

Introduction:

A cache-oblivious BST that adapts to access sequences to improve search


efficiency.

Properties:

 Designed to be competitive with offline optimal BSTs.

 Partitions BST into preferred paths.

Insertion/Deletion:

 Maintains preferred paths dynamically.

 More theoretical than practical.

Applications:

 Mostly used in theoretical computer science and research.

Binary Heap and Heap Order Property (10 marks)

1. Concept of a Binary Heap (4 marks)


A binary heap is a complete binary tree that satisfies the heap order property. It is commonly used to
implement priority queues due to its efficient operations. There are two main types of binary heaps:

 Min-Heap: The parent node is always less than or equal to its children.
 Max-Heap: The parent node is always greater than or equal to its children.

Being a complete binary tree means all levels are fully filled except possibly the last, which is filled from
left to right.

Binary heaps are usually implemented using arrays for efficiency, with parent and child relationships
easily derived:

 Parent at index i has children at indices 2i + 1 and 2i + 2.

2. Heap Order Property (2 marks)

The heap order property ensures that for every node n:

 In a min-heap, n.key ≤ n.left.key and n.key ≤ n.right.key


 In a max-heap, n.key ≥ n.left.key and n.key ≥ n.right.key

This property enables quick access to the minimum or maximum element, which is always at the root.

Use of Binary Heap for Priority Queue (4 marks)

A **

4o

You said:

explain in simple terms for me to understand

ChatGPT said:

Of course, Lydia! Let me explain it in a simpler way that still covers everything you'd need for a 10-mark
answer.

Binary Heap and Heap Order Property (10 marks)

1. What is a Binary Heap? (4 marks)


A binary heap is a special kind of binary tree used mainly to manage data quickly, especially when you
want to always access the smallest or largest value first.

 It's a complete tree, meaning all levels are fully filled except maybe the last one, which fills from
left to right.
 There are two types:
o Min-Heap: The smallest number is always at the top (the root).
o Max-Heap: The largest number is always at the top.

It’s often stored in an array because we can easily find parent and child nodes using index formulas:

 For a node at position i:


o Left child = 2i + 1
o Right child = 2i + 2
o Parent = (i - 1) // 2

2. What is the Heap Order Property? (3 marks)

The heap order property means:

 In a min-heap, every parent is smaller than or equal to its children.


 In a max-heap, every parent is larger than or equal to its children.

This property ensures that the root (top) always holds the most important value—either the smallest or
largest depending on the heap type.

3. Use of Binary Heap in a Priority Queue (3 marks)

A priority queue is like a normal queue, but items come out in order of priority, not just first-in-first-out.

 A binary heap is perfect for this because:


o We can insert a new item quickly (in O(log n) time).
o We can remove the highest (or lowest) priority item quickly (also O(log n)).
 This makes it efficient and fast, especially compared to sorting the whole list every time.

You might also like