Binary Search Tree
5 stars based on
A deque double-ended queue is a linear list where we can add items and remove them at both ends. We already have definitions of functions to add-at-the-rear and remove-from-the-front see last week's definition of the Queue class. It remains to add-at-the-front and remove-from-the-rear. The first of these is fairly straightforward: Every time it is called, it has to traverse the full length of the list looking for the next-to-the-last node. This is because class binary search tree c++ the one-way nature of the singly-linked list.
Doubly-linked lists An alternative implementation, which removes this particular inefficiency, is to use nodes that have two pointers rather than one, so as to produce a doubly-linked list: A doubly-linked list looks like this: Adding or removing nodes is going to be equally easy at either end of the list. A possibly unsatisfactory class binary search tree c++ of our Binode class like our Node class is that all of its data fields are public. At first sight this seems a bit fishy, as you don't want anything messing class binary search tree c++ the contents of your deque.
As it happens, because the only way to get at the Binode list is through left or rightand since these are privatethe data is effectively encapsulated. If we wanted to make quite certain that nothing except a Deque object could make any use of a Class binary search tree c++we could make the Binode class private to the Deque class by adding the following code to the declaration of Deque:. It would also now be necessary to define the Deque class before the Deque:: Now, other parts of the program could define their own Binode classes, but this particular Binode class would be private class binary search tree c++ the Deque class.
We could do this, obviously, by providing accessor and mutator functions or we could fix it at a stroke by making the Deque class a friend of the Class binary search tree c++ class, by modifying the definition of Binode: It declares that the Deque class has special privileges with respect to the members of the Binode class: The idea behind object-oriented programming, they would say, is that we can see, from inspecting the class binary search tree c++ in a class definition, how the private data members of an object can be altered; we do not need to fear that data will be changed in a way that is not obvious from the class definition.
Yet this friend feature, they point out, provides a back-door route into an object; private data can be changed in a way that is not at all obvious from the object's own interface but is only to be found in the function definitions of a different class. On the other hand, it provides a convenient way of permitting close collaboration between objects of two intimately related classes.
Binary trees Another structure that we can create with Binodes is a binary tree. Pointers from one Binode down to Binodes at the next level are called branches. Binodes that don't have any branches are known as leaf nodes. We can also define a binary tree recursively by saying that a binary tree is either empty or consists of a Binode with a binary tree on each side. The nodes are all related to one another as parents, children and siblings brothers and sisters. Let's look at how to build a binary tree.
The nodes in the above tree are in no particular order. However, if we arrange the values so that the values in all the nodes in the subtree to the left of the parent are less than the parent's value note: If we rearrange our tree as follows, we turn it into a binary search tree:.