33B. Trees in C++


The Node and Tree types

The following types are suitable for representing a tree that has an integer stored at each node. An object of type Node represents one node in a binary tree. It holds an integer item, a pointer to the left subtree and a pointer to the right subtree. Those pointers can be NULL to indicate an empty subtree.

Note that type Tree is identical to type Node*. An empty tree is represented by a NULL pointer.

  struct Node
  {
    int   item;      // Information at this node
    Node* left;      // The left subtree
    Node* right;     // The right subtree

    Node(int it, Node* lft, Node* rgt)
    {
      item  = it;
      left  = lft;
      right = rgt;
    }
  };

  typedef Node* Tree;
  typedef const Node* ConstTree;