Professional Documents
Culture Documents
The disadvantage of linked list is the access time At logical level a stack is an ordered group of Disadvantage of Queues and Stacks
taken. homogeneous items or elements, occurring in a Their sizes must be known prior to implementation of
particular sequence, organized according to how long\ actions.
e.g. they have been on the stack.
struct node
{ Because elements are only added and removed from
int key; the top of the stack, the last element to be added to it
struct node*next; is the first to be removed.
};
struct node* head*E; Stack operations
head = new node; 1. Push – this is an operation hat adds an
Z = new node; element to the top of the stack.
Head ->next = Z; 2. Pop – this is an operation that removes an
element from the top of the stack.
Inserting an element in a linked list 3. Top – this is an operation that returns a copy
- Create the node of the top element, without removing it from
- Make it point to the next/previous item the stack.
- Destroy the existing link 4. Makeempty – this operation sets the stack to
empty, and when one needs to use a stack it
Exercise must be empty.
Create a list with 5 elements 5. Isempty – this is an operations that is used to
tell whether a stack contains any elements
Types of linked lists before we pop it.
1. Circular linked lists. These are sometimes 6. Isfull – this is an operation that is used to test
used as an alternative for having damaged whether a stack is full before pushing.
nodes.
2. Double linked lists. It makes it possible to find Queues
the item before *** by using double linked list These are sequential organizations of data. Data is
in which we maintain 2 links on each node: - accessible using first in first out order (FIFO).
- To the item before
Trees Each node has exactly one node above it. The
A tree is an empty collection of vertices and edges that nodes directly below a node are called child/children.
satisfy certain requirements. Nodes without children are sometimes called leaves
or terminal nodes.
An ordered tree
Sometimes the way in which the children of each node
are ordered is significant. An ordered tree is one in
which the order of the children at any node is A binary tree could be empty, consisting of no internal
specific. nodes and external nodes. A full binary tree is one in
A vertex is a simple object that has a name and which internal nodes completely fill every level except
carries associated information. An edge is a Levels probably the last.
connection between two vertices. The level of a node is the number of nodes on the
path, from the nodes of the roots, not including itself. Applications of trees
A path in a tree is a list of distinct vertices in which Tree traversals – visiting elements of a tree.
successive vertices are connected by edges in the Height Traversal tree
tree. The height of the tree is the maximum level among all To traverse a tree is to systematically visit every node.
the nodes in the tree. There are a number of methods for traversing a tree.
One node in the tree is designated as the root. The
defining property of a tree is that there is exactly one Path length 1. Pre-order (ABC)
path between the route and each of the other nodes in This is the sum of all the levels.
the tree. If there is more than one route between the
route and some node then we have a graph. Multi-way
If each node must have a specific number of children
appearing in a specific order, then we have a multi-way 2. Post-order
tree. (BCA)
A multi-way tree is appropriate to define special 3. In order
external nodes which have no children. They are called (BAC)
dummy nodes.
The simplest type of the multi-way tree is the binary
tree. A binary tree is an ordered tree consisting of 2
types of nodes;
1. External nodes with no children
2. Internal nodes with exactly two children
Example 2 of Pre-order traversing
Algorithms
1. Pre-order transversal
This order is defined by the recursive rule.
Step1: Visit the root, then visit the left sub-tree
Step2: Then visit the right sub-tree
2. In order transversal
Step1: Visit the left sub-tree
Step2: Visit the root
Step3: Visit the right sub-tree
3. Post-order transversal
Step1: Visit the left sub-tree
Step2: Visit the right sub-tree
Step3: Visit the root