• Embed Doc
  • Readcast
  • Collections
  • 1
    CommentGo Back
Download

The curiosity of our surroundings is the constant force that drives humanity forward. In an effort to
better ourselves and our future we study the world we live in. Our own inventions define the way in
which we lead our lives, and what people we become. Computers have now become the center of many
cultures around the world, as technology unites people in a way that has never been seen before,
changing the way we view each other. In the last decade, the Internet has become a medium that lets
people share their ideas and opinions with millions of others. We are very close to a world where
ignorance is no longer an excuse. Computers are, and will be even more, in the center of our lives.

-- "World Around Us" by Alec Solway

Computer programming is an exiciting field in the modern world. We make our lives easier by "telling"
the computer to perform certain tasks for us. In a sense, this is what programming is. All types of tasks
require some kind of data to be manipulated. Whether we want to play a game, or manage our portfolio,
data is involved. By creating new ways to manage (access and change) data, we can make programs
more efficient, and thus obtain more reliable and faster results. Different types of programs require
different ways of handling data, however, standards exists between various programs. This website gives
you a peek into those standards, into the world of data structures and algorithms. It is
recommended that the reader have some experience with programming in general, although a brief
review of the C++ concepts needed to understand the data structure tutorials is provided. Please click on
"C++ Review" to begin your visit, or on "Data Structures" if you already have a solid C++ programming
background. Enjoy!

\u00a9 2000 ThinkQuest Team C005618
Page 1 of 1
An Introduction to Data Structures with C++
12/13/2007
file://F:\M Ebooks\Introduction_to_Data_Structures_with_C++\An Introduction to Data ...
Binary Trees
Binary trees are different from the three previous structures we have covered before. Lists, stacks, and
queues were all linear structures, that is, the elements in them were logically following each other.

A binary tree structure contains a root node, which is the first in the structure. The root points to one or
two other nodes, its left and right children. The root is considered to be a parent of these two nodes.
Each child is also a sub-tree, since it can have one or two children of its own. If a node has no children,
it is referred to as a leaf node.

Each node in the tree also has a level associated with it. The root node is at level 0, and increases with
each row of nodes below the root.

Binary trees have many different basic implementations. An array implementation is often times used,
where every level must be completely filled. In larger trees, this can be a very big waste of space. For
our demonstration, we will create a generic class using dynamic memory allocation. This particular
implementation was created by the author using a mixture of possible approaches. It is very effective in
explaining the concepts behind binary trees.

The binary tree class gives the programmer complete control over the tree. Nodes may be removed and inserted into any location in the list. The class allows the user to traverse the tree by keeping acurrent pointer, just as in the linked list class. The programmer can then use the functionsleft(),right(), and

parent() to move from one node to another. The class also allows the user to display the tree in in-

order, post-order, and pre-order. The "order" refers to how the nodes are displayed. For instance, in pre- order, a node's value is displayed, then the value of its left child, followed by the right child. In the case of in-order, the node's value is displayed between the value of its left and right child. In post order, the node's children are displayed before it. The implementation for the binary tree class is displayed below. Although it may look intimidating at first, the code is very easy to follow. The purpose and code behind each function is explained following the definition. You will note, most functions are programmed using recursion. Since each node is actually a tree within itself, using recursion is the easiest approach

Many books make a class for a single node, and use it to implement the tree. However, we will separate the structure for each node and the entire tree to conserve overhead processing time. Each time a node is created, much less time and memory is used than when a whole tree structure is made. Each node will store a value, and pointers to its children and parent. These will be used and modified by the general tree class.

template <class ItemType>
struct TreeNode
{

ItemType data;
Page 1 of 14
An Introduction to Data Structures with C++
12/13/2007
file://F:\M Ebooks\Introduction_to_Data_Structures_with_C++\binar tree.htm

TreeNode<ItemType> *left;
TreeNode<ItemType> *right;
TreeNode<ItemType> *parent;

};

template <class ItemType>
class BinaryTree
{public:

BinaryTree(); //create empty tree with default root node which has no value. set
current to main root node.

BinaryTree(TreeNode<ItemType>*,int); //create new tree with passed node as the
new main root. set current to main root. if the second parameter is 0, the new
object simply points to the node of the original tree. If the second parameter is
1, a new copy of the subtree is created, which the object points to.

~BinaryTree();
void insert(const ItemType&,int); //insert new node as child of current. 0=left
1=right
void remove(TreeNode<ItemType>*); //delete node and its subtree
ItemType value() const; //return value of current

//navigate the tree
void left();
void right();
void parent();
void reset(); //go to main_root
void SetCurrent(TreeNode<ItemType>*);

//return subtree (node) pointers
TreeNode<ItemType>* pointer_left() const;
TreeNode<ItemType>* pointer_right() const;
TreeNode<ItemType>* pointer_parent() const;
TreeNode<ItemType>* pointer_current() const;

//return values of children and parent without leaving current node
ItemType peek_left() const;
ItemType peek_right() const;
ItemType peek_parent() const;

//print the tree or a subtree. only works if ItemType is supported by <<

operator
void DisplayInorder(TreeNode<ItemType>*) const;
void DisplayPreorder(TreeNode<ItemType>*) const;
void DisplayPostorder(TreeNode<ItemType>*) const;

//delete all nodes in the tree
void clear();

//
bool IsEmpty() const;
bool IsFull() const;

private:
TreeNode<ItemType>* current;
TreeNode<ItemType>* main_root;

Page 2 of 14
An Introduction to Data Structures with C++
12/13/2007
file://F:\M Ebooks\Introduction_to_Data_Structures_with_C++\binar tree.htm
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...