This repository contains an implementation of the Splay Tree data structure in C. Splay trees are a type of self-adjusting binary search tree with the additional property that recently accessed elements are quick to access again.
- Splay Tree structure with frequency-based and standard splaying
- Insert operation with automatic splaying
- Pre-order traversal output
- Frequency count for repeated elements
- Comparison of splay and modified splay performance
- Simple file I/O for batch operations
main.c— Main implementation and demo programsplaytree.h— (Recommended) Declarations for splay tree structure and functionsinput.txt— Input file with data to insert into the tree (comma-separated integers)output.txt— Output file with traversal and timing results
-
Compile the code
Use a C compiler such as gcc:gcc -o main main.c
-
Prepare input
Create aninput.txtfile in the same directory. List integers separated by commas, e.g.:10,5,20,10,15,25, -
Run the program
./main
Output will be written to
output.txt.
Given an input.txt:
10,5,20,10,15,25,
The output.txt will contain:
Pre-order demonstration of splay tree: (10,1) (5,0) (20,0) (15,0) (25,0)
Comparison time of splay tree: 8
Pre-order demonstration of mod-splay tree: (10,1) (5,0) (20,0) (15,0) (25,0)
Comparison time of mod-splay tree: 7
- StNode — Node structure with data, frequency, parent, left, and right pointers
- Insert — Inserts a node and updates frequency if duplicate
- Splay — Brings the last accessed node to the root using rotations
- preorder — Prints a pre-order traversal to the output file