patterncppMinor
AVL tree for inserting and searching
Viewed 0 times
searchingandavlforinsertingtree
Problem
I have made an AVL tree. It works and does what it needs to do. I only need it to insert and search. I am comparing how long it takes to load and search a dictionary file in relation to other various data structures. My AVL tree takes 25 seconds to load the file.
This is in comparison to hash tables that took only three.
Is there any way I can make it faster?
MyDS stands for "My Data Structure", in case anyone was wondering.
```
//MyDS.h
#ifndef MyDS_H
#define MyDS_H
#include
#include
class MyDS {
private:
struct treeNode
{
treeNode* left;
treeNode* right;
int height;
std::string data;
treeNode() {left = NULL; right = NULL;};
treeNode(const std::string &v, treeNode l, treeNode r, int h){data = v; left = l; right = r; height = h;};
};
treeNode* root;
void push(const std::string & n, treeNode * & v);
bool search( const std::string& s, treeNode * & tree) ;
public:
MyDS();
~MyDS();
void push(const std::string & n);
void printPreOrder() const;
void preOrder(treeNode* pre) const;
void clear(treeNode* & tree);
void singleRightRotate(treeNode * & n);
void doubleRightRotate(treeNode * & n);
void singleLeftRotate(treeNode * & n);
void doubleLeftRotate(treeNode * & n);
bool search(const std::string & s);
int avlHeight (treeNode * h);
int max(int v1, int v2);
};
MyDS::MyDS()
{
root = NULL;
}
MyDS::~MyDS()
{
clear(root);
}
void MyDS::push(const std::string & n)
{
push(n,root);
}
void MyDS::singleRightRotate(treeNode * & n)
{
treeNode * temp;
temp = n->right;
n->right = temp->left;
temp->left = n;
n = temp;
n->height = max(avlHeight(n->left),avlHeight(n->right)) + 1;
temp->height = max(n->height,avlHeight(temp->right)) + 1;
}
void MyDS::singleLeftRotate(treeNode * & n)
{
treeNode * temp;
temp = n->left;
n->left = temp->right;
temp->right = n;
n = temp;
n->height = max(avlHeigh
This is in comparison to hash tables that took only three.
Is there any way I can make it faster?
MyDS stands for "My Data Structure", in case anyone was wondering.
```
//MyDS.h
#ifndef MyDS_H
#define MyDS_H
#include
#include
class MyDS {
private:
struct treeNode
{
treeNode* left;
treeNode* right;
int height;
std::string data;
treeNode() {left = NULL; right = NULL;};
treeNode(const std::string &v, treeNode l, treeNode r, int h){data = v; left = l; right = r; height = h;};
};
treeNode* root;
void push(const std::string & n, treeNode * & v);
bool search( const std::string& s, treeNode * & tree) ;
public:
MyDS();
~MyDS();
void push(const std::string & n);
void printPreOrder() const;
void preOrder(treeNode* pre) const;
void clear(treeNode* & tree);
void singleRightRotate(treeNode * & n);
void doubleRightRotate(treeNode * & n);
void singleLeftRotate(treeNode * & n);
void doubleLeftRotate(treeNode * & n);
bool search(const std::string & s);
int avlHeight (treeNode * h);
int max(int v1, int v2);
};
MyDS::MyDS()
{
root = NULL;
}
MyDS::~MyDS()
{
clear(root);
}
void MyDS::push(const std::string & n)
{
push(n,root);
}
void MyDS::singleRightRotate(treeNode * & n)
{
treeNode * temp;
temp = n->right;
n->right = temp->left;
temp->left = n;
n = temp;
n->height = max(avlHeight(n->left),avlHeight(n->right)) + 1;
temp->height = max(n->height,avlHeight(temp->right)) + 1;
}
void MyDS::singleLeftRotate(treeNode * & n)
{
treeNode * temp;
temp = n->left;
n->left = temp->right;
temp->right = n;
n = temp;
n->height = max(avlHeigh
Solution
Personally I would move memory management from MyDS into the tree structure (disable the assignment and copy constructor just to remove the possibility of incorrect usage. Add a constructor that moves the data into the tree.
struct treeNode
{
std::string data;
int height;
treeNode* left;
treeNode* right;
treeNode()
{
delete left;
delete right;
};
treeNode(std::string const& v, treeNode* l, treeNode* r, int h)
: data(v)
, height(h)
, left(l)
, right(r)
{}
treeNode(std::string&& v, treeNode* l, treeNode* r, int h)
: data(std::forward(v))
, height(h)
, left(l)
, right(r)
{}
treeNode(treeNode const&) = delete;
treeNode& operator=(treeNode const&) = delete;
};Code Snippets
struct treeNode
{
std::string data;
int height;
treeNode* left;
treeNode* right;
treeNode()
{
delete left;
delete right;
};
treeNode(std::string const& v, treeNode* l, treeNode* r, int h)
: data(v)
, height(h)
, left(l)
, right(r)
{}
treeNode(std::string&& v, treeNode* l, treeNode* r, int h)
: data(std::forward(v))
, height(h)
, left(l)
, right(r)
{}
treeNode(treeNode const&) = delete;
treeNode& operator=(treeNode const&) = delete;
};Context
StackExchange Code Review Q#70842, answer score: 3
Revisions (0)
No revisions yet.