36B. Trees in C++


The Node and Tree types

The following types Node and Tree 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 three pieces of information: an integer item, a pointer to its left subtree and a pointer to its right subtree. Type Tree is equivalent to Node*; a null pointer represents an empty tree.

  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;