GNU libavl 2.0.1
Author: Ben Pfaff

4 Binary Search Trees |

The previous chapter motivated the need for binary search trees. This chapter implements a table ADT backed by a binary search tree. Along the way, we'll see how binary search trees are constructed and manipulated in abstract terms as well as in concrete C code.

The library includes a header file <`bst.h` 24> and an implementation file <`bst.c` 25>, outlined below. We borrow most of the header file from the generic table headers designed a couple of chapters back, simply replacing tbl by bst, the prefix used in this table module.

    24. <bst.h 24> =
    <License 1>
    #ifndef BST_H
    #define BST_H 1

    #include <stddef.h>

    <Table types; tbl => bst 14>
    <BST maximum height 28>
    <BST table structure 27>
    <BST node structure 26>
    <BST traverser structure 61>
    <Table function prototypes; tbl => bst 15>
    <BST extra function prototypes 88>

    #endif /* bst.h */

    <Table assertion function control directives; tbl => bst 593>

    25. <bst.c 25> =
    <License 1>
    #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "bst.h"

    <BST operations 29>

Exercises:

1. What is the purpose of #ifndef `BST_H` ... #endif in <`bst.h` 24> above? [answer]

Sections:

- 4.1 Vocabulary
  - 4.1.1 Aside: Differing Definitions
- 4.2 Data Types
  - 4.2.1 Node Structure
  - 4.2.2 Tree Structure
  - 4.2.3 Maximum Height
- 4.3 Rotations
- 4.4 Operations
- 4.5 Creation
- 4.6 Search
- 4.7 Insertion
  - 4.7.1 Aside: Root Insertion
- 4.8 Deletion
  - 4.8.1 Aside: Deletion by Merging
- 4.9 Traversal
  - 4.9.1 Traversal by Recursion
  - 4.9.2 Traversal by Iteration
    - 4.9.2.1 Improving Convenience
  - 4.9.3 Better Iterative Traversal
    - 4.9.3.1 Starting at the Null Node
    - 4.9.3.2 Starting at the First Node
    - 4.9.3.3 Starting at the Last Node
    - 4.9.3.4 Starting at a Found Node
    - 4.9.3.5 Starting at an Inserted Node
    - 4.9.3.6 Initialization by Copying
    - 4.9.3.7 Advancing to the Next Node
    - 4.9.3.8 Backing Up to the Previous Node
    - 4.9.3.9 Getting the Current Item
    - 4.9.3.10 Replacing the Current Item
- 4.10 Copying
  - 4.10.1 Recursive Copying
  - 4.10.2 Iterative Copying
  - 4.10.3 Error Handling
- 4.11 Destruction
  - 4.11.1 Destruction by Rotation
  - 4.11.2 Aside: Recursive Destruction
  - 4.11.3 Aside: Iterative Destruction
- 4.12 Balance
  - 4.12.1 From Tree to Vine
  - 4.12.2 From Vine to Balanced Tree
    - 4.12.2.1 General Trees
    - 4.12.2.2 Implementation
    - 4.12.2.3 Implementing Compression
- 4.13 Aside: Joining BSTs
- 4.14 Testing
  - 4.14.1 Testing BSTs
    - 4.14.1.1 BST Verification
    - 4.14.1.2 Displaying BST Structures
  - 4.14.2 Test Set Generation
  - 4.14.3 Testing Overflow
  - 4.14.4 Memory Manager
  - 4.14.5 User Interaction
  - 4.14.6 Utility Functions
  - 4.14.7 Main Program
- 4.15 Additional Exercises

3.7 Dynamic Lists | Table of Contents | 4.1 Vocabulary