二叉查找树的基类实现(中)
来源:互联网 发布:淘宝115会员暗号 编辑:程序博客网 时间:2024/05/22 17:37
二叉查找树的基类实现(三)
下面是字符串数据类型的实现
StringClass.h 文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef STRING_CLASS_H
#define STRING_CLASS_H
#include "objectclass.h"
class
StringClass:
public
ObjectClass
{
private
:
char
*data;
int
length;
public
:
StringClass();
StringClass(
char
*data,
int
length);
public
:
int
Compare(ObjectClass *objcls);
virtual
void
OutPut();
float
getValue();
int
getLength();
};
#endif
StringClass.cpp 文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "StringClass.h"
StringClass::StringClass()
{
data=NULL;
length=0;
}
StringClass::StringClass(
char
*data,
int
length)
{
this
->data=data;
this
->length=length;
}
int
StringClass::Compare(ObjectClass *objcls)
{
if
(
this
->getValue() < objcls->getValue())
return
-1;
else
if
(
this
->getValue() > objcls->getValue())
return
1;
else
return
0;
}
//字符串的值的大小主要看前面几个字母,即按字典顺序比较
//因此越到后面它的权重就越小
float
StringClass::getValue()
{
if
(data==NULL)
return
0;
float
returnValue = 0;
for
(
int
i=0;i<length;i++)
{
float
mul=1;
for
(
int
k=0;k<i;k++)
mul=mul*10;
returnValue = returnValue+((
int
)(*(data+i))-(
int
)
'A'
)/mul;
}
return
returnValue;
}
void
StringClass::OutPut()
{
for
(
int
i=0;i<length;i++)
cout<<*(data+i);
}
int
StringClass::getLength()
{
return
length;
}
二叉查找树的基类实现(四)
下面是树的节点数据类型的实现
这里节点的成员数据域全部使用指针变量
BinaryTreeNode.h 文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//此类为二叉查找树的树节点类
//定义的关键子,值,父节点和儿子节点
#ifndef BINARY_TREE_NODE_H
#define BINARY_TREE_NODE_H
#include "objectclass.h"//通用类
class
BinaryTreeNode
{
private
:
ObjectClass *theKey;
//关键字
ObjectClass *theValue;
//值
BinaryTreeNode *parent;
//父亲节点
BinaryTreeNode *left;
//左儿子
BinaryTreeNode *right;
//右儿子
//定义左右子树的宽度以便打印
int
leftWidth;
int
rightWidth;
//定义当前节点应该输出的位子,从左起点到右的宽度
int
leftOutPutLen;
public
:
BinaryTreeNode();
BinaryTreeNode(ObjectClass *theKey,ObjectClass *theValue);
ObjectClass *getKey();
ObjectClass *getValue();
BinaryTreeNode *getLeft();
BinaryTreeNode *getRight();
BinaryTreeNode *getParent();
int
getLeftWidth();
int
getRightWidth();
int
getLeftOutPutLen();
void
setKey(ObjectClass *theKey);
void
setValue(ObjectClass *theValue);
void
setLeft(BinaryTreeNode *left);
void
setRight(BinaryTreeNode *Right);
void
setParent(BinaryTreeNode *parent);
void
setWidth(
int
,
int
);
//设置子树宽度
void
setLeftOutPutLen(
int
len);
};
#endif
BinaryTreeNode.cpp 文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "BinaryTreeNode.h"
BinaryTreeNode::BinaryTreeNode()
{
theKey = NULL;
theValue = NULL;
parent = NULL;
left = NULL;
right = NULL;
leftWidth=0;
rightWidth=0;
leftOutPutLen=0;
}
BinaryTreeNode::BinaryTreeNode(ObjectClass *theKey,ObjectClass *theValue)
{
this
->theKey = theKey;
this
->theValue = theValue;
parent = NULL;
left = NULL;
right = NULL;
leftWidth=0;
rightWidth=0;
leftOutPutLen=0;
}
int
BinaryTreeNode::getLeftWidth()
{
return
leftWidth;
}
int
BinaryTreeNode::getRightWidth()
{
return
rightWidth;
}
ObjectClass *BinaryTreeNode::getKey()
{
return
theKey;
}
ObjectClass *BinaryTreeNode::getValue()
{
return
theValue;
}
BinaryTreeNode *BinaryTreeNode::getLeft()
{
return
left;
}
BinaryTreeNode *BinaryTreeNode::getRight()
{
return
right;
}
BinaryTreeNode *BinaryTreeNode::getParent()
{
return
parent;
}
void
BinaryTreeNode::setWidth(
int
leftWidth,
int
rightWidth)
{
this
->leftWidth=leftWidth;
this
->rightWidth=rightWidth;
}
void
BinaryTreeNode::setValue(ObjectClass *theValue)
{
this
->theValue = theValue;
}
void
BinaryTreeNode::setKey(ObjectClass *theKey)
{
this
->theKey = theKey;
}
void
BinaryTreeNode::setLeft(BinaryTreeNode *left)
{
this
->left = left;
}
void
BinaryTreeNode::setRight(BinaryTreeNode *right)
{
this
->right = right;
}
void
BinaryTreeNode::setParent(BinaryTreeNode *parent)
{
this
->parent=parent;
}
int
BinaryTreeNode::getLeftOutPutLen()
{
return
leftOutPutLen;
}
void
BinaryTreeNode::setLeftOutPutLen(
int
len)
{
this
->leftOutPutLen = len;
}
本文出自:http://www.cnblogs.com/xiao-cheng/category/325955.html
- 二叉查找树的基类实现(中)
- 二叉查找树的基类实现(上)
- 二叉查找树的基类实现(下)
- 二叉树类的实现(查找树)
- 二叉查找树的类模板实现
- 二叉查找树(BST)的实现
- 二叉查找树的实现(c++)
- 二叉查找树的实现
- 二叉查找树的实现
- 二叉查找树的实现
- 二叉查找树的实现
- 二叉查找树的实现
- 二叉查找树的实现
- 二叉查找树的实现
- 二叉查找树的实现
- 二叉查找树的实现
- 自己实现Java中二叉查找树的部分功能(待补充)
- 二叉查找树实现
- android权限列表
- iPhone OS编程指南(二)
- iPhone 3.0 SDK开发秘籍笔记(第三章)
- Excel 2003移动工作表(含公式)到另一个工作薄
- 查询数据集导入到Execl
- 二叉查找树的基类实现(中)
- cisco mds 9124 cli学习【1】
- Tomcat Can't load AMD 64-bit .dll on a IA 32
- 转载:java泛型
- iPhone OS编程指南(一)
- 增加虚拟机可以使用的最大内存
- HDU 4006 The kth great number
- 二叉查找树的基类实现(下)
- 从这里开始