More From This User
logic for computer science - automated theorem proving
logic for computer science - automated theorem proving by Jean H Gallier
Tech Interview
Puzzles and other questions for interviews...
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.
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!
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.
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
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
{
TreeNode<ItemType> *left;
TreeNode<ItemType> *right;
TreeNode<ItemType> *parent;
template <class ItemType>
class BinaryTree
{public:
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.
//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;
operator
void DisplayInorder(TreeNode<ItemType>*) const;
void DisplayPreorder(TreeNode<ItemType>*) const;
void DisplayPostorder(TreeNode<ItemType>*) const;
//
bool IsEmpty() const;
bool IsFull() const;
private:
TreeNode<ItemType>* current;
TreeNode<ItemType>* main_root;
Leave a Comment
hello