Tree-In Data Structure

27

Transcript of Tree-In Data Structure

Page 1: Tree-In Data Structure
Page 2: Tree-In Data Structure

TREEBY:Samrin Ahmed RiyaID: 011142021Sanzida AkterID: 011142032

Page 3: Tree-In Data Structure

leaves

branches roo

t

Page 4: Tree-In Data Structure

root

nodes

leaves

branches

Page 5: Tree-In Data Structure

Connected graph.Connected graph. Acyclic graph. A finite non-empty set of elements. CConsisting of nodes with a parent-child relation.

Introduction of a Tree

B C

FD E G

A

Page 6: Tree-In Data Structure

Family Structures of a Tree Root-node without parent (A) Siblings-nodes share the same parent.Internal node-node with at least one child (A, B, C, E)External node (leaf): node without children (D,F,G, H, I)Ancestors of a node: parent, grandparent, grand-grandparent, etc.Descendant of a node: child, grandchild, grand-grandchild, etc.Depth of a node: number of ancestorsHeight of a tree: maximum depth of any node (3)Degree of a node: the number of its childrenDegree of a tree: the maximum number of its node. Subtree: tree consisting of a node and its descendants

Page 7: Tree-In Data Structure
Page 8: Tree-In Data Structure

Left Child Right Sibling Representation

Data

Left Child Right Sibling

A

B C D

IHGFE

J K L

Page 9: Tree-In Data Structure

Preorder Sequence. Inorder Sequence. Postorder Sequence. Level order Sequence

Tree Traversal Technique

Tree Traversal Technique

Preorder Inorder Postorder Level order

Page 10: Tree-In Data Structure

Tree Traversal Technique (CONT.)

Preorder Sequence: Visits the nodes of a tree in a systematic manner A node is visited before its descendants Application: print a structured document

Algorithm: void preorder(node *root){

if(root!=NULL){printf(“%c”,root->data);

if(root->left!=NULL)preorder(root->left);

if(root->right!=NULL)preorder(root->right);

}}

Page 11: Tree-In Data Structure
Page 12: Tree-In Data Structure

Inorder Sequence: A node is visited after its left subtree and before its

right sub treeAlgorithm :void inorder(node *root){

if(root!=NULL){if(root->left!=NULL)

inorder(root->left);printf(“%c”,root->data);if(root->right!=NULL)

inorder(root->right);}

}

Page 13: Tree-In Data Structure
Page 14: Tree-In Data Structure

Postorder Sequence: A node is visited after its descendants Application: compute space used by files in a

directory and its subdirectoriesAlgorithm :void postorder(node *root){

if(root!=NULL){if(root->left!=NULL)

postorder(root->left);if(root->right!=NULL)

postorder(root->right);printf(“%c”,root->data);

}}

Page 15: Tree-In Data Structure
Page 16: Tree-In Data Structure

Level order Sequence: A node is traversed in a level by

level order. Algorithm :Queue<-rootwhile Queue !=Empty

v<-Queuevisit vVisited Sequence<-vif left(v) !=NULL then

Queue<-left(v)if right(v) !=NULL then

Queue<-right(v)endwhilereturn Visited Sequence

Page 17: Tree-In Data Structure
Page 18: Tree-In Data Structure

Each internal node has at most two children (degree of two) The children of a node are an ordered pair

Alternative Recursive Defintion: A tree consisting of a single node, or A tree whose root has an ordered pair of children.Applications: Arithmetic expressions. Decisions process searching. Searching.

A

B C

F GD E

H I

Page 19: Tree-In Data Structure

Strict Binary Tree: Each node exactly having two or zero

child.

Full Binary Tree: Each node exactly having two node or

zero child and leaf node at the same level.

Complete Binary Tree: All leaf node is at the same height. No missing node in the sequence.

Page 20: Tree-In Data Structure

Strict Binary Tree

Full Binary Tree

A

B C

GE

I

D

H

F

Complete Binary Tree

A

B C

GF

Page 21: Tree-In Data Structure

Basic Operations: Inserting an element into tree. Deleting an element from tree. Searching for an element. Traversing the tree.

Auxiliary Operations: Finding size of tree. Calculating height of tree.

Page 22: Tree-In Data Structure

Expressions trees are used in compiler. Huffman coding trees which are used in data

compression technique. Binary Search Tree (BST) supports insertion &

deletion of node with o(log n) Priority queue supports search and deletion of

min or max on a collection of items in a logarithmic times.

Page 23: Tree-In Data Structure

Method 1: height=max(level(i))Method 2: height= log2(n)

Method 3: height(T)=max(height(Tl),height(Tr))+1

Page 24: Tree-In Data Structure

Algorithm :

int height(node *root){if(root==NULL)

return -1;else if(root->left==NULL && root->right==NULL)

return 0;else

return max(height(root->left),height(root->right))+1;}

Int max(int x,int y){if(x>y)

return x;else

return y;}

Page 25: Tree-In Data Structure

https://en.wikipedia.org/wiki/Tree_%28data_stru cture%29 http://www.slideshare.net/samsumitgupta/trees-data-structure http://www.i-programmer.info/babbages-bag/477-trees.html https://www.cse.iitb.ac.in/~cs101/pdfs/Trees.ppt

Page 26: Tree-In Data Structure
Page 27: Tree-In Data Structure