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

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

下面是字符串数据类型的实现

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值