Selasa, 27 Maret 2018

Tree & Binary Tree

Tree & Binary Tree

1. Binary Tree
    A binary tree is a tree data structure in which each node has at most two children, which 
    are referred to as the left child and the right child.

Types of Binary Tree

Perfect binary tree is a binary tree in which every level are at the same depth.
Complete binary tree is a binary tree in which every level, except possibly the last, is completely
  filled, and all nodes are as far left as possible. A perfect binary tree is a complete binary tree.
Skewed binary tree is a binary tree in which each node has at most one child.
Balanced binary tree is a binary tree in which no leaf is much farther away from the root than any
  other leaf (different balancing scheme allows different definitions of “much farther”).

Perfect Binary Tree









Complete Binary Tree










Skewed Binary Tree














2. Difference Between Tree & Graph

       Tree :

       1. A tree is a special kind of graph that there are never multiple paths exist. There is always one         way to get from A to B.
       2. Tree must be connected. 
   3. Since it connected we can reach from one particular node to all other nodes. This                 kind of searching is called traversal.
   4. Tree contains no loops, no circuits. 
5. There must be a root node in tree.

Graph :

1. A graph is a system that has multiple ways to get from any point A to any
    other point  B.
2. Graph may not be connected.
3. Traversal always not applicable on graphs. Because graphs may not be connected.
4. Graph may contain self-loops, loops.
5. There is no such kind of root node in graphs.

Selasa, 20 Maret 2018

Introduction to Tree, Binary Tree And Expression Tree


Introduction to Tree, Binary Tree
and Expression Tree

1. Tree Concept




DEGREE of TREE = 3
DEGREE of C = 2
HEIGHT = 3
PARENT of C = A
CHILDREN of  A = B, C, D
SIBILING of F = G
ANCESTOR of F = A, C
DESCENDANT of C = F, G




Node at the top is called as root.
A line connecting the parent to the child is edge.
Nodes that do not have children are called leaf.
Nodes that have the same parent are called sibling.
Degree of node is the total sub tree of the node.
Height/Depth is the maximum degree of nodes in a tree.
If there is a line that connects p to q, then p is called the ancestor of q, and q is a descendant of p.

2. Binary Tree Concept


Binary tree is a rooted tree data structure in which each node has at most two children.Those
two children usually distinguished as left child and right child.Node which doesn’t have any
child is called leaf.

A sample of binary tree of
9 nodes, rooted on node which
contains 18

Leaves are nodes which contains
9, 12, 10, and 23.
3. Types of Binary Tree

            •  Perfect binary tree is a binary tree in which every level                     are at the same depth


      •  Complete binary tree is a binary tree in which every                       level, except possibly the last, is completely filled, and all               nodes are as far left as possible. A perfect binary tree is a               complete binary tree.



                         


                                     • Skewed binary tree is a binary tree in which each node                                           has at most one child











3       4. Property of Binary Tree
a      

Maximum number of nodes on a binary tree of height h is 2h+1 - 1.


Maximum nodes
of a binary tree of
height 3
= 1 + 2 + 4 + 8
= 20 + 21 + 22 + 23
= 24 – 1
= 15




5. Representation of Binary Tree

  • Implementation using array


Index on array represents node number
Index 0 is Root node
Index Left Child is 2p + 1, where p is parent index
Index Right Child is 2p + 2
Index Parent is (p-1)/2

  • Implementation using linked list


  struct node {

  int data;
  struct node *left;
  struct node *right;
  struct node *parent;
  };
•      struct node *root = NULL;


   6. Expression Tree Concept



Prefix  : *+ab/-cde
Postfix    : ab+cd-e/*
Infix  : (a+b)*((c-d)/e)




We will use this structure for each node in the tree:

struct tnode {
  char chr;
  struct tnode *left;
  struct tnode *right;
};

7. Prefix Transversal

void prefix(struct tnode *curr) {
  printf( “%c “, curr->chr );
  if ( curr->left  != 0 ) prefix(curr->left);
  if ( curr->right != 0 ) prefix(curr->right);
}

In prefix, you have to print/process before its child are processed.

8. Postfix Transversal

void postfix(struct tnode *curr) {
  if ( curr->left  != 0 ) postfix(curr->left);
  if ( curr->right != 0 ) postfix(curr->right);
 
printf( “%c“, curr->chr );
}
In postfix, you have to print/process after its child have been processed.

9. Infix Transversal

void infix(tnode *curr) {
  if ( is_operator(curr->chr) ) putchar( '(' );
  if ( curr->left != 0 ) infix(curr->left);
  printf( "%c", curr->chr );
  if ( curr->right != 0 ) infix(curr->right);
  if ( is_operator(curr->chr) ) putchar( ')' );
}

So * + a b c in prefix will be encoded as ((a + b) * c) in infix, which is correct.

Selasa, 13 Maret 2018

Linked List Implementation

Linked List Implementation

1. Stack Concept 

•Stack is an important data structure which stores its elements in an ordered manner




   Stack is a linear data structure which can be implemented by either
   using an array or linked list. The elements in a stack are added and
   removed only from one end, which is called the top. The data are 
   stored in Last In First Out (LIFO) way.




Linked List Representation


Technique of creating a stack using array is easier, but the drawback is that the array must 
be declared to have some fixed size. In a linked stack, every node has two parts:
–One that stores data
–One that stores the address of the next node
•The START pointer of the linked list is used as TOP
•All insertions and deletions are done at the node pointed by the TOP
If TOP = NULL, then it indicates that the stack is empty

Stack Operations

•push(x)  : add an item x to the top of the stack.
•pop()  : remove an item from the top of the stack.
•top()  : reveal/return the top item from the stack.

Example :








2. Stack Applications
There are three arithmetic notations known:
Prefix notation, also known as Reverse Polish notation.
Infix notation (commonly used)
Postfix notation, also known as Polish notation.

Example :

Prefix  : * 4 10
Infix    : 4 * 10
Postfix: 4 10 *

Infix Evaluation

Evaluate a given infix expression: 4 + 6 * (5 – 2) / 3

To evaluate infix notation, we should search the highest precedence
operator in the string.

4 + 6 * (5 – 2) / 3  search the highest precedence operator, it is ( )
4 + 6 * 3 / 3  search the highest precedence operator, it is *
4 + 18 / 3  search the highest precedence operator, it is  /
4 + 6  search the highest precedence operator, it is +
10

Postfix Evaluation

Manually
Scan from left to right
7   6   5   x   3   2   ^   –    +  , scan until reach the first operator
7   6   5   x   3   2   ^   –    +  , calculate 6 x 5
7   30           3   2   ^   –    +  , scan again until reach next operator
7   30           3   2   ^   –    +  , calculate 32
7   30           9             –    +  , scan again to search next operator
7   30           9             –    +  , calculate 30 – 9
7   21                                +  , scan again
7   21                                +  , calculate 7 + 24
28    , finish

String  

  4 6 5 2 – * 3 / +
  4 6 5 2 – * 3 / +  4  push(4)
  4 6 5 2 – * 3 / +  4 6  push(6)
  4 6 5 2 – * 3 / +  4 6 5  push(5)
  4 6 5 2 – * 3 / +  4 6 5 2  push(2)
  4 6 5 2 – * 3 / +  4 6 3  pop 2, pop 5, push(5 – 2)
  4 6 5 2 – * 3 / +  4 18  pop 3, pop 6, push(6 * 3)
  4 6 5 2 – * 3 / +  4 18 3  push(3)
  4 6 5 2 – * 3 / +  4 6  pop 3, pop 18, push(18 / 3)
  4 6 5 2 – * 3 / +  10  pop 6, pop 4, push(10) à  the result

Prefix Evaluation

Manually
Scan from right to left
+   7   –   x   6   5   ^   3   2
+   7   –   x   6   5   ^   3   2
+   7   –   x   6   5   9
+   7   –   x   6   5   9 
+   7   –   30           9
+   7   –   30           9
+   7   21
+   7   21
28

Conversion : Infix to Postfix

•Manually
•A + B – C x D ^ E / F   , power has the highest precedence
•A + B – C x D E ^ / F   , put ^ behind D and E
•A + B – C x D E ^ / F   , x  and / have same level precedence
•A + B – C D E ^ x / F   , put x at the end
•A + B – C D E ^ x / F   , continue with the same algorithm till finish
•A + B – C D E ^ x F /
A + B – C D E ^ x F /
•A B + – C D E ^ x F /
A B + – C D E ^ x F /
•A B + C D E ^ x F / –   , this is the Postfix notation

String                             Stack             Postfix String
 4 + 6 * (5 – 2) / 3 
 4 + 6 * (5 – 2) / 3                                          4
 4 + 6 * (5 – 2) / 3              +                          4
 4 + 6 * (5 – 2) / 3              +                          4 6
 4 + 6 * (5 – 2) / 3              + *                       4 6
 4 + 6 * (5 – 2) / 3              + * (                     4 6
 4 + 6 * (5 – 2) / 3              + * (                     4 6 5
 4 + 6 * (5 2) / 3              + * ( –                  4 6 5
 4 + 6 * (5 – 2) / 3              + *                       4 6 5 2
 4 + 6 * (5 – 2) / 3              + * /                     4 6 5 2 –
 4 + 6 * (5 – 2) / 3              + /                        4 6 5 2 – *
 4 + 6 * (5 – 2) / 3              + /                        4 6 5 2 – * 3
 4 + 6 * (5 – 2) / 3                                           4 6 5 2 – * 3 / +

Conversion : Infix to Prefix

Manually

A + B – C x D ^ E / F
A + B – C x ^ D E / F
A + B – C x ^ D E  / F
A + B – x C ^ D E  / F
A + B – x C ^ D E / F
A + B – / x C ^ D E F
A + B – / x C ^ D E F
+ A B – / x C ^ D E F
+ A B – / x C ^ D E F
– + A B / x C ^ D E F  , this is the Prefix notation

3. Depth Search

Depth First Search (DFS) is an algorithm for traversing or searching in a tree or graph. We start at the root of the 
tree (or some arbitrary node in graph) and explore as far as possible along each branch beforebacktracking.
DFS has many applications, such as:
Finding articulation point and bridge in a graph
Finding connected component
Topological sorting
etc.
DFS can be implemented by a recursive function or an iterative procedure using stack, their results may have a 
little differences on visiting order but both are correct.

Algorithm:

Prepare an empty stack
Push source/root into stack
Mark source/root
While stack is not empty
  Pop an item from stack into P
  For each node X adjacent with P
  If X is not marked
  Mark X
  Push X into stack

4. Queue

Queue can be implemented by either using arrays or linked lists. The elements in a queue
 are added at one end called the rear and removed from the other one end called frontThe 
data are stored in First In First Out (FIFO) way, this is the main difference between stack
and queue

push(x)  : add an item x to the back of the queue.
pop()  : remove an item from the front of the queue.
front()  : reveal/return the most front item from the queue.

(front is also known as peek.)


Cicular Queue

We can wrap-around index L and R.
If R reach MAXN then set R as zero, do the same to L.
It is also known as circular queue.


Deques
A deque (pronounced as ‘deck’ or ‘dequeue’) is a list in which elements can be inserted
or deleted at either end. It is also known as a head-tail linked list, because elements can 
be added to or removed from the front (head) or back (tail).

Two variants of a double-ended queue, include:
Input restricted deque
  In this dequeue insertions can be done only at one of the dequeue while deletions can be done
  from both the ends.
Output restricted deque
  In this dequeue deletions can be done only at one of the dequeue while insertions can be done
  on both the ends.

Priority Queue

A priority queue is an abstract data type in which the each
element is assigned a priority.
The general rule of processing elements of a priority queue
can be given as:
•An element with higher priority is processes before an element with lower priority
•Two elements with same priority are processed on a first come first served (FCFS) basis

5. Breadth First Search

Breadth First Search (BFS) like DFS is also an algorithm for traversing or searching in a 
tree or graph. We start at the root of the tree (or some arbitrary node in graph) and explore 
all neighboring nodes level per level until we find the goal.

BFS has many applications, such as:
Finding connected component in a graph.
Finding shortest path in an unweighted graph.
•Ford-Fulkerson method for computing maximum flow.
etc

The difference between DFS and BFS iterative implementation is only the data structure 
being used.

DFS uses stack while BFS uses queue.

Algorithm:
Prepare an empty queue
Push source/root into queue
Mark source/root
While queue is not empty
  Pop an item from queue into P
  For each node X adjacent with P
  If X is not marked
  Mark X
  Push X into queue