Showing posts with label Non-linear Data Structure. Show all posts
Showing posts with label Non-linear Data Structure. Show all posts
Data Structure : Binary Tree Introduction
The simplest form of tree is a binary tree. A binary tree consists of
- a node (called the root node) and
- left and right sub-trees.
Both the sub-trees are themselves binary trees.
You now have a recursively defined data structure. (It is also possible to define a list recursively)
The nodes at the lowest levels of the tree (the ones with no sub-trees) are called leaves.
In an ordered binary tree,
- the keys of all the nodes in the left sub-tree are less than that of the root,
- the keys of all the nodes in the right sub-tree are greater than that of the root,
- the left and right sub-trees are themselves ordered binary trees.
Data Structure
The data structure for the tree implementation simply adds left and right pointers in place of the next pointer of the linked list implementation. [Load the tree struct.]
The AddToCollection method is, naturally, recursive. [ Load the AddToCollection method.]
Similarly, the FindInCollection method is recursive. [ Load the FindInCollection method.]
Analysis
Complete Trees
Before we look at more general cases, let's make the optimistic assumption that we've managed to fill our tree neatly, ie that each leaf is the same 'distance' from the root.| This forms a complete tree, whose height is defined as the number of links from the root to the deepest leaf. |
First, we need to work out how many nodes, n, we have in such a tree of height, h.
Now,
n = 1 + 21 + 22 + .... + 2hFrom which we have,
n = 2h+1 - 1and
h = floor( log2n )
Examination of the Find method shows that in the worst case, h+1 or ceiling( log2n ) comparisons are needed to find an item. This is the same as for binary search.
However, Add also requires ceiling( log2n ) comparisons to determine where to add an item. Actually adding the item takes a constant number of operations, so we say that a binary tree requires O(logn) operations for both adding and finding an item - a considerable improvement over binary search for a dynamic structure which often requires addition of new items.
Deletion is also an O(logn) operation.
General binary trees
However, in general addition of items to an ordered tree will not produce a complete tree. The worst case occurs if we add an ordered list of items to a tree.
What will happen?
This problem is readily overcome: we use a structure known as a heap. However, before looking at heaps, we should formalise our ideas about the complexity of algorithms by defining carefully what O(f(n)) means.
Key terms |
- Root Node
- Node at the "top" of a tree - the one from which all operations on the tree commence. The root node may not exist (a NULL tree with no nodes in it) or have 0, 1 or 2 children in a binary tree.
- Leaf Node
- Node at the "bottom" of a tree - farthest from the root. Leaf nodes have no children.
- Complete Tree
- Tree in which each leaf is at the same distance from the root. A more precise and formal definition of a complete tree is set out later.
- Height
- Number of nodes which must be traversed from the root to reach a leaf of a tree.
Data Structure : Introduction of Non linear D.S.
In the first lesson of this section, we discussed the problem of representing relationships between employees at the XYZ company. Since these relationships are not linear, we could not adequately show the relationships using a linear data structure like a list or a stack. Instead, we needed something that looks more like a tree.
A tree is just one example of a nonlinear data structure. Two other examples are multidimensional arrays and graphs. In the next few lessons, we will examine these data structures to see how they are represented using the computer's linear memory. Remember that in the last lesson we saw that we could create a logical representation of a circular queue. Although the actual memory locations were just an array (a group of linear memory cells), we made them seem to be circular by wrapping our pointers around to the front of the array each time they reached the end. This example demonstrated that there are two ways of representing our data structure. The logical representation was a circle or a loop, while the physical representation was a simple linear array.
For each of the data structures we examine, we will look at a simple implementation for the data structure to see how it can be represented in physical memory. Then we will compare this physical representation with the logical representation of the data structure.
Subscribe to:
Posts
(
Atom
)