home *** CD-ROM | disk | FTP | other *** search
- Threaded-Height-Balanced Trees Library
- TAVL-trees for short
-
- Copyright (c) 1991 Bert C. Hughes
-
- The source files on this disk are an implementation of a hybrid data
- structure, the "threaded height-balanced" tree, or tavl-tree. The AVL
- in "tavl" stands for Adelson-Velskii-Landis, inventors of the height-balanced
- tree in 1962. A.J.Perlis and C.Thornton developed the idea of threaded
- binary trees as early as 1960. I simply combined these two well-known
- concepts to develop the TAVLtree library.
-
- The height-balanced tree, or AVL-tree, is an improvement on the traditional
- binary tree (not to be confused with B-trees). As items are inserted into
- the traditional binary tree, the structure of the tree may degrade into some-
- thing resembling a linked list, so that retrieval performance suffers. AVL
- trees right this wrong by rebalancing the tree as necessary whenever items are
- inserted or deleted.
-
- With traditional binary trees and AVL trees it is not efficient to move from
- any given node to its successor or predecessor. To find the successor of a
- given node in a binary or AVL tree, you must walk through the entire tree
- "in-order" until you arrive at the node whose successor you wish to find,
- then the next "in-order" node is the desired successor. Finding the
- predecessor is done similarly. Threaded binary trees solve this problem
- by replacing the nil links in leaf & half-leaf nodes with links to the
- node's "inorder" successor (or predecessor or both). "Threads" are
- distinguished from links with an additional 2 bit field in each node; one
- bit for each child link. With this additional information, the procedure
- for moving to a successor node becomes simple, and does not require a stack
- or recursion:
-
- function node_succ(P: node_ptr): node_ptr;
- variable Q: node_ptr;
- begin
- 1: Q := RightChild(P); /* if P's right ptr is a thread all done */
- 2: if (Is_A_Link(RightChild(P)))
- 3: while (Is_A_Link(LeftChild(Q))) /* get leftmost most of */
- 4: Q := LeftChild(Q); /* P's right subtree - */
- 5: node_succ := Q; /* that is P's successor */
- end;
-
- A rough order analysis is as follows:
-
- First, assume the tree is perfectly balanced, so that 1/2 of all
- nodes are "leaf" nodes, 1/4 are at the next level up, 1/8 in the
- next level, etc. This is approximately true with height balanced
- trees. Statement (2) is always executed, and 1/2 the time state-
- ment (2) is false so we are finished. If not, the "while" loop
- is entered, and 1/4 the time it is tested once before success,
- 1/8 the time twice, etc. So the order (big O) is:
-
- Sum = (1 * (1/2)) + (2 * (1/4)) + (3 * (1/8)) + ... + (n * (1/2)power(n))
-
- or Sum = 2, for O(1), which is same order as an ordinary linked list.
-
-
- FILES ON THIS DISK:
- -------------------
-
- documentation
- -------------
-
- TAVLTREE.DOC Reference for TAVL library functions
- READ.ME This file! General info
- HISTORY.TXT History of revisions.
- TAVL_TCC.MAK Sample "make" file for compiling libraries with Turbo C
- TAVL_BCC.MAK Sample "make" file for compiling libraries with Borland C++
-
- sample programs
- ---------------
-
- EXAMPLE1.C
- EXAMPLE2.C
- EXAMPLE3.C
- EXAMPLE4.C
- EXAMPLE5.C
- SORTX.C A working replacement for MS-DOS "Sort". Much faster,
- and it won't bomb on large input files.
-
- data files for the examples:
-
- WORDLIST | word list data file for example1, example2
- SHORTLST | word list data file for example3
- EMPLDATA | employee data file for example4, example5
-
-
- TAVL library: all library files are named TAVL*.C or TAVL*.H
- -------------
-
- public:
-
- TAVLTREE.H Include this header file in all your applications;
- contains prototypes for all TAVL library functions.
-
- TAVLINIT.C "tavl_init"
- TAVL_INS.C "tavl_insert"
- TAVL_DEL.C "tavl_delete"
- TAVLFIND.C "tavl_find"
- TAVL_RST.C "tavl_reset"
- TAVLSUCC.C "tavl_succ"
- TAVLPRED.C "tavl_pred"
- TAVLFREE.C "tavl_destroy"
- TAVLDALL.C "tavl_delete_all"
- TAVL_SDT.C "tavl_setdata"
- TAVL_GDT.C "tavl_getdata"
-
- private:
-
- TAVLPRIV.H Private header for compiling the library; do NOT
- include this in your application.
-
- TAVLREBL.C "rebalance_tavl" - private routine used by
- "tavl_insert" and "tavl_delete"
-
-
- REGISTRATION:
- -------------
-
- No registration is required for any use whatsoever of the
- TAVLTREE library. I, Bert C. Hughes, have donated this
- package to the PUBLIC DOMAIN.
-
- However, I invite and welcome comments and critisms. Send same to:
-
- Bert Hughes
- J&B Consulting Services
- 200 N. Saratoga
- St. Paul, MN 55104
-
- I may also be contacted via Compuserve Mail. My ID is 71211,577.
-
-
- References & Bibliography
- -------------------------
- Holub, Allen "An AVL Tree Database Package",
- C Chest Column
- Aug. 86, Dr.Dobbs Journal
-
- Horowitz & Sahni Fundamentals of Data Structures, see Chap. 5, Sec. 6:
- Computer Science Press Threaded Binary Trees
- and Chap. 9, Sec. 2:
- Dynamic Tree Tables
- Jarvis, Bob "Balanced Binary Trees in C++",
- Jan. 91, C User's Journal
-
- Mathews, James "Threaded Binary Trees"
- Mar. 88, Dr.Dobbs Journal