Selasa, 20 Maret 2018

4-Introduction to Tree, Binary Tree And Expression Tree-2101632055-YOEL JORDANIO IMANUEL

Introduction to Tree, Binary Tree And Expression Tree



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



Tree Concept

1. Node at the top is called as root.
2. A line connecting the parent to the child is edge.
3. Nodes that do not have children are called leaf.
4. Nodes that have the same parent are called sibling.
5. Degree of node is the total sub tree of the node.
6. Height/Depth is the maximum degree of nodes in a tree.
7. 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.



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

Leaves are nodes which
contain 9, 12, 10 and 23.













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
A perfect binary tree is also a 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 nodes
of a binary tree of
height 3
= 1 + 2 + 4 + 8
= 20 + 21 + 22 + 23
= 24 – 1
= 15




Representation of Binary Tree
Implementation using linked list

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



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

struct node *root = NULL;


Expression Tree Concept
Recall our discussion on stack application about arithmetic notation (session 6).

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

It is a binary tree.
Create Expression Tree from Prefix
We can create an expression tree from a prefix by recursive.


Tidak ada komentar:

Posting Komentar