Data structure Store and organize data in computer. Linear Data Structure Arrays Linked List Stacks Queues 2
Logic of Tree Used to represent hierarchica data. Shuvo(CEO ) Shawon(CTO ) Tarun(Presedent) Bithi Moni Sila Rashel Raj Dola Shakila Riaj Shakib 3
Used to represent hierarchical data. Shuvo(CEO ) Shawon(CTO ) Tarun(Presedent) Bithi Moni Sila Rashel Raj Dola Shakila Riaj Shakib 4
A Collection of entities called Nodes. Tree is a Non-Linear Data Structure. It’s a hierarchica Structure . TRee Nodes 5
Root- The top most Node. Children Parents Siblings - Have same parents. Leaf - Has no Child. Relation of TRee 1 3 2 4 6 5 8 7 11 10 9 6
Edges, Depth, Height Edges: If a tree have N nodes It have N-1 edges. Depth of x: Length of path from Root to x. Hight of x: No. Of edges in longest Path from x to a leaf Nodes Leaf 7
Some Application of Tree in Computer Science Storing naturally hierarchicl data- File system . Organige data for quick search, insertion, deletion- Binary search tree. Dictionary Network Routing Algorithm. 8
Each node can have at most 2 childern . A node have only left and right child or Only left child or Only right child. A leaf node has no left or right child. A leaf node has only NULL. Binary TRee Root Right- child of root. Left-child of root Leaf NULL 9
Strict/Proper binary tree Each node can have either 2 or 0 child Root 10
Complete binary tree T he last level is completely filled. All nodes are as left as possible. Root L-2 L-3 L-4 L-1 11
Parfect binary tree All the levels are comletely filled. Root L-2 L-3 L-1 12
We can implement binary tree using A) Dynamically created nodes. struct node { int data; struct node* left; struct node* right } 2 4 6 13
or 0 1 2 3 4 5 6 B) Arrays: It only works “complete Binary tree”. For node at index i; Left-child-index=2i+1 Right-child-index=2i+2 2 4 1 5 8 7 9 14
Implement of binary search tree Value of all the nodes in left subtree is Lesser or Equal. Value of all the nodes in right subtree is greater. Left-Subtree Right-Subtree (Lesser) (Greater) 15
Binary tree traversal Tree traversal Breadth-first or Lever-order F,D,J,B,E,G,K,A,C,I,H Depth-first Preorder, Inorder & Postorder F J D I E K B G A C Root H 17
Preorder(DLR) Data Left Right <root><left><right> F,D,B,A,C,E,J,G,I,H,K Void Postorder(struct bstnode* root) { if(root==NULL) Postorder(root->right); Postordrt(root->right); printf(“%c”,root->data); } F J D I E K B G A C Root H 18
Inorder(ldR) Left Data Right <left><root><right> A,B,C,D,E,F,G,H,I,J,K Void Inorder(struct bstnode* root) { if(root==NULL) return; Inorder(root->left); printf(“%c”,root->data); Inorder(root->right); } F J D I E K B G A C Root H 19
pOSTORDER(LRD) Left Right Data <left><right><root> A,C,B,E,D,H,I,G,K,J,F,A,B Void Postorder(struct bstnode* root) { if(root==NULL) Postorder(root->right); Postordrt(root->right); printf(“%c”,root->data); } F J D I E K B G A C Root H 20
Dlelete a node from bst There are three items to delete. Case 1: No Child Case 2: One Child Case 3: Two Child 12 15 5 14 7 17 3 13 8 11 Root 22
Case 1: No child Remove to reference of the Node From it’s parent & Return NULL 12 15 5 14 7 17 3 13 8 11 Root 23
Case 2: One child Find the Node to Delete Link it’s parent to this only child. Remain attached to the tree. 12 15 5 14 7 17 3 13 8 11 Root 24
Case 3: two child Two way to delete. 1. Find minimum in right Copy the value in targetted node Delete duplicate from right subtree. 12 15 5 14 7 17 3 13 8 11 Root 25
Case 3: two child 2. Find maximum in left Copy the value in targetted node Delete duplicate from left-subtree. 12 15 5 14 7 17 3 13 8 11 Root 26
Running time of Operation Operation Array Link List Binary Search Tree (average case) Search(x)-Search for an element x. O(Log n) O(n) O(Log n) Insertion(x)-Insert an element x. O(n) O(1) O(Log n) Remove(x)-Remove an element x. O(n) O(n) O(Log n) 27