Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

adt::Tree

Abstract data type "Tree"

MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

Syntax

adt::Tree(tree)

Description

adt::Tree implements the abstract data type "Tree".

A tree must be given as a special MuPAD® list. The first object of the list is the root of the tree. All further objects are leaves or subtrees of the tree. A subtree is again a special list (as described), and any other MuPAD object will be interpreted as leaf of the tree (see Example 1).

A tree can be used to display data in tree structure using the function output::tree (or the method "print" of a tree). The nodes and leaves of the tree will be printed by MuPAD when the tree will be displayed.

A tree can also be used as datatype to keep and handle any MuPAD data.

    Note:   The methods of all abstract data types must be called especially and will result changing the object itself as side effect.

T := adt::Tree([_plus, 3, 4, [_mult, 5, 3], 1]) builds a tree and assigns it to the variable T.

Every tree will be displayed as Tree followed by a number. This name is generated by genident.

To display the content of a tree, the function expose or the method "print" of the tree itself must be used.

    Note:   All following methods changes the value of T itself. A new assignment to the variable (in this example T) is not necessary, in contrast to all other MuPAD functions and data types.

The methods nops, op, expr, print, indent, chars are now available for handling with trees.

Examples

Example 1

Creating a simple tree with only two leaves. To access and display a tree it must be assigned to a variable:

T := adt::Tree(["ROOT", "LEFT", "RIGHT"])

The tree will only be printed by its name. To display the tree, the function expose or the method "print" of the tree must be used:

T::print()
ROOT
|
+-- LEFT
|
`-- RIGHT
expose(T)
ROOT
|
+-- LEFT
|
`-- RIGHT

The next tree contains two subtrees as leaves:

T := adt::Tree(["ROOT", ["LROOT", "LLEFT", "LRIGHT"],
                        ["RROOT", "RLEFT", "RRIGHT"]]):
T::print()
ROOT
|
+-- LROOT
|   |
|   +-- LLEFT
|   |
|   `-- LRIGHT
|
`-- RROOT
    |
    +-- RLEFT
    |
    `-- RRIGHT

Example 2

Get the operands of a tree: Also a subtree can be an operand:

T := adt::Tree(["ROOT", ["LROOT", "LLEFT", "LRIGHT"],
                        "MIDDLE",
                        ["RROOT", "RLEFT", "RRIGHT"]]):
T::op()

Use expose to display subtrees:

map(%, expose)
LROOT                 RROOT
|                     |
+-- LLEFT , "MIDDLE", +-- RLEFT
|                     |
`-- LRIGHT            `-- RRIGHT

Get all operands including the root:

T::op(0..T::nops())

Access to various operands:

T::op(0);
T::op(2..3);
T::op([1, 2])

Example 3

The default characters are ["|", "+", "-", „", " "]:

T := adt::Tree(["ROOT", ["LROOT", "LLEFT", "LRIGHT"],
                        ["RROOT", "RLEFT", "RRIGHT"]]):
T::print()
ROOT
|
+-- LROOT
|   |
|   +-- LLEFT
|   |
|   `-- LRIGHT
|
`-- RROOT
    |
    +-- RLEFT
    |
    `-- RRIGHT

The characters can be changed:

T::chars(["|", "|", "_", "|", " "]):
T::print()
ROOT
|
|__ LROOT
|   |
|   |__ LLEFT
|   |
|   |__ LRIGHT
|
|__ RROOT
    |
    |__ RLEFT
    |
    |__ RRIGHT

Example 4

A tree visualizes the structure of an expression:

T:= adt::Tree([_plus, [_power, [sin, x], 2], [_power, [cos, x], 2]]):
T::print()
_plus
|
+-- _power
|   |
|   +-- sin
|   |   |
|   |   `-- x
|   |
|   `-- 2
|
`-- _power
    |
    +-- cos
    |   |
    |   `-- x
    |
    `-- 2

A tree can be converted to a MuPAD expression:

T::expr(), simplify(T::expr())

Parameters

tree

The tree, given as a special list (see details)

Methods

expand all

nops()

In this example T has 4 operands, the numbers 3, 4, 1 and the subtree adt::Tree([_mult, 5, 3]).

op(<n>)

T::op(n) returns the specified operands of the tree. n can be a number between 0 and T::nops() (0 gives the root of the tree), a sequence i..j (to return the ith to jth operand), or a list to specify operands of subtrees (exactly as for the kernel function op). T::op() returns all operands except the 0-th as expression sequence. See Example 2.

expr()

print()

indent(<n>)

chars(<list>)

See Also

MuPAD Functions

Was this topic helpful?