Ahoj, potřebuji poradit jak vytvořit dokonalý binární vyhledávací strom/ nebo aspoň vyvážený. Hlavní problém mám s vytvořením rotací.
Můj kód:
Tree.h
#ifndef _TREE_H_
#define _TREE_H_
#include <iostream>
#include <string>
using namespace std;
class Tree
{
private:
class Node
{
public:
string value;
string value_in;
Node* left;
Node* right;
Node(string);
};
Node* root;
void internalInsertValue(string, Node*&);
bool internalFindValue(string, Node*&);
void internalPrintSorted(bool, Node*&);
int internalGetLHeight(int, Node*);
int internalGetRHeight(int, Node*);
void internalRotateLeftOnce(Node*);
void internalRotateRightOnce(Node*);
public:
Tree(void) { this->root = NULL; }
void insertValue(string);
bool findValue(string);
void printSorted(bool);
int getLHeight(void);
int getRHeight(void);
void rotateLeftOnce();
void rotateRightOnce();
};
#endif
Tree.cpp
#include "Tree.h"
#include <iostream>
#include <string>
using namespace std;
Tree::Node::Node(string value)
{
this->value = value;
this->left = this->right = NULL;
}
void Tree::insertValue(string value)
{
internalInsertValue(value, this->root);
return;
}
void Tree::internalInsertValue(string value, Node*& root)
{
if (root == NULL) {
root = new Node(value);
return;
}
if (root->value == value) {
return;
}
if (root->value > value) {
internalInsertValue(value, root->left);
return;
}
internalInsertValue(value, root->right);
return;
}
bool Tree::findValue(string value)
{
return(internalFindValue(value, this->root));
}
bool Tree::internalFindValue(string value, Node*& root)
{
if (root == NULL)
return(false);
if (root->value == value)
return(true);
if (value < root->value)
return(internalFindValue(value, root->left));
return(internalFindValue(value, root->right));
}
void Tree::printSorted(bool desc)
{
internalPrintSorted(desc, this->root);
return;
}
void Tree::internalPrintSorted(bool desc, Node*& root)
{
if (root == NULL)
return;
if (!desc)
{
internalPrintSorted(desc, root->left);
cout << root->value << " ";
internalPrintSorted(desc, root->right);
}
else
{
internalPrintSorted(desc, root->right);
cout << root->value << " ";
internalPrintSorted(desc, root->left);
}
return;
}
int Tree::getLHeight(void)
{
return(internalGetLHeight(-1, this->root));
}
int Tree::internalGetLHeight(int h, Node* root)
{
if (root == NULL)
return(h);
int lHeight = internalGetLHeight(h + 1, root->left);
return(lHeight);
}
int Tree::getRHeight(void)
{
return(internalGetRHeight(-1, this->root));
}
int Tree::internalGetRHeight(int h, Node* root)
{
if (root == NULL)
return(h);
int rHeight = internalGetRHeight(h + 1, root->right);
return(rHeight);
}
void Tree::rotateLeftOnce()
{
if (getLHeight() < getRHeight())
return(internalRotateLeftOnce(this->root));
}
void Tree::internalRotateLeftOnce(Node* root)
{
Node*otherNode;
otherNode = root->left;
root->left = otherNode->right;
otherNode->right = root;
root = otherNode;
return;
}
Main.cpp
#include "Tree.h"
#include <iostream>
#include<string>
using namespace std;
int main()
{
Tree t;
t.insertValue("acl");
t.insertValue("aspx");
t.insertValue("css3");
t.insertValue("odbc");
t.insertValue("postgre");
t.insertValue("php5");
t.insertValue("wysiwyg");
t.printSorted(false);
cout << "vyskaL" <<t.getLHeight() << endl;
cout << "vyskaR" << t.getRHeight() << endl;
system("pause");
return(0);
}
Děkuji za rady