Trees

Creating a tree

First, we will make some simple rectangles to represent the nodes of a tree. Then the following code creates a tree attached to the master root, with one parent and three children, and lays out the tree.

set root [.pad create rectangle 0 0 50 50 -fill white]
set child1 [.pad create rectangle 0 0 50 50 -fill red]
set child2 [.pad create rectangle 0 0 50 50 -fill green]
set child3 [.pad create rectangle 0 0 50 50 -fill blue]

  # Create the dynamic tree data nodes
.pad tree create $root 
.pad tree create $child1 
.pad tree create $child2 
.pad tree create $child3
  # Create the tree structure 
.pad tree addnode $child1 $root 
.pad tree addnode $child2 $root 
.pad tree addnode $child3 $root
  # Layout the tree
.pad tree layout $root



The tree structure can be accessed with the getparent and getchildren commands. These return the ids of the appropriate members of the hierarchy.

.pad tree getparent $child1 
 => Returns $root
.pad tree getchildren $root
 => Returns a list of child1 child2 child3
.pad tree getchildren $child1
 => Returns an empty string


Pad++ Programmer's Guide - 10 JUN 1996

Generated with Harlequin WebMaker

Web Accessibility