• 数据集+树的一种最简单高效的算法


    此法可用于设计功能模块树,用户权限树,类别树以及一切需要TREE来表示之层级关系的。。。

    此法只需要遍历一次数据集即生成了整棵树。

    读取数据生成树的算法:

    procedure CreateTree(DataSet: TADOQuery; Tree: TTreeView;
      const table, aid, aname: string; idname: Boolean);
    const
      ID_DEPT = 2;
    var
      nLevel: Integer;
      pNodes: array[0..1023] of TTreeNode;
      lpID, lpName, s: string;
    begin
      if DataSet = nil then Exit;
      if Tree = nil then Exit;
      if table = '' then Exit;
      pNodes[0] := nil;
      Tree.Items.Clear;
      with DataSet do
      begin
        Close;
        SQL.Clear;
        s := Format('select %s, %s from %s order by %s', [aid, aname, table, aid]);
        SQL.Text := s;
        Open;
        if IsEmpty then Exit;
        First;
        while not Eof do
        begin
          lpID := FieldByName(aid).AsString;
          lpName := FieldByName(aname).AsString;
          nLevel := Length(lpID) div ID_DEPT;
          if not idname then
            pNodes[nLevel] := Tree.Items.AddChild(pNodes[nLevel - 1], lpName)
          else
            pNodes[nLevel] := Tree.Items.AddChild(pNodes[nLevel - 1],
              lpID + '-' + lpName);
          Next;
        end;
      end;
    end;

    数据表设计样式,ID字段升序:

    生成树
  • 相关阅读:
    Kafka 配置
    Zookeeper的Watcher机制
    Ubuntu18.04下希捷移动硬盘Seagate Backup Plus读写慢
    Spring Boot 使用Jar打包发布, 并使用 Embedded Jetty/Tomcat 容器
    再谈C#委托与事件
    C#委托和事件例析
    PHP:session无法使用
    C++:实现类似MFC的IsKindOf功能
    C++:复制构造函数
    C++:运算符重载
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/2940688.html
Copyright © 2020-2023  润新知