Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Interfaces and Collection Classes in Delphi


December 2001/Interfaces and Collection Classes in Delphi

Example 5: Constructing the tree view representation of the red-black tree

// Debug a red-black tree by showing the internal tree structure.
// Visibly display an image for red and black nodes. To help color-blind
// users, Red nodes have a Round icon and Black nodes have a Box icon.

// Let the debug form access protected methods and properties.
type
  TExposeTree = class(TRbTree)
  end;
  TExposeNode = class(TRbNode)
  end;

// Refresh the tree display after changing the tree.
procedure TTreeForm.RefreshTree;
  procedure WalkTree(RbNode: TRbNode; ViewNode: TTreeNode);
  var
    Item: ICollectible;
    Assoc: IAssociation;
  begin
    if RbNode <> TExposeTree(Tree).Leaf then
    begin
      // Get the string item from the node. If the collection is a map,
      // the node holds an association, and the string is the key.
      Item := TExposeNode(RbNode).Item;
      if Item.QueryInterface(IAssociation, Assoc) = S_OK then
        Assoc.GetKey(Item);
      ViewNode := TreeView.Items.AddChild(ViewNode, (Item as IString).Value);
      if TExposeNode(RbNode).IsRed then
        ViewNode.ImageIndex := 0
      else
        ViewNode.ImageIndex := 1;
      ViewNode.SelectedIndex := ViewNode.ImageIndex;

      WalkTree(TExposeNode(RbNode).Left, ViewNode);
      WalkTree(TExposeNode(RbNode).Right, ViewNode);
    end;
  end;
  
begin
  TreeView.Items.BeginUpdate;
  try
    TreeView.Items.Clear;
    WalkTree(TExposeTree(Tree).Root, nil);
    TreeView.FullExpand;
  finally
    TreeView.Items.EndUpdate;
  end;
end;

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.