2

I want to create an AVL tree in tcl. I read that in tcl we cannot have structures that contain references of themselves like in C.

struct tree{
    tree *treelink;
}

I want to be able to create the trees without using OTcl. Is it possible and how?

Monty Swanson
  • 695
  • 16
  • 41

1 Answers1

4

You're in luck. I wrote one for Rosetta Code a week or so ago. The code is long enough that I'm not repeating it here, but feel free to take it and adapt. I wouldn't use it in production though. Arrays and dictionaries provide the same key abstract operations (insertion, deletion, lookup, update, enumeration) and work just fine with their built-in implementation.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • The license on that code is formally GFDL due to it being on RC, but I give permission to consider it to be under the Tcl-flavor BSD license as well. – Donal Fellows Jun 04 '13 at 08:33
  • Oh it was you who wrote that code. Thanks a bunch. You are awesome!! But the problem i had with that was with the "package require TclOO" which gives the error "can't find package TclOO" in ubuntu. I havent been quite able to install it correctly. – Monty Swanson Jun 04 '13 at 09:01
  • @Chhama The code should be relatively easy to adapt to Itcl, XOTcl or Snit; it's not really using anything very fancy (the fanciest bit? the special root `nil` sentinel, which is just a particular object). – Donal Fellows Jun 04 '13 at 09:25
  • Thanks, I installed tcl 8.6 and the code runs successfully with it. I'll have to reinstall ns2 with tcl 8.6 and I think I'll be OK. Thanks again. – Monty Swanson Jun 04 '13 at 09:33