Implementation scheme of iteration and combination mode of general tree structure

Time:2022-7-29

In the daily development process. Tree structure is used very frequently.

For example: company organizational structure, various classification structures, grouping structures, etc.

 

 

 

 

Implementation scheme of iteration and combination mode of general tree structureImplementation scheme of iteration and combination mode of general tree structure

SET FOREIGN_KEY_CHECKS = 0;

CREATE TABLE IF NOT EXISTS `tbl_sapo_group` (
  `id` int(10) unsigned NOT NULL AUTO_ Increment comment 'primary key',
  `Code ` varchar (100) not null comment 'unique code',
  `create_ Time ` datetime (3) not null comment 'creation time',
  `last_ update_ Time ` datetime (3) default null comment 'last update time',
  `Name ` varchar (255) not null comment 'name',
  `Detail ` varchar (255) default null comment 'details',
  `Status ` int (10) unsigned not null default 2 comment 'status: 0-invalid, 1-valid, 2-edit',
  `group_ Type ` varchar (100) not null comment 'group type',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uni_idx_group_code` (`code`),
  KEY `idx_group_group_type` (`group_type`),
  CONSTRAINT `fk_group_group_type` FOREIGN KEY (`group_type`) REFERENCES `tbl_sapo_group_type` (`code`)
)Engine=innodb default charset=utf8 comment='Group';

CREATE TABLE IF NOT EXISTS `tbl_sapo_group_rel` (
  `id` int(10) unsigned NOT NULL AUTO_ Increment comment 'primary key',
  `create_ Time ` datetime (3) not null comment 'creation time',
  `last_ update_ Time ` datetime (3) default null comment 'last update time',
  `parent_ Code ` varchar (100) not null comment 'parent node code, TBL_ sapo_ Group table code',
  `child_ Code ` varchar (100) not null comment 'child node code, TBL_ sapo_ Group table code',
  `Status ` int (10) unsigned not null default 2 comment 'status: 0-invalid, 1-valid, 2-edit',
  `group_ rel_ Type ` varchar (100) not null comment 'group relationship type code, from TBL_ sapo_ group_ rel_ Type table code',
  `tree_ Code ` varchar (100) not null comment 'tree node code, TBL_ sapo_ Tree table code',
  PRIMARY KEY (`id`),
  KEY `idx_group_rel_child_code` (`child_code`),
  KEY `idx_group_rel_parent_code` (`parent_code`),
  KEY `idx_group_rel_group_rel_type` (`group_rel_type`),
  KEY `idx_group_rel_tree_code_status_parent_code_child_code` (`tree_code`,`status`,`parent_code`,`child_code`),
  CONSTRAINT `fk_group_rel_child_code` FOREIGN KEY (`child_code`) REFERENCES `tbl_sapo_group` (`code`),
  CONSTRAINT `fk_group_rel_group_rel_type` FOREIGN KEY (`group_rel_type`) REFERENCES `tbl_sapo_group_rel_type` (`code`),
  CONSTRAINT `fk_group_rel_parent_code` FOREIGN KEY (`parent_code`) REFERENCES `tbl_sapo_group` (`code`),
  CONSTRAINT `fk_group_rel_tree_code` FOREIGN KEY (`tree_code`) REFERENCES `tbl_sapo_tree` (`code`)
)Engine=innodb default charset=utf8 comment='group relationship';

CREATE TABLE IF NOT EXISTS `tbl_sapo_group_rel_type` (
  `id` int(10) unsigned NOT NULL AUTO_ Increment comment 'primary key',
  `Code ` varchar (100) not null comment 'unique code',
  `create_ Time ` datetime (3) not null comment 'creation time',
  `last_ update_ Time ` datetime (3) default null comment 'last update time',
  `Name ` varchar (255) not null comment 'name',
  `Detail ` varchar (255) default null comment 'details',
  `Status ` int (10) unsigned not null default 2 comment 'status: 0-invalid, 1-valid, 2-edit',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uni_idx_group_rel_type_code` (`code`)
)Engine=innodb default charset=utf8 comment='group relationship type';

CREATE TABLE IF NOT EXISTS `tbl_sapo_group_type` (
  `id` int(10) unsigned NOT NULL AUTO_ Increment comment 'primary key',
  `Code ` varchar (100) not null comment 'unique code',
  `create_ Time ` datetime (3) not null comment 'creation time',
  `last_ update_ Time ` datetime (3) default null comment 'last update time',
  `Name ` varchar (255) not null comment 'name',
  `Detail ` varchar (255) default null comment 'details',
  `Status ` int (10) unsigned not null default 2 comment 'status: 0-invalid, 1-valid, 2-edit',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uni_idx_group_type_code` (`code`)
)Engine=innodb default charset=utf8 comment='group type';

CREATE TABLE IF NOT EXISTS `tbl_sapo_tree` (
  `id` int(10) unsigned NOT NULL AUTO_ Increment comment 'primary key',
  `Code ` varchar (100) not null comment 'unique code',
  `create_ Time ` datetime (3) not null comment 'creation time',
  `last_ update_ Time ` datetime (3) default null comment 'last update time',
  `Name ` varchar (255) not null comment 'name',
  `Detail ` varchar (255) default null comment 'details',
  `Status ` int (10) unsigned not null default 2 comment 'status: 0-invalid, 1-valid, 2-edit',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uni_idx_tree_code` (`code`)
)Engine=innodb default charset=utf8 comment='tree definition';

CREATE TABLE IF NOT EXISTS `tbl_sapo_tree_group` (
  `id` int(10) unsigned NOT NULL AUTO_ Increment comment 'primary key',
  `create_ Time ` datetime (3) not null comment 'creation time',
  `last_ update_ Time ` datetime (3) default null comment 'last update time',
  `group_ Code ` varchar (100) not null comment 'group code, TBL_ sapo_ Group table code',
  `tree_ Code ` varchar (100) not null comment 'tree code, TBL_ sapo_ Tree table code',
  `Status ` int (10) unsigned not null default 2 comment 'status: 0-invalid, 1-valid, 2-edit',
  `is_ Root ` int (10) unsigned default null comment 'is the root node: 1-root node, null non root node',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uni_idx_tree_group_tree_code_is_root` (`tree_code`,`is_root`),
  KEY `idx_tree_group_group_code` (`group_code`),
  CONSTRAINT `fk_tree_group_group_code` FOREIGN KEY (`group_code`) REFERENCES `tbl_sapo_group` (`code`),
  CONSTRAINT `fk_tree_group_tree_code` FOREIGN KEY (`tree_code`) REFERENCES `tbl_sapo_tree` (`code`)
)Engine=innodb default charset=utf8 comment='groups contained in the tree';

SET FOREIGN_KEY_CHECKS = 1;

Create table statement

As shown in the figure, the relational database model basically meets the purpose of reusing multiple trees and groups in a system.

 

The node of a tree may be a single node or the root of a subtree. When we need to traverse, different nodes need different processing, using [polymorphism].

However, when processing, do not distinguish which node it is, and provide a [transparency] processing method. To achieve this, you need to reference two modes:Iteration mode, combination mode

The old rule is to introduce concepts first and then implement them.

Iterator mode Provides a way to traverse a collection without exposing its implementation
Combination mode Customers can treat collections of objects as well as individual objects equally

 

Iterator mode:

 

 

Iterator example: array implementation iterator

 

 

Implementation scheme of iteration and combination mode of general tree structureImplementation scheme of iteration and combination mode of general tree structure

//Iterator interface
interface Iterator {
    boolean hasNext();

    Object next();
}

//Menu item
class MenuItem {

    String name;
    String description;
    boolean vegetarian;
    double price;

    public MenuItem(String name, String description, boolean vegetarian, double price) {
        this.name = name;
        this.description = description;
        this.vegetarian = vegetarian;
        this.price = price;
    }
    //Getter, setter method
    public String getName() {
        return name;
    }
}

//Menu
class DinerMenu {
    static final int MAX_ITEMS = 6;
    int numberOfItems = 0;
    MenuItem[] menuItems;

    public DinerMenu() {
        menuItems = new MenuItem[MAX_ITEMS];
        AddItem ("braised lion head", "Jiangnan famous dish", true, 50D);
        AddItem ("husband and wife lung tablets", "nothing to do with husband and wife", true, 70D);
    }

    public void addItem(String name, String description, boolean vegetarian, double price) {
        MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
        if (numberOfItems >= MAX_ITEMS) {
            System.out.println("sorry,menu is full");
        } else {
            menuItems[numberOfItems] = menuItem;
            numberOfItems += 1;
        }
    }

    public MenuItem[] getMenuItems() {
        return menuItems;
    }

    public Iterator createIteator() {
        return new DinerMenuIterator(menuItems);
    }
}

class DinerMenuIterator implements Iterator {
    MenuItem[] items;
    int position = 0;

    public DinerMenuIterator(MenuItem[] items) {
        this.items = items;
    }

    public Object next() {
        MenuItem menuItem = items[position];
        position = position + 1;
        return menuItem;
    }

    public boolean hasNext() {
        //The array may not be full
        if (position >= items.length || items[position] == null) {
            return false;
        } else {
            return true;
        }
    }

    public void remove() {
        if (position <= 0) {
            throw new IllegalStateException("you can't an item unitl you've done at least on next()");
        }
        if (items[position - 1] != null) {
            for (int i = position - 1; i < (items.length - 1); i++) {
                items[i] = items[i + 1];
            }
            items[items.length - 1] = null;
        }

    }

}

//Testing
class Test {
    public static void main(String[] args) {
        Iterator iterator = (new DinerMenu()).createIteator();
        while(iterator.hasNext()){
            MenuItem menuItem = (MenuItem) iterator.next();
            System.out.println(menuItem.getName());
        }

    }
}
Iterator pattern example

Array iterator

1. Of course, remove can not be implemented, because it may be removed concurrently, and the iterator is not safe.

We simply throw a java.lang.unsupported operation exception

After 2.java5, collections can use the form of for/in instead of the displayed creation iterator.

for( Object obj: collection){

}

 

For different sets, we have different traversal methods. Is there a general pattern of traversing a set to mask this difference? This pattern is iterator.

The iterator pattern provides a way to sequentially access the elements of an aggregate object without exposing its internal representation.

In fact, to put it bluntly, iterator mode is to shield different underlying operation logic by defining a unified operation interface.

If you can have a unified method to access every object in the aggregation, you can write polymorphic code to match these aggregations.

Put the wandering task on the iterator, not the aggregation. This simplifies the interface and implementation of aggregation. Clear allocation of responsibilities.

Conform to [single responsibility]. If the iterator mode is not used, and the set is changed, for example, from set to array, this class must be changed, and the traversal method must also be changed.

 

Combination mode:

Allows you to combine objects into a tree structure to represent the “whole / part” hierarchy.

Composition allows customers to deal with individual objects and combinations of objects in a consistent manner. That is, we can ignore the differences between object combinations and individual objects and use the same operation.

The combination mode sacrifices [single responsibility] to obtain [transparency], which means that the customer processing combination and leaf nodes are treated equally. Whether a node is a combination or a leaf node is transparent to customers.

 

 

 

 

Example of combined mode:

 

 

 

 

Implementation scheme of iteration and combination mode of general tree structureImplementation scheme of iteration and combination mode of general tree structure

public abstract class MenuComponent {
   
    //Operation node requires method
    public void add(MenuComponent menuComponent) {
        throw new UnsupportedOperationException();
    }
    public void remove(MenuComponent menuComponent) {
        throw new UnsupportedOperationException();
    }
    public MenuComponent getChild(int i) {
        throw new UnsupportedOperationException();
    }
  
    //Menu itself method
    public String getName() {
        throw new UnsupportedOperationException();
    }
    public String getDescription() {
        throw new UnsupportedOperationException();
    }
    public double getPrice() {
        throw new UnsupportedOperationException();
    }
    public boolean isVegetarian() {
        throw new UnsupportedOperationException();
    }
  
    public void print() {
        throw new UnsupportedOperationException();
    }
}

MenuComponent

Implementation scheme of iteration and combination mode of general tree structureImplementation scheme of iteration and combination mode of general tree structure

public class Menu extends MenuComponent {
    ArrayList menuComponents = new ArrayList();
    String name;
    String description;

    public Menu(String name, String description) {
        this.name = name;
        this.description = description;
    }

    public void add(MenuComponent menuComponent) {
        menuComponents.add(menuComponent);
    }

    public void remove(MenuComponent menuComponent) {
        menuComponents.remove(menuComponent);
    }

    public MenuComponent getChild(int i) {
        return (MenuComponent) menuComponents.get(i);
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public void print() {
        System.out.print("\n" + getName());
        System.out.println(", " + getDescription());
        System.out.println("---------------------");

        Iterator iterator = menuComponents.iterator();
        while (iterator.hasNext()) {
            MenuComponent menuComponent = (MenuComponent) iterator.next();
            menuComponent.print();
        }
    }
}

Menu

Implementation scheme of iteration and combination mode of general tree structureImplementation scheme of iteration and combination mode of general tree structure

public class MenuItem extends MenuComponent {
    String name;
    String description;
    boolean vegetarian;
    double price;
    
    public MenuItem(String name, 
                    String description, 
                    boolean vegetarian, 
                    double price) 
    { 
        this.name = name;
        this.description = description;
        this.vegetarian = vegetarian;
        this.price = price;
    }
  
    public String getName() {
        return name;
    }
  
    public String getDescription() {
        return description;
    }
  
    public double getPrice() {
        return price;
    }
  
    public boolean isVegetarian() {
        return vegetarian;
    }
  
    public void print() {
        System.out.print("  " + getName());
        if (isVegetarian()) {
            System.out.print("(v)");
        }
        System.out.println(", " + getPrice());
        System.out.println("     -- " + getDescription());
    }
}

MenuItem

Implementation scheme of iteration and combination mode of general tree structureImplementation scheme of iteration and combination mode of general tree structure

public class Waitress {
    MenuComponent allMenus;
 
    public Waitress(MenuComponent allMenus) {
        this.allMenus = allMenus;
    }
 
    public void printMenu() {
        allMenus.print();
    }
}

Waitress

 

Example:

Use iterative and composite patterns to achieve a common tree structure:

1. Core and group to group relationship.

2. The scheme realizes internal iterators and external iterators. Use according to the actual situation.

 

 

Implementation scheme of iteration and combination mode of general tree structureImplementation scheme of iteration and combination mode of general tree structure

/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!50503 SET NAMES utf8mb4 */;
/*!40014 SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @[email protected]@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @[email protected]@SQL_NOTES, SQL_NOTES=0 */;

CREATE TABLE IF NOT EXISTS `tbl_sapo_group` (
  `id` int(10) unsigned NOT NULL AUTO_ Increment comment 'primary key',
  `Code ` varchar (100) not null comment 'unique code',
  `create_ Time ` datetime (3) not null comment 'creation time',
  `last_ update_ Time ` datetime (3) default null comment 'last update time',
  `Name ` varchar (255) not null comment 'name',
  `Detail ` varchar (255) default null comment 'details',
  `Status ` int (10) unsigned not null default 2 comment 'status: 0-invalid, 1-valid, 2-edit',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uni_idx_group_code` (`code`) USING BTREE
) ENGINE=InnoDB AUTO_ Increment=9 default charset=utf8 comment='Group';

/*!40000 ALTER TABLE `tbl_sapo_group` DISABLE KEYS */;
INSERT INTO `tbl_sapo_group` (`id`, `code`, `create_time`, `last_update_time`, `name`, `detail`, `status`) VALUES
    (1,'hotel','2022-06-08 10:25:53.000', null, 'hotel', null, 2),
    (2,'7day','2022-06-08 10:26:18.000', null, 'seven days', null, 2),
    (3,'rujia','2022-06-08 10:26:43.000', null, 'home like', null, 2),
    (4,'wujiaochang','2022-06-08 10:27:37.000', null, 'pentagonal field', null, 2),
    (5,'huazhu','2022-06-08 22:48:23.000', null, 'Huazhu', null, 2),
    (6,'hanting','2022-06-08 22:48:43.000', null, 'Hanting', null, 2),
    (7,'hongkou','2022-06-09 09:46:57.000', null, 'Hongkou Football Field', null, 2),
    (8,'jiangwan','2022-06-09 09:47:22.000', null, 'Jiangwan', null, 2);
/*!40000 ALTER TABLE `tbl_sapo_group` ENABLE KEYS */;

CREATE TABLE IF NOT EXISTS `tbl_sapo_group_rel` (
  `id` int(10) unsigned NOT NULL AUTO_ Increment comment 'primary key',
  `create_ Time ` datetime (3) not null comment 'creation time',
  `last_ update_ Time ` datetime (3) default null comment 'last update time',
  `parent_ Code ` varchar (100) not null comment 'name',
  `child_ Code ` varchar (100) not null comment 'details',
  `Status ` int (10) unsigned not null default 2 comment 'status: 0-invalid, 1-valid, 2-edit',
  PRIMARY KEY (`id`),
  KEY `fk_tbl_sapo_group_rel_parent_code` (`parent_code`),
  KEY `fk_tbl_sapo_group_rel_child_code` (`child_code`),
  KEY `idx_group_rel_status_parent_child_code` (`status`,`parent_code`,`child_code`) USING BTREE,
  CONSTRAINT `fk_tbl_sapo_group_rel_child_code` FOREIGN KEY (`child_code`) REFERENCES `tbl_sapo_group` (`code`),
  CONSTRAINT `fk_tbl_sapo_group_rel_parent_code` FOREIGN KEY (`parent_code`) REFERENCES `tbl_sapo_group` (`code`)
) ENGINE=InnoDB AUTO_ Increment=8 default charset=utf8 comment='group relationship';

/*!40000 ALTER TABLE `tbl_sapo_group_rel` DISABLE KEYS */;
INSERT INTO `tbl_sapo_group_rel` (`id`, `create_time`, `last_update_time`, `parent_code`, `child_code`, `status`) VALUES
    (1, '2022-06-08 10:28:05.000', NULL, 'hotel', '7day', 2),
    (2, '2022-06-08 10:28:19.000', NULL, 'hotel', 'rujia', 2),
    (3, '2022-06-08 10:28:36.000', NULL, '7day', 'wujiaochang', 2),
    (4, '2022-06-08 22:49:00.000', NULL, 'hotel', 'huazhu', 2),
    (5, '2022-06-08 22:49:12.000', NULL, 'hotel', 'hanting', 2),
    (6, '2022-06-09 09:47:58.000', NULL, 'hanting', 'hongkou', 2),
    (7, '2022-06-09 09:48:40.000', NULL, 'hanting', 'jiangwan', 2);
/*!40000 ALTER TABLE `tbl_sapo_group_rel` ENABLE KEYS */;

/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
/*!40014 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS, 1) */;
/*!40101 SET [email protected]_CHARACTER_SET_CLIENT */;
/*!40111 SET SQL_NOTES=IFNULL(@OLD_SQL_NOTES, 1) */;

Table building and test data

 

 

 

 

Implementation scheme of iteration and combination mode of general tree structureImplementation scheme of iteration and combination mode of general tree structure

public abstract class GroupComponent {

   

    public abstract Iterator createIterator();
    
    //How many spaces are left in the first line of characters
    protected abstract String printTreeStr(int i);
    
    public abstract String getName();
    
    public  String printTreeStr() {
        return printTreeStr(0);
    };
    
    
    
    //Print tree solution structure
    protected String padding_n(int n) {
        StringBuffer space = new StringBuffer("");
        for (int i = 0; i < n; i++) {
            space.append("-");
        }
        space.append("|");
        return space.toString();
    }
    
 //Recursively obtain tree structure
    public static GroupComponent getTree(String groupCode) {
        //Get general Dao
        CommonDao dao = SpringUtil.getBean(CommonDao.class);
        //Group details model class in database
        SapoGroup sapoGroup = dao.getObj(SapoGroup.getInstance().setCode(groupCode));

        //Query all sons of this node
        List childList = dao.getObjListWithEmpty(SapoGroupRel.getInstance().setParentCode(groupCode));

        //If there is no child node, directly create a new leaf node and return
        if (childList == null || childList.size() == 0) {
            LeafGroup leafGroup = new LeafGroup();
            leafGroup.setLeafGroup(sapoGroup);
            return leafGroup;
        } else {
            //If there are child nodes
            Group group = new Group();
            group.setGroupDetail(sapoGroup);
            for (SapoGroupRel rel : childList) {
                //Recursively get the previous node
                GroupComponent child = getTree(rel.getChildCode());
                group.getList().add(child);
            }
            return group;
        }
    }
}

GroupComponent

Implementation scheme of iteration and combination mode of general tree structureImplementation scheme of iteration and combination mode of general tree structure

public class Group extends GroupComponent {

    Iterator iterator = null;

    public List list = new ArrayList();

    public SapoGroup groupDetail;

    public SapoGroup getGroupDetail() {
        return groupDetail;
    }

    public void setGroupDetail(SapoGroup groupDetail) {
        this.groupDetail = groupDetail;
    }

    /*
     *Print tree hierarchy
     */
    protected String printTreeStr(int i) {
        //Fields to be printed
        String waitPrintStr = this.groupDetail.getName();

        StringBuilder sb = new StringBuilder();
        sb.append(padding_n(i));
        sb.append(waitPrintStr);
        sb.append("\r\n");

        Iterator iterator = list.iterator();

        while (iterator.hasNext()) {
            GroupComponent next = iterator.next();
            //Recursive traversal
            String printTree = next.printTreeStr(i + 2);
            sb.append(printTree);
        }
        return sb.toString();
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    @Override
    public Iterator createIterator() {
        if (iterator == null) {
            iterator = new GroupIterator(list.iterator());
        }
        return iterator;
    }

    @Override
    public String getName() {

        return "list: " + groupDetail.getName();
    }

}

Group

Implementation scheme of iteration and combination mode of general tree structureImplementation scheme of iteration and combination mode of general tree structure

public class LeafGroup extends GroupComponent {

    private SapoGroup leafGroup;

    public SapoGroup getLeafGroup() {
        return leafGroup;
    }

    public void setLeafGroup(SapoGroup leafGroup) {
        this.leafGroup = leafGroup;
    }

    public Iterator createIterator() {
        return new NullIterator();
    }

    protected String printTreeStr(int i) {
        //Key fields
        String waitPrintStr = this.getLeafGroup().getName();
        return padding_n(i) + waitPrintStr + "\r\n";
    }

    /* (non-Javadoc)
     * @see cn.com.fmsh.nfcos.sapo.biz.testGroup.GroupComponent#getName()
     */
    @Override
    public String getName() {
       return "leaf: "+leafGroup.getName();
    }

}

LeafGroup

Implementation scheme of iteration and combination mode of general tree structureImplementation scheme of iteration and combination mode of general tree structure

public class GroupIterator implements Iterator {

    Stack> stack = new Stack>();

    public GroupIterator(Iterator iterator) {
        stack.push(iterator);
    }

    public boolean hasNext() {
        if (stack.isEmpty()) {
            return false;
        } else {
            Iterator iterator = stack.peek();
            if (!iterator.hasNext()) {
                stack.pop();
                return hasNext();
            } else {
                return true;
            }
        }

    }

    
    public GroupComponent next() {
       if(hasNext()) {
           Iterator iterator = stack.peek();
           GroupComponent next = iterator.next();
           stack.push(next.createIterator());
           return next;
       }else {
           return null;
       }        
    }

}

GroupIterator

Implementation scheme of iteration and combination mode of general tree structureImplementation scheme of iteration and combination mode of general tree structure

public class NullIterator implements Iterator {
   
    public GroupComponent next() {
        return null;
    }
  
    public boolean hasNext() {
        return false;
    }
   
    
}

NullIterator

 

Test the program, traverse the tree structure and print the tree structure.

@Test
    public void Test() {

        //Traversal with external iterators
        GroupComponent tree = Group.getTree("hotel");

        Iterator iterator = tree.createIterator();

        while (iterator.hasNext()) {
            GroupComponent next = iterator.next();
            //Todo traversal operation content

        }

        System.out.println ("---- print tree structure -----");
        
        //Print tree structure
        System.err.println(GroupComponent.getTree("hotel").printTreeStr());

    }

 

This article is from the blog Park, written by wanglifeng. Please indicate the original link if you reprint it:https://www.cnblogs.com/wanglifeng717/p/16363485.html

 

Recommended Today

Digital DP-902. Number combinations up to N

Problem Description Given an array digits of digits in non-decreasing order. A number you can write with digits[i] any number of times. For example, if digits = ['1','3','5'], we can write digits like '13', '551', and '1351315'. Returns the number of positive integers that can be generated that are less than or equal to the […]