Skip to content

Akshayy67/avl-tree-visualizer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

AVL Tree Visualizer

An interactive web application that visualizes AVL tree operations with step-by-step animations, showing every rebalancing step (LL, LR, RL, RR) with smooth animations, height/balance factors, and side-by-side code tracing.

๐ŸŒŸ Features

Core Functionality

  • Interactive Tree Operations: Insert, delete, and search operations with real-time visualization
  • Step-by-Step Animation: Watch every step of AVL tree operations with smooth 60fps animations
  • Rotation Visualization: Clear visualization of all four rotation types (LL, LR, RL, RR)
  • Balance Factor Display: Real-time display of height and balance factors for each node
  • Code Tracing: Side-by-side pseudocode with synchronized highlighting

Advanced Features

  • Batch Operations: Insert or delete multiple values at once
  • Predefined Scenarios: 20+ built-in test scenarios demonstrating different rotation cases
  • Undo/Redo: Full operation history with undo/redo functionality
  • Import/Export: Save and load tree states as JSON
  • Dark/Light Theme: Toggle between themes with smooth transitions
  • Keyboard Shortcuts: Efficient navigation with keyboard controls

Educational Tools

  • Algorithm Complexity: Real-time complexity analysis and performance metrics
  • Tree Statistics: Node count, height, balance status, and operation history
  • Color Legend: Visual guide for different node states and operations
  • Rotation Guide: Interactive diagrams explaining each rotation type

๐Ÿš€ Quick Start

Prerequisites

  • Node.js 18+
  • npm or yarn

Installation

# Clone the repository
git clone <repository-url>
cd avl-tree-visualizer

# Install dependencies
npm install

# Start development server
npm run dev

Open http://localhost:5173 in your browser.

Building for Production

# Build the application
npm run build

# Preview the build
npm run preview

๐ŸŽฎ Usage

Basic Operations

  1. Insert: Enter a number and click "Insert" or press I for quick insert
  2. Delete: Enter a number and click "Delete" or press D for quick delete
  3. Search: Enter a number and click "Search" or press S for quick search

Animation Controls

  • Play/Pause: Space bar or play button
  • Step Forward/Backward: Arrow keys or step buttons
  • Speed Control: Adjust animation speed from 0.25x to 2x
  • Frame Scrubber: Jump to any frame in the animation

Batch Operations

Switch to the "Batch Operations" tab to:

  • Insert multiple values: 10, 20, 30, 40
  • Delete multiple values from existing tree
  • Test complex scenarios quickly

Predefined Scenarios

Switch to the "Scenarios" tab to try:

  • Rotation Demos: LL, LR, RL, RR rotation examples
  • Pattern Tests: Sorted input, Fibonacci sequence, zigzag patterns
  • Stress Tests: Large datasets and complex operations

๐Ÿ—๏ธ Architecture

Project Structure

src/
โ”œโ”€โ”€ avl/                    # AVL tree core logic
โ”‚   โ”œโ”€โ”€ avl.types.ts       # TypeScript type definitions
โ”‚   โ”œโ”€โ”€ avl.core.ts        # Pure AVL tree algorithms
โ”‚   โ”œโ”€โ”€ avl.generator.ts   # Animation frame generation
โ”‚   โ”œโ”€โ”€ avl.layout.ts      # Tree layout and positioning
โ”‚   โ”œโ”€โ”€ scenarios.ts       # Predefined test scenarios
โ”‚   โ””โ”€โ”€ __tests__/         # Unit tests
โ”œโ”€โ”€ store/
โ”‚   โ””โ”€โ”€ useAvlStore.ts     # Zustand state management
โ”œโ”€โ”€ components/
โ”‚   โ”œโ”€โ”€ Canvas.tsx         # SVG tree renderer
โ”‚   โ”œโ”€โ”€ Controls.tsx       # Operation controls
โ”‚   โ”œโ”€โ”€ CodeTrace.tsx      # Algorithm pseudocode panel
โ”‚   โ”œโ”€โ”€ Metrics.tsx        # Tree statistics
โ”‚   โ””โ”€โ”€ Legend.tsx         # Color and rotation legend
โ””โ”€โ”€ App.tsx                # Main application component

Key Technologies

  • Frontend: React 18 + TypeScript
  • State Management: Zustand
  • Visualization: D3.js + SVG
  • Styling: Tailwind CSS
  • Build Tool: Vite
  • Testing: Vitest + jsdom

๐Ÿงฎ Algorithm Implementation

AVL Tree Operations

The implementation follows standard AVL tree algorithms with these key features:

Height Calculation

function updateHeight(node: AvlNode): void {
  node.height = 1 + Math.max(getHeight(node.left), getHeight(node.right));
}

Balance Factor

function getBalanceFactor(node: AvlNode | null): number {
  if (!node) return 0;
  return getHeight(node.left) - getHeight(node.right);
}

Rotation Types

  • LL (Left-Left): Right rotation when left subtree is left-heavy
  • RR (Right-Right): Left rotation when right subtree is right-heavy
  • LR (Left-Right): Left rotation on left child, then right rotation on node
  • RL (Right-Left): Right rotation on right child, then left rotation on node

Animation System

The animation system generates frames for each step:

  1. Traversal Frames: Show path during search
  2. Insertion/Deletion Frames: Highlight affected nodes
  3. Rebalancing Frames: Show height updates and balance factor changes
  4. Rotation Frames: Step-by-step rotation visualization
  5. Completion Frames: Final tree state

๐Ÿงช Testing

Run the comprehensive test suite:

# Run tests
npm test

# Run tests with UI
npm run test:ui

# Run tests once
npm run test:run

The test suite includes:

  • โœ… 26 unit tests covering all AVL operations
  • โœ… Rotation correctness verification
  • โœ… Balance property maintenance
  • โœ… Edge case handling
  • โœ… Complex scenario testing

โŒจ๏ธ Keyboard Shortcuts

Key Action
Space Play/Pause animation
โ† Step backward
โ†’ Step forward
I Quick insert
D Quick delete
S Quick search

๐ŸŽจ Customization

Theme Configuration

The application supports custom themes through Tailwind CSS variables:

:root {
  --color-primary: #3b82f6;
  --color-accent: #d946ef;
  --color-success: #10b981;
  --color-error: #ef4444;
}

Layout Configuration

Adjust tree layout parameters:

const layoutConfig: LayoutConfig = {
  nodeRadius: 25,
  horizontalSpacing: 60,
  verticalSpacing: 80,
  minNodeDistance: 50,
};

๐Ÿ“Š Performance

The application is optimized for smooth 60fps animations:

  • Efficient Rendering: SVG-based rendering with D3.js optimizations
  • Memory Management: Proper cleanup of animation frames and event listeners
  • Tree Layout: Optimized tidy tree algorithm for large datasets
  • State Management: Zustand for minimal re-renders

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Commit your changes: git commit -m 'Add amazing feature'
  4. Push to the branch: git push origin feature/amazing-feature
  5. Open a Pull Request

๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • AVL tree algorithm based on Adelson-Velsky and Landis' original paper
  • Tree layout algorithm inspired by the Reingold-Tilford algorithm
  • Educational content structure influenced by visualgo.net
  • Color scheme and design patterns from Tailwind CSS

๐Ÿ”— Links


Built with โค๏ธ for computer science education

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published