Selasa, 27 Maret 2018

Pertemuan5-Binary Search Tree-2101660790




Binary Search Tree:
-          Operations: Search, Insertion, Deletion
-          Program Examples
-          Expression Tree Concept
-          Create Exp. Tree from Prefix
-          Create Exp. Tree from Postfix
-          Create Exp. Tree from Infix
-          Prefix, Postfix, Infix Traversal

-          Binary Search Tree has the following basic operations:
-          find(x)             : find key x in the BST
-          insert(x)          : insert new key x into BST
-          remove(x)      : remove key x from BST

           Operations: Search
         Because of the property of BST, finding/searching in BST is easy.
         Let the key that we want to search is X.
     We begin at root
     If the root contains X then search terminates successfully.
     If X is less than root’s key then search recursively on left sub tree, otherwise search recursively on right sub tree.

-            struct node* search (struct node *curr, int X) {
-            if ( curr == NULL ) return NULL;
-            // X is found
-            if ( X == curr->data ) return curr;
-            // X is located in left sub tree
-            if ( X  < curr->data ) return find(curr->left, X);
-            // X is located in right sub tree
-            return find(curr->right, X);
-          }
Operations: Insertion
         Inserting into Binary SearchTree  is done recursively.
         Let the new node’s key be X,
     Begin at root
     If X is less than node’s key then insert X into left sub tree, otherwise insert X into right sub tree
     Repeat until we found an empty node to put X (X will always be a new leaf)
Algorithm:
Step 1:           IF TREE = NULL, then 
                                    Allocate memory for TREE
                        SET TREE->DATA = VAL
                        SET TREE->LEFT = TREE ->RIGHT = NULL
            ELSE
                        IF VAL < TREE->DATA
                                                Insert(TREE->LEFT, VAL)
                        ELSE
                                                Insert(TREE->RIGHT, VAL)
                        [END OF IF]
            [END OF IF]

Step 2: End

Operations: Insertion – Example






Operations: Deletion
         There are 3 cases which should be considered:
     If the key is in a leaf, just delete that node
     If the key is in a node which has one child, delete that node and connect its child to its parent
     If the key is in a node which has two children, find the right most child of its left sub tree (node P), replace its key with P’s key and remove P recursively. (or alternately you can choose the left most child of its right sub tree)
Algorithm:
Step 1: IF TREE = NULL, then 
            Write “VAL not found in the tree”
        ELSE IF VAL < TREE->DATA
            Delete(TREE->LEFT, VAL)
        ELSE IF VAL > TREE->DATA
            Delete(TREE->RIGHT, VAL)
        ELSE IF TREE->LEFT AND TREE->RIGHT
                        SET TEMP = findLargestNode(TREE->LEFT)
                        SET TREE->DATA = TEMP->DATA
                        Delete(TREE->LEFT, TEMP->DATA)
ELSE
            SET TEMP = TREE
            IF TREE->LEFT = NULL AND TREE ->RIGHT = NULL
                        SET TREE = NULL
            ELSE IF TREE->LEFT != NULL
                        SET TREE = TREE->LEFT
            ELSE
                        SET TREE = TREE->RIGHT
            FREE TEMP
Step 2: End
      








Selasa, 20 Maret 2018

pertemuan4-Introduction to Tree, Binary Tree And Expression Tree-ihsan-2101669790




   
         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.



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.





















Type 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

Property of Binary Tree
Maximum number of nodes on level k of a binary tree is 2k.
Maximum number of nodes on a binary tree of height h is 2h+1 - 1.
Maximum number of nodes on a binary tree of height h is 2h+1 - 1.












Representation of Binary Tree




















struct node {
            int data;
            struct node *left;
            struct node *right;
            struct node *parent;
};

struct node *root = NULL;
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;
};
It is a binary tree.
Create Expression Tree from Prefix
char s[MAXN];
int  p = 0;
void f(struct tnode *curr) {
            if ( is_operator(s[p]) ) {
                        p++; curr->left  = newnode(s[p]);
                        f(curr->left);
                        p++; curr->right = newnode(s[p]);
            f(curr->right);
            }
}
Prefix Traversal
void prefix(struct tnode *curr) {
            printf( “%c “, curr->chr );
            if ( curr->left  != 0 ) prefix(curr->left);
            if ( curr->right != 0 ) prefix(curr->right);
}
Postfix Traversal
void postfix(struct tnode *curr) {
            if ( curr->left  != 0 ) postfix(curr->left);
            if ( curr->right != 0 ) postfix(curr->right);
            printf( “%c“, curr->chr );
}
Infix Traversal
void infix(struct tnode *curr) {
            if ( curr->left  != 0 ) infix(curr->left);
            printf( “%c“, curr->chr );
            if ( curr->right != 0 ) infix(curr->right);
}









Selasa, 13 Maret 2018

pertemuan3-Stack-ihsan-2101660790


Stack is an important data structure which stores its elements in an ordered manner
Analogy:
            You must have seen a pile of plates where one plate is placed on top of another. When you want to remove a plate, you remove the topmost plate first. Hence, you can add and remove an element (i.e. the plate) only at/from one position which is the topmost position
         Stack has two variables:
      TOP which is used to store the address of the topmost element of the stack
      MAX which is used to store the maximum number of elements that the stack can hold
         If TOP = NULL, then indicates that the stack is empty
         If TOP = MAX – 1, then the stack is full
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.

Infix, Postfix, and Prefix Notation











      Prefix             : operator is written before operand
      Infix                : operator is written between operands
      Postfix           : operator is written after operands

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 before
backtracking.
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.
Queue is an important data structure which stores its elements in an ordered manner
         Example:
     People moving on an escalator. The people who got on the escalator first will be the first one to step out of it.
     People waiting for a bus. The person standing in the line will be the first one to get into the bus
     Luggage kept on conveyor belts
     Cars lined for filling petrol
     Cars lined at a toll bridge

Linked List Representation
         Similar with stack, technique of creating a queue using array is easy, but its drawback is that the array must be declared to have some fixed size.
         In a linked queue, every element has two parts
     One that stores the data
     Another that stores the address of the next element
         The START pointer of the linked list is used as the FRONT
         All insertions will be done at the REAR, and all the deletions are done at the FRONT end.
         If FRONT = REAR = NULL, then it indicates that the queue is empty

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.