资源简介
c++实现的平衡树算法,带测试用例,测试中可以添加元素和删除元素,在添加和删除过程中树仍保持平衡
代码片段和文件信息
#include “OurCSCE310Tree.h“
#include
#include
using namespace std;
/*
class OurCSCE310Tree{
public:
OurCSCE310Tree(void);
OurCSCE310Tree(OurCSCE310Tree&);
~OurCSCE310Tree(void);
void operator=(OurCSCE310Tree&);
OurCSCE310Tree* getParent(void);
OurCSCE310Tree* getLeft(void);
OurCSCE310Tree* getRight(void);
int getValue(void);
void setParent(OurCSCE310Tree*);
void setLeft(OurCSCE310Tree*);
void setRight(OurCSCE310Tree*);
void setValue(int);
void insert(int);
void printPreorder(void);
void printInorder(void);
void printPostorder(void);
void rotateLeft(void);
void rotateRight(void);
void rotateLeftRight(void);
void rotateRightLeft(void);
void deleteNode(int);
int getHeight();
private:
int value;
OurCSCE310Tree* parent;
OurCSCE310Tree* left;
OurCSCE310Tree* right;
};
*/
OurCSCE310Tree::OurCSCE310Tree(){
value = 0;
parent = NULL;
left = NULL;
right = NULL;
}
OurCSCE310Tree::OurCSCE310Tree( OurCSCE310Tree& other){
delete parent;
delete left;
delete right;
value = other.getValue();
parent = other.getParent();
left = other.getLeft();
right = other.getRight();
}
void OurCSCE310Tree::operator=( OurCSCE310Tree& other){
delete parent;
delete left;
delete right;
value = other.getValue();
parent = other.getParent();
left = other.getLeft();
right = other.getRight();
}
OurCSCE310Tree::~OurCSCE310Tree(){
delete left;
left = NULL;
delete right;
right = NULL;
value = 0;
}
OurCSCE310Tree* OurCSCE310Tree::getParent(){
return parent;
}
OurCSCE310Tree* OurCSCE310Tree::getLeft(){
return left;
}
OurCSCE310Tree* OurCSCE310Tree::getRight(){
return right;
}
int OurCSCE310Tree::getValue(){
return value;
}
void OurCSCE310Tree::setParent( OurCSCE310Tree* par ){
parent = par;
}
void OurCSCE310Tree::setLeft( OurCSCE310Tree* lft ){
left = lft;
}
void OurCSCE310Tree::setRight( OurCSCE310Tree* rght ){
right = rght;
}
void OurCSCE310Tree::setValue( int val ){
value = val;
}
void OurCSCE310Tree::insert( int val ){
if( !getValue() ){
setValue( val );
}
else if( ( val < getValue() && !getLeft() ) || ( val < getValue() && !getLeft()->getValue() ) ){
left = new OurCSCE310Tree();
left->setParent( this );
left->setValue( val );
}
else if ((val > getValue() && !getRight()) || (val > getValue() && !getRight()->getValue()))
{
right = new OurCSCE310Tree();
right->setParent( this );
right->setValue( val );
}
else if( val < getValue() ){
getLeft()->insert( val );
}
else{
getRight()->insert( val );
}
if( getLeft() && getLeft()->getRight() && !getRight()/*1*/ || getLeft() && getLeft()->getRight() && getRight() && getLeft()->getHeight() > getRight()->getHeight() + 1/*2*/ && getLeft()->getRight()->getHeight() > getLeft()->getLeft()->getHeight() + 1 /*3*/){
rotate
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 14 2018-04-24 12:15 add.txt
文件 26 2018-04-24 12:15 delete.txt
文件 9737 2018-04-24 12:49 OurCSCE310Tree.cpp
文件 840 2018-04-20 17:09 OurCSCE310Tree.h
文件 30 2018-04-20 17:11 part01test01.adds.input.txt
文件 73 2018-04-20 17:11 part01test01.solution.txt
文件 22 2018-04-20 17:11 part02test01.adds.input.txt
文件 55 2018-04-20 17:12 part02test01.solution.txt
文件 10 2018-04-20 17:12 part03test01.adds.input.txt
文件 26 2018-04-20 17:12 part03test01.deletes.input.txt
文件 5 2018-04-20 17:13 part03test01.solution.txt
文件 210 2018-04-22 13:55 readme.txt
文件 707 2018-04-20 17:13 test01.cpp
文件 707 2018-04-20 17:13 test02.cpp
文件 1022 2018-04-24 12:17 test03.cpp
- 上一篇:Tarjan 的 LCA 算法
- 下一篇:c++实现的单链表
评论
共有 条评论