Selasa, 27 Maret 2018

Session 5-Tree & Binary Tree

Subtopic:
-Binary Tree Termology
-Advantages of Tree
-Binary Search Tree
-Threaded Binary Tree

Binary Tree Termology
  • The depth of a node is the number of edges from the root to the node.
  • The height of a node is the number of edges from the node to the deepest leaf.
  • The height of a tree is a height of the root.
  • A full binary tree.is a binary tree in which each node has exactly zero or two children.
  • A complete binary tree is a binary tree, which is completely filled, with the possible exception of the bottom level, which is filled from left to right.

A complete binary tree is very special tree, it provides the best possible ratio between the number of nodes and the height. The height h of a complete binary tree with N nodes is at most O(log N).

Advantages of Tree
Trees are so useful and frequently used, because they have some very serious advantages:
  • Trees reflect structural relationships in the data
  • Trees are used to represent hierarchies
  • Trees provide an efficient insertion and searching
  • Trees are very flexible data, allowing to move subtrees around with minumum effort
Binary Search Tree
We consider a particular kind of a binary tree called a Binary Search Tree (BST). The basic idea behind this data structure is to have such a storing repository that provides the efficient way of data sorting, searching and retriving.

Insertion
The insertion procedure is quite similar to searching. We start at the root and recursively go down the tree searching for a location in a BST to insert a new node. If the element to be inserted is already in the tree, we are done (we do not insert duplicates). The new node will always replace a NULL reference.
Searching
Searching in a BST always starts at the root. We compare a data stored at the root with the key we are searching for (let us call it as toSearch). If the node does not contain the key we proceed either to the left or right child depending upon comparison. If the result of comparison is negative we go to the left child, otherwise - to the right child. The recursive structure of a BST yields a recursive algorithm.

Deletion
Deletion is somewhat more tricky than insertion. There are several cases to consider. A node to be deleted (let us call it as toDelete)
  • is not in a tree;
  • is a leaf;
  • has only one child;
  • has two children.
If toDelete is not in the tree, there is nothing to delete. If toDelete node has only one child the procedure of deletion is identical to deleting a node from a linked list - we just bypass that node being deleted

Deletion of an internal node with two children is less straightforward. If we delete such a node, we split a tree into two subtrees and therefore, some children of the internal node won't be accessible after deletion. In the picture below we delete 8:

Threaded Binary Tree
A binary tree is threaded by making all right child pointers that would normally be null point to the inorder successor of the node (if it exists), and all left child pointers that would normally be null point to the inorder predecessor of the node  
  • We have the pointers reference the next node in an inorder traversal; called threads
  • We need to know if a pointer is an actual link or a thread, so we keep a boolean for each pointer
Types of threaded binary trees:
  • Single Threaded: each node is threaded towards either the in-order predecessor or successor (left or right) means all right null pointers will point to inorder successor OR all left null pointers will point to inorder predecessor.
  • Double Threaded: each node is threaded towards both the in-order predecessor and successor (left and right) means all right null pointers will point to inorder successor AND all left null pointers will point to inorder predecessor.

Selasa, 20 Maret 2018

Session 4-Introduction to Tree, Binary Tree And Expression Tree

Subtopic:
-Tree Concept
-Binary Tree Concept
-Type of Binary Tree
-Property of Binary Tree
-Representation of Binary Tree
-Expression Tree Concept
-Prefix, Postfix, and Infix Traversal

Tree Concept

Degree of Tree = 3

Degree of C = 2

Height = 3

Parent of C = A

Children of A = B, C, D

Sibling of F = G

Ancestor of F = A, C

Descendant of C = F, G


Binary Tree Concept
is a rooted tree data structure in which each node has at  most two children.













Rooted = 18

Leaves = 9, 12 ,10, and 23

Type of Binary Tree

Perfect Binary Tree
is a binary tree which every level are at the same depth.

Complete Binary Tree
is binary tree in which every level, except possibly the last, is completely filled, and all nodes far as possible

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

Property of Binary Tree 
is a binary tree in which no leaf is much father away from the root

-Explanation for  the Example Property of Binary Tree
Maximum nodes of a binary tree of height 3
= 1 + 2 + 4 + 8
= 20 + 21 + 22 + 23
= 24 – 1
= 15


Representation of Binary Tree

-Using Array
Ex:
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

-Using Linked List
Ex:
struct node {
  int data;
  struct node *left;
  struct node *right;
  struct node *parent;
};


struct node *root = NULL;

Expression Tree Concept

Ex:





Prefix             : *+ab/-cde

Postfix            : ab+cd-e/*

Wrong Inflix   : a+b*c-d/e

Correct Inflix  : (a+b)*((c-d/e)





Prefix, Postfix, and Inflix Traversal

Prefix Traversal Ex:
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 Ex:
void postfix(struct tnode *curr) {
  if ( curr->left  != 0 ) postfix(curr->left);
  if ( curr->right != 0 ) postfix(curr->right);
 
printf( “%c“, curr->chr );
}

Inflix Traversal Ex:
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

Session 3-Linked List Implementation II-2101727190-Fedrick

Subtopic:
- Stack Concept
- Inflix, postfix and prefix notation
- Depth First Search
- Queue Concept
- Breadth First Search

Stack Concept
is an important data structure which stores its elements in an ordered maner.
In stack concept, the data are stored in Last In First Out way.
Stack Operation
- 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.
//top = peek.

Stack Applications
There are 4 applications of stack we will discuss:
- Infix
- Postfix
- Prefix
- Depth first search

Inflix, Postfix, and Prefix Notation
- Prefix notation = Reverse Polish notation
- Inflix notation (commonly used)
- Postfix notation = Polish notation


Prefix   : operator is written before operands
Infix     : operator is written betwen operands
Postfix : operator is written after operands
//We recommend to use Prefix and Postfix because much easier for computer to evaluate.

Example - Infix.
4 + 8 * (4+1) / 5
4 + 8 * 5 / 5
4 + 40 / 5
4 + 8
12

Example - Postfix
String                   Stack
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

Example - Prefix
+ 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

Depth First Search
Depth First Search is an algorithm we can use for traversing or searching in tree or graph.
in this search, data with last index will be poped firstly.

Queue Concepts
is an important data structure which stores its elements in an ordered maner.
In queue concept, the data are stored in First In First Out way.

Same with stack concepts but in queue top = front.
Queue Operation
- push(x) : add an item x to the top of the stack.
- pop()    : remove an item from the top of the stack.
- front()     : reveal/return the top item from the stack.
//front = peek.

now what happen if we push MAXN times and pop MAXN times?
we cant push another data into the queue, so we can use Circular Queue.

Circular Queue
if R reach MAXN then set R as zero, do the same to L.

Queue Applications
There are 3 applications of queue we will discuss:
- Deques
- Priority Queues
- Breadth First Search

Deques
is a list in which elements can be inserted or deleted at either end.

Priority Queue
is an abstract data type in which the each element is assigned a priority.

Breadth First Search
same like DFS is also an algorithm for traversing or searching in a tree or graph but pop data in the smaller index first.

in this search data with front index will be poped firstly.