Splay Tree.cpp - Petar'PetarV Velickovic Data Structure Splay Tree#include <stdio.h>#include <math.h>#include <string.h>#include <time.h>#include

Splay Tree.cpp - Petar'PetarV Velickovic Data Structure...

This preview shows page 1 - 3 out of 7 pages.

/* Petar 'PetarV' Velickovic Data Structure: Splay Tree */ #include <stdio.h> #include <math.h> #include <string.h> #include <time.h> #include <iostream> #include <vector> #include <list> #include <string> #include <algorithm> #include <queue> #include <stack> #include <set> #include <map> #include <complex> using namespace std; typedef long long lld; typedef unsigned long long llu; /* Splay Tree data structure is an efficient self-balancing implementation of a binary search tree. Complexity: O(log N) amortized for all operations */ struct TreeNode { int key; TreeNode* parent; TreeNode* left; TreeNode* right; TreeNode(int key) { this -> key = key; this -> parent = NULL; this -> left = NULL; this -> right = NULL; } }; class SplayTree { TreeNode *root; void zig(TreeNode*); void zig_zig(TreeNode*); void zig_zag(TreeNode*); void splay(TreeNode*); public: SplayTree(); SplayTree(TreeNode*); TreeNode* find(int); void insert(int); void Delete(int); void inOrderPrint(bool); };
Image of page 1
void SplayTree::zig(TreeNode *x) { TreeNode *p = x -> parent; if (p -> left == x) { TreeNode *A = x -> left; TreeNode *B = x -> right; TreeNode *C = p -> right; x -> parent = NULL; x -> right = p; p -> parent = x; p -> left = B; if (B != NULL) B -> parent = p; } else { TreeNode *A = p -> left; TreeNode *B = x -> left; TreeNode *C = x -> right; x -> parent = NULL; x -> left = p; p -> parent = x; p -> right = B; if (B != NULL) B -> parent = p; } } void SplayTree::zig_zig(TreeNode *x) { TreeNode *p = x -> parent; TreeNode *g = p -> parent;
Image of page 2
Image of page 3

You've reached the end of your free preview.

Want to read all 7 pages?

  • Spring '08
  • Ozemek,H
  • ASCII, Splay tree, class SplayTree

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes