二叉树的定义,节点的增删改查以及父节点、子节点的各种操作。

 
 
public class TreeNode implements Serializable {
private String treename ;
private String parentNodeName ;
private List < TreeNode > childList ;
public TreeNode (){
initChildList ();
}
public TreeNode ( TreeNode parentNode ){
init ChildList ();
}
public boolean isLeaf (){
if ( childList == null ){
return true ;
} else {
if ( childLisy . isEmpty ()){
return true ;
} else {
return false ;
}
}
}
/* 插入一个child节点到当前节点中 */
public void addChildNode ( TreeNode treeNode ) {
initChildList ();
childList . add ( treeNode );
}
/**
* init childList
*/
public void initChildList () {
if ( childList == null )
childList = new ArrayList < TreeNode >();
}
public boolean isValidTree () {
return true ;
}
/* 返回当前节点的父辈节点集合 */
public List < TreeNode > getElders () {
List < TreeNode > elderList = new ArrayList < TreeNode >();
TreeNode parentNode = findTreeNodeByName ( this . getParentNoteName ());
if ( parentNode == null ) {
return elderList ;
} else {
elderList . add ( parentNode );
elderList . addAll ( parentNode . getElders ());
return elderList ;
}
}
/* 返回当前节点的晚辈集合 */
public List < TreeNode > getJuniors () {
List < TreeNode > juniorList = new ArrayList < TreeNode >();
List < TreeNode > childList = this . getChildList ();
if ( childList == null ) {
return juniorList ;
} else {
int childNumber = childList . size ();
for ( int i = 0 ; i < childNumber ; i ++) {
TreeNode junior = childList . get ( i );
juniorList . add ( junior );
juniorList . addAll ( junior . getJuniors ());
}
return juniorList ;
}
}
/* 删除节点和它下面的晚辈 */
public void deleteNode () {
TreeNode parentNode = findTreeNodeByName ( this . getParentNoteName ());
String idName = this . getNodeName ();
if ( parentNode != null ) {
parentNode . deleteChildNode ( idName );
}
}
/* 删除当前节点的某个子节点 */
public void deleteChildNode ( String childIdName ) {
List < TreeNode > childList = this . getChildList ();
int childNumber = childList . size ();
for ( int i = 0 ; i < childNumber ; i ++) {
TreeNode child = childList . get ( i );
if ( child . getNodeName () == childIdName ) {
childList . remove ( i );
return ;
}
}
}
//动态的插入一个节点到当前树中
public boolean insertJuniorNode ( TreeNode treeNode ){
String juniorParentName = treeNode . getParentName ();
if ( this . parentNodeName == juniorParentName ){
addChildNode ( treeNode );
return true ;
} else {
List < TreeNode > childList = this . getChildList ();
int childNumber = childList . size ();
boolean insertFalg ;
for ( int i = 0 ; i < childNumber ; i ++){
TreeNode childNode = childList . get ( i );
insertFlag = childNode . insertJuniorNode ();
if ( insertFlag ){
return true ;
} return false ;
}
return false ;
}
}
/* 找到一颗树中某个节点 */
    public TreeNode findTreeNodeById(String name) {         if (this.nodeName == name)             return this;         if (childList.isEmpty() || childList == null) {             return null;         } else {             int childNumber = childList.size();             for (int i = 0; i < childNumber; i++) {                 TreeNode child = childList.get(i);                 TreeNode resultNode = child.findTreeNodeById(name);                 if (resultNode != null) {                     return resultNode;                 }             }             return null;         }     }     /* 找到一颗树中某个节点 */     public TreeNode findTreeNodeByName(String name) {         if (this.nodeName .equals(name))             return this;         if (childList.isEmpty() || childList == null) {             return null;         } else {             int childNumber = childList.size();             for (int i = 0; i < childNumber; i++) {                 TreeNode child = childList.get(i);                 TreeNode resultNode = child.findTreeNodeByName(name);                 if (resultNode != null) {                     return resultNode;                 }             }             return null;         }     }     /* 遍历一棵树,层次遍历 */     public String traverseNames() {            StringBuffer sb=new StringBuffer();         if (childList == null || childList.isEmpty())             return "";         int childNumber = childList.size();         for (int i = 0; i < childNumber; i++) {             TreeNode child = childList.get(i);                          sb.append(String.format(",'%s' %s",child.getNodeName(),child.traverseNames()));         }         return sb.toString();     }       public List<TreeNode> getChildList() { return childList; }     public void setChildList(List<TreeNode> childList) {         this.childList = childList;     }     public String getNodeName() {         return nodeName;     }     public void setNodeName(String nodeName) {         this.nodeName = nodeName;     }         public String getParentNoteName() { return parentNoteName; } public void setParentNoteName(String parentNoteName) { this.parentNoteName = parentNoteName; } }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值