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; } }
二叉树的定义,节点的增删改查以及父节点、子节点的各种操作。
最新推荐文章于 2020-06-01 21:10:04 发布