回 帖 发 新 帖 刷新版面

主题:Treeview结构问题

如何将Treeview存成数据结构中的二叉数形式。 
  希望能有具体的实现过程,最好能有源代码 

注:每个节点最多有2个孩子,就象通常意义上的二叉树那样。

回复列表 (共4个回复)

沙发

unit MainFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, ComCtrls, ImgList, StdCtrls;

type
   PNodedata=^TNodeData; //需要申明
    TNodeData = record
    id:integer;
    nodetype: integer;
    end;
  TMainForm = class(TForm)
    ADOQuery1: TADOQuery;
    TreeView1: TTreeView;
    ImageList1: TImageList;
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure TreeView1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;
implementation

uses Linkbank;

{$R *.dfm}

板凳

procedure TMainForm.FormShow(Sender: TObject);
var
  RootNode, FirstDeptNode,SecondDeptNode,ThreeDeptNode: TTreeNode;
  PFirstDeptdata,PSecondDeptdata,ThreeDeptdata:PNodedata;
  FirstDeptName,SecondDeptname,ThreeDeptname:string;  
  ado1,ado2,ado3:Tadoquery;
begin
  treeview1.Items.Clear;
  RootNode:=treeview1.Items.Add(nil,'我的树形结构');
  RootNode.ImageIndex := 0;
  RootNode.SelectedIndex := 0;
  ado1:=Tadoquery.Create(self);
  ado1.Connection:=dataform.conn;
  ado1.Close;
  ado1.SQL.Clear;
  ado1.SQL.Add('select * from tt'); //表结构为tt1 int,tt2 varchar;
  ado1.Open;
  ado1.First;
  while not ado1.Eof do
     begin
        PFirstDeptdata := new(PNodedata);
        PFirstDeptdata.id := ado1.fieldByName('tt1').AsInteger;
        PFirstDeptdata.nodetype := 0;
        FirstDeptName := ado1.FieldByName('tt2').AsString;
        FirstDeptNode := treeview1.Items.AddChildObject(RootNode, FirstDeptName,PFirstDeptdata);//注意展开时需要改成上一级的树的节点。
        ado1.Next;
            ado2:=Tadoquery.Create(self);
            ado2.Connection:=dataform.conn;
            ado2.Close;
            ado2.SQL.Clear;
            ado2.SQL.Add('select * from bb where bb1=:bb1');//表结构为:yy1 int,yy2 int,yy3 varchar;tt1与yy2关联。
            ado2.Parameters.ParamByName('bb1').Value:=PFirstDeptdata.id ;
            ado2.Open;
            ado2.First;
            while not ado2.Eof do

3 楼

begin
                      if ado2.FieldByName('bb1').AsInteger=PFirstDeptdata.id then
                              begin
                                     PSecondDeptdata := new(PNodedata);
                                     PSecondDeptdata.id := ado2.fieldByName('bb2').AsInteger;
                                     PSecondDeptdata.nodetype := 1;
                                     SecondDeptName := ado2.FieldByName('bb3').AsString;
                                     SecondDeptNode := treeview1.Items.AddChildObject(FirstDeptNode,SecondDeptName, PSecondDeptdata);
                                     FirstDeptNode.ImageIndex := 1;
                                     FirstDeptNode.SelectedIndex := 1;
                                     ado2.Next;
                                       ado3:=Tadoquery.Create(self);
                                       ado3.Connection:=dataform.conn;
                                       ado3.Close;
                                       ado3.SQL.Clear;
                                       ado3.SQL.Add('select * from ss where ss0=:ss0 and ss1=:ss1');
                                       ado3.Parameters.ParamByName('ss0').Value :=PFirstDeptdata.id ;
                                       ado3.Parameters.ParamByName('ss1').Value :=PSecondDeptdata.id ;
                                       ado3.Open;
                                       ado3.First;
                                       while not ado3.Eof do
                                          begin
                                            if (ado3.FieldByName('ss0').AsInteger=PFirstDeptdata.id) and (ado3.FieldByName'ss1').AsInteger=PSecondDeptdata.id) then
                                               begin
                                                ThreeDeptdata:= new(PNodedata) ;
                                                ThreeDeptdata.id:=ado3.fieldByName('ss2').AsInteger;
                                                ThreeDeptdata.nodetype := 2;
                                                ThreeDeptName := ado3.FieldByName('ss3').AsString;
                                                ThreeDeptNode := treeview1.Items.AddChildObject(SecondDeptNode,ThreeDeptName, ThreeDeptdata);
                                                ado3.Next;
                                               end;

                                          end;
                               end ;
                      end ;
            end ;
     RootNode.Expanded := true; //设置根目录默认打开
  end;

4 楼

首先感谢 lishan200012 的回答
   但是我问的就是Treeview的结构转成二叉链的形式,象以下的那样,也没有跟数据库连接什么的
type
   PNodedata=^TNodeData; 
    TNodeData = record
    data:integer;
    lch,rch;tnodedata;
    end;

我来回复

您尚未登录,请登录后再回复。点此登录或注册