二叉查找树的基类实现(中)

来源:互联网 发布:淘宝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;

    intlength;

public:

    StringClass();

    StringClass(char*data,int length);

public:

    intCompare(ObjectClass *objcls);

    virtualvoid OutPut();

    floatgetValue();

    intgetLength();

};

#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;

    elseif(this->getValue() > objcls->getValue())

    return1;

    else

    return0;

}

//字符串的值的大小主要看前面几个字母,即按字典顺序比较

//因此越到后面它的权重就越小

float StringClass::getValue()

{

    if(data==NULL)return0;

    floatreturnValue = 0;

    for(inti=0;i<length;i++)

    {

       floatmul=1;

       for(intk=0;k<i;k++)

          mul=mul*10;

       returnValue = returnValue+((int)(*(data+i))-(int)'A')/mul;

    }   

    returnreturnValue;

}

void StringClass::OutPut()

{

    for(inti=0;i<length;i++)

    cout<<*(data+i);

}

int StringClass::getLength()

{

    returnlength;

}

 

 

 

二叉查找树的基类实现(四)

 

下面是树的节点数据类型的实现

这里节点的成员数据域全部使用指针变量

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;//右儿子

  

    //定义左右子树的宽度以便打印

    intleftWidth;

    intrightWidth;

  

    //定义当前节点应该输出的位子,从左起点到右的宽度

    intleftOutPutLen;

public:

    BinaryTreeNode();

    BinaryTreeNode(ObjectClass *theKey,ObjectClass *theValue);

  

    ObjectClass *getKey();

    ObjectClass *getValue();

    BinaryTreeNode *getLeft();

    BinaryTreeNode *getRight();

    BinaryTreeNode *getParent();

    intgetLeftWidth();

    intgetRightWidth();

    intgetLeftOutPutLen();

  

    voidsetKey(ObjectClass *theKey);

    voidsetValue(ObjectClass *theValue);   

    voidsetLeft(BinaryTreeNode *left);

    voidsetRight(BinaryTreeNode *Right);

    voidsetParent(BinaryTreeNode *parent);

    voidsetWidth(int,int);//设置子树宽度

    voidsetLeftOutPutLen(intlen);

};

#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()

{

    returnleftWidth;

}

  

int BinaryTreeNode::getRightWidth()

{

    returnrightWidth;

}

  

ObjectClass *BinaryTreeNode::getKey()

{

    returntheKey;

}

  

ObjectClass *BinaryTreeNode::getValue()

{

    returntheValue;

}

  

BinaryTreeNode *BinaryTreeNode::getLeft()

{

    returnleft;

}

  

BinaryTreeNode *BinaryTreeNode::getRight()

{

    returnright;

}

  

BinaryTreeNode *BinaryTreeNode::getParent()

{

    returnparent;

}

  

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()

{

    returnleftOutPutLen;

}

void BinaryTreeNode::setLeftOutPutLen(int len)

{

    this->leftOutPutLen = len;

}

 

 

本文出自:http://www.cnblogs.com/xiao-cheng/category/325955.html

原创粉丝点击