Name

Tree::Trek - Trek through a tree one character at a time.

Synopsis

Create a trekkable tree and trek through it:

my $n = node;

$n->put("aa") ->data = "AA";
$n->put("ab") ->data = "AB";
$n->put("ba") ->data = "BA";
$n->put("bb") ->data = "BB";
$n->put("aaa")->data = "AAA";

is_deeply [map {[$_->key, $_->data]} $n->traverse],
 [["aa",  "AA"],
  ["aaa", "AAA"],
  ["ab",  "AB"],
  ["ba",  "BA"],
  ["bb",  "BB"]];

Description

Trek through a tree one character at a time.

Version "20210424".

The following sections describe the methods in each functional area of this module. For an alphabetic listing of all methods by name see Index.

Tree::Trek

Methods to create a trekkable tree.

node($parent)

Create a new node

   Parameter  Description
1  $parent    Optional parent

Example:

if (1)

 {my $n = node;  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲

  $n->put("aa")->data = "AA";
  $n->put("ab")->data = "AB";
  $n->put("ba")->data = "BA";
  $n->put("bb")->data = "BB";
  $n->put("aaa")->data = "AAA";
  is_deeply $n->count, 5;

  is_deeply $n->find("aa") ->data, "AA";
  is_deeply $n->find("ab") ->data, "AB";
  is_deeply $n->find("ba") ->data, "BA";
  is_deeply $n->find("bb") ->data, "BB";
  is_deeply $n->find("aaa")->data, "AAA";

  is_deeply [map {[$_->key, $_->data]} $n->traverse],
   [["aa",  "AA"],
    ["aaa", "AAA"],
    ["ab",  "AB"],
    ["ba",  "BA"],
    ["bb",  "BB"]];

  ok  $n->find("a");
  ok !$n->find("a")->data;
  ok  $n->find("b");
  ok !$n->find("b")->data;
  ok !$n->find("c");

  ok $n->find("aa")->delete;  ok  $n->find("aa");  is_deeply $n->count, 4;
  ok $n->find("ab")->delete;  ok !$n->find("ab");  is_deeply $n->count, 3;
  ok $n->find("ba")->delete;  ok !$n->find("ba");  is_deeply $n->count, 2;
  ok $n->find("bb")->delete;  ok !$n->find("bb");  is_deeply $n->count, 1;

  ok  $n->find("a");
  ok !$n->find("b");

  ok $n->find("aaa")->delete; ok !$n->find("aaa"); is_deeply $n->count, 0;
  ok  !$n->find("a");
 }

put($tree, $key)

Add a key to the tree

   Parameter  Description
1  $tree      Tree
2  $key       Key

Example:

if (1)
 {my $n = node;

  $n->put("aa")->data = "AA";  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  $n->put("ab")->data = "AB";  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  $n->put("ba")->data = "BA";  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  $n->put("bb")->data = "BB";  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  $n->put("aaa")->data = "AAA";  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲

  is_deeply $n->count, 5;

  is_deeply $n->find("aa") ->data, "AA";
  is_deeply $n->find("ab") ->data, "AB";
  is_deeply $n->find("ba") ->data, "BA";
  is_deeply $n->find("bb") ->data, "BB";
  is_deeply $n->find("aaa")->data, "AAA";

  is_deeply [map {[$_->key, $_->data]} $n->traverse],
   [["aa",  "AA"],
    ["aaa", "AAA"],
    ["ab",  "AB"],
    ["ba",  "BA"],
    ["bb",  "BB"]];

  ok  $n->find("a");
  ok !$n->find("a")->data;
  ok  $n->find("b");
  ok !$n->find("b")->data;
  ok !$n->find("c");

  ok $n->find("aa")->delete;  ok  $n->find("aa");  is_deeply $n->count, 4;
  ok $n->find("ab")->delete;  ok !$n->find("ab");  is_deeply $n->count, 3;
  ok $n->find("ba")->delete;  ok !$n->find("ba");  is_deeply $n->count, 2;
  ok $n->find("bb")->delete;  ok !$n->find("bb");  is_deeply $n->count, 1;

  ok  $n->find("a");
  ok !$n->find("b");

  ok $n->find("aaa")->delete; ok !$n->find("aaa"); is_deeply $n->count, 0;
  ok  !$n->find("a");
 }

key($node)

Return the key of a node

   Parameter  Description
1  $node      Node

Example:

if (1)
 {my $n = node;
  $n->put("aa")->data = "AA";
  $n->put("ab")->data = "AB";
  $n->put("ba")->data = "BA";
  $n->put("bb")->data = "BB";
  $n->put("aaa")->data = "AAA";
  is_deeply $n->count, 5;

  is_deeply $n->find("aa") ->data, "AA";
  is_deeply $n->find("ab") ->data, "AB";
  is_deeply $n->find("ba") ->data, "BA";
  is_deeply $n->find("bb") ->data, "BB";
  is_deeply $n->find("aaa")->data, "AAA";


  is_deeply [map {[$_->key, $_->data]} $n->traverse],  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲

   [["aa",  "AA"],
    ["aaa", "AAA"],
    ["ab",  "AB"],
    ["ba",  "BA"],
    ["bb",  "BB"]];

  ok  $n->find("a");
  ok !$n->find("a")->data;
  ok  $n->find("b");
  ok !$n->find("b")->data;
  ok !$n->find("c");

  ok $n->find("aa")->delete;  ok  $n->find("aa");  is_deeply $n->count, 4;
  ok $n->find("ab")->delete;  ok !$n->find("ab");  is_deeply $n->count, 3;
  ok $n->find("ba")->delete;  ok !$n->find("ba");  is_deeply $n->count, 2;
  ok $n->find("bb")->delete;  ok !$n->find("bb");  is_deeply $n->count, 1;

  ok  $n->find("a");
  ok !$n->find("b");

  ok $n->find("aaa")->delete; ok !$n->find("aaa"); is_deeply $n->count, 0;
  ok  !$n->find("a");
 }

find($tree, $key)

Find a key in a tree - return its node if such a node exists else undef

   Parameter  Description
1  $tree      Tree
2  $key       Key

Example:

if (1)
 {my $n = node;
  $n->put("aa")->data = "AA";
  $n->put("ab")->data = "AB";
  $n->put("ba")->data = "BA";
  $n->put("bb")->data = "BB";
  $n->put("aaa")->data = "AAA";
  is_deeply $n->count, 5;


  is_deeply $n->find("aa") ->data, "AA";  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  is_deeply $n->find("ab") ->data, "AB";  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  is_deeply $n->find("ba") ->data, "BA";  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  is_deeply $n->find("bb") ->data, "BB";  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  is_deeply $n->find("aaa")->data, "AAA";  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  is_deeply [map {[$_->key, $_->data]} $n->traverse],
   [["aa",  "AA"],
    ["aaa", "AAA"],
    ["ab",  "AB"],
    ["ba",  "BA"],
    ["bb",  "BB"]];


  ok  $n->find("a");  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  ok !$n->find("a")->data;  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  ok  $n->find("b");  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  ok !$n->find("b")->data;  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  ok !$n->find("c");  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲



  ok $n->find("aa")->delete;  ok  $n->find("aa");  is_deeply $n->count, 4;  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  ok $n->find("ab")->delete;  ok !$n->find("ab");  is_deeply $n->count, 3;  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  ok $n->find("ba")->delete;  ok !$n->find("ba");  is_deeply $n->count, 2;  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  ok $n->find("bb")->delete;  ok !$n->find("bb");  is_deeply $n->count, 1;  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲



  ok  $n->find("a");  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  ok !$n->find("b");  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲



  ok $n->find("aaa")->delete; ok !$n->find("aaa"); is_deeply $n->count, 0;  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  ok  !$n->find("a");  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲

 }

delete($node)

Remove a node from a tree

   Parameter  Description
1  $node      Node to be removed

Example:

if (1)
 {my $n = node;
  $n->put("aa")->data = "AA";
  $n->put("ab")->data = "AB";
  $n->put("ba")->data = "BA";
  $n->put("bb")->data = "BB";
  $n->put("aaa")->data = "AAA";
  is_deeply $n->count, 5;

  is_deeply $n->find("aa") ->data, "AA";
  is_deeply $n->find("ab") ->data, "AB";
  is_deeply $n->find("ba") ->data, "BA";
  is_deeply $n->find("bb") ->data, "BB";
  is_deeply $n->find("aaa")->data, "AAA";

  is_deeply [map {[$_->key, $_->data]} $n->traverse],
   [["aa",  "AA"],
    ["aaa", "AAA"],
    ["ab",  "AB"],
    ["ba",  "BA"],
    ["bb",  "BB"]];

  ok  $n->find("a");
  ok !$n->find("a")->data;
  ok  $n->find("b");
  ok !$n->find("b")->data;
  ok !$n->find("c");


  ok $n->find("aa")->delete;  ok  $n->find("aa");  is_deeply $n->count, 4;  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  ok $n->find("ab")->delete;  ok !$n->find("ab");  is_deeply $n->count, 3;  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  ok $n->find("ba")->delete;  ok !$n->find("ba");  is_deeply $n->count, 2;  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  ok $n->find("bb")->delete;  ok !$n->find("bb");  is_deeply $n->count, 1;  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  ok  $n->find("a");
  ok !$n->find("b");


  ok $n->find("aaa")->delete; ok !$n->find("aaa"); is_deeply $n->count, 0;  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲

  ok  !$n->find("a");
 }

count($node)

Count the nodes addressed in the specified tree

   Parameter  Description
1  $node      Node to be counted from

Example:

if (1)
 {my $n = node;
  $n->put("aa")->data = "AA";
  $n->put("ab")->data = "AB";
  $n->put("ba")->data = "BA";
  $n->put("bb")->data = "BB";
  $n->put("aaa")->data = "AAA";

  is_deeply $n->count, 5;  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  is_deeply $n->find("aa") ->data, "AA";
  is_deeply $n->find("ab") ->data, "AB";
  is_deeply $n->find("ba") ->data, "BA";
  is_deeply $n->find("bb") ->data, "BB";
  is_deeply $n->find("aaa")->data, "AAA";

  is_deeply [map {[$_->key, $_->data]} $n->traverse],
   [["aa",  "AA"],
    ["aaa", "AAA"],
    ["ab",  "AB"],
    ["ba",  "BA"],
    ["bb",  "BB"]];

  ok  $n->find("a");
  ok !$n->find("a")->data;
  ok  $n->find("b");
  ok !$n->find("b")->data;
  ok !$n->find("c");


  ok $n->find("aa")->delete;  ok  $n->find("aa");  is_deeply $n->count, 4;  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  ok $n->find("ab")->delete;  ok !$n->find("ab");  is_deeply $n->count, 3;  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  ok $n->find("ba")->delete;  ok !$n->find("ba");  is_deeply $n->count, 2;  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  ok $n->find("bb")->delete;  ok !$n->find("bb");  is_deeply $n->count, 1;  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


  ok  $n->find("a");
  ok !$n->find("b");


  ok $n->find("aaa")->delete; ok !$n->find("aaa"); is_deeply $n->count, 0;  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲

  ok  !$n->find("a");
 }

traverse($node)

Traverse a tree returning an array of nodes

   Parameter  Description
1  $node      Node to be counted from

Example:

if (1)
 {my $n = node;
  $n->put("aa")->data = "AA";
  $n->put("ab")->data = "AB";
  $n->put("ba")->data = "BA";
  $n->put("bb")->data = "BB";
  $n->put("aaa")->data = "AAA";
  is_deeply $n->count, 5;

  is_deeply $n->find("aa") ->data, "AA";
  is_deeply $n->find("ab") ->data, "AB";
  is_deeply $n->find("ba") ->data, "BA";
  is_deeply $n->find("bb") ->data, "BB";
  is_deeply $n->find("aaa")->data, "AAA";


  is_deeply [map {[$_->key, $_->data]} $n->traverse],  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲

   [["aa",  "AA"],
    ["aaa", "AAA"],
    ["ab",  "AB"],
    ["ba",  "BA"],
    ["bb",  "BB"]];

  ok  $n->find("a");
  ok !$n->find("a")->data;
  ok  $n->find("b");
  ok !$n->find("b")->data;
  ok !$n->find("c");

  ok $n->find("aa")->delete;  ok  $n->find("aa");  is_deeply $n->count, 4;
  ok $n->find("ab")->delete;  ok !$n->find("ab");  is_deeply $n->count, 3;
  ok $n->find("ba")->delete;  ok !$n->find("ba");  is_deeply $n->count, 2;
  ok $n->find("bb")->delete;  ok !$n->find("bb");  is_deeply $n->count, 1;

  ok  $n->find("a");
  ok !$n->find("b");

  ok $n->find("aaa")->delete; ok !$n->find("aaa"); is_deeply $n->count, 0;
  ok  !$n->find("a");
 }

Index

1 count - Count the nodes addressed in the specified tree

2 delete - Remove a node from a tree

3 find - Find a key in a tree - return its node if such a node exists else undef

4 key - Return the key of a node

5 node - Create a new node

6 put - Add a key to the tree

7 traverse - Traverse a tree returning an array of nodes

Installation

This module is written in 100% Pure Perl and, thus, it is easy to read, comprehend, use, modify and install via cpan:

sudo cpan install Tree::Trek

Author

philiprbrenan@gmail.com

http://www.appaapps.com

Copyright

Copyright (c) 2016-2021 Philip R Brenan.

This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.