我有两个二叉树类,分别管理着不同的数据
class bt1{
//自身业务逻辑
String s;
public String getS(){
return "godness " + s;
}
//二叉树逻辑
bt1 left;
bt1 right;
bt1 parent;
public void setLeft(bt1 l){
this.left = l;
l.parent = this;
}
public bt1 getLeft(){
return left;
}
public void setRight(bt1 r){
this.right = r;
right.parent = this;
}
public bt1 getRight(){
return right;
}
public bt1 getParent(){
return parent;
}
}
class bt2{
//自身业务逻辑
int i;
public void add(){
i++;
}
//二叉树逻辑
bt2 left;
bt2 right;
bt2 parent;
public void setLeft(bt2 l){
this.left = l;
l.parent = this;
}
public bt2 getLeft(){
return left;
}
public void setRight(bt2 r){
this.right = r;
right.parent = this;
}
public bt2 getRight(){
return right;
}
public bt2 getParent(){
return parent;
}
}
有没有什么办法把两个类公共的二叉树逻辑部分提出来做为父类,两个类分别继承这个父类再只需实现自己的业务逻辑,就能达成原代码功能的?
其实关于二叉树的泛型实现,我觉得 树应该通用,其节点存储的数据为泛型比较理想,左右孩子和父节点的类型应该与创建的树的类型一致,
你觉得呢?今天才看到,很抱歉
class BTrees {
protected BTrees<T> leftChild;
protected BTrees<T> rightChild;
T data;
protected BTrees<T> parent; //非必需,半冗余数据
public BTrees<T> getLeftChild() {
return leftChild;
}
public void setLeftChild(BTrees<T> leftChild) {
this.leftChild = leftChild;
}
public BTrees<T> getRightChild() {
return rightChild;
}
public void setRightChild(BTrees<T> rightChild) {
this.rightChild = rightChild;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public BTrees<T> getParent() {
return parent;
}
public void setParent(BTrees<T> parent) {
this.parent = parent;
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。