2. DATA STRUCTURE
Store and organize data in computer.
Linear Data Structure
Arrays
Linked List
Stacks
Queues 2
3. LOGIC OF TREE
Used to represent hierarchica data.
Shuvo(CEO)
Shawon(CTO ) Tarun(Presedent)
Bithi Moni Sila Rashel Raj
Dola Shakila Riaj Shakib 3
4. Used to represent hierarchical data.
Shuvo(CEO)
Shawon(CTO ) Tarun(Presedent)
Bithi Moni Sila Rashel Raj
Dola Shakila Riaj Shakib 4
5. A Collection of entities called Nodes.
Tree is a Non-Linear Data Structure.
It’s a hierarchica Structure.
TREE
Nodes
5
6. 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
11109
6
7. 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
Leaf7
8. SOME APPLICATION OF TREE IN COMPUTER SCIENCE
1. Storing naturally hierarchicl data- File system.
2. Organige data for quick search, insertion, deletion- Binary search
tree.
3. Dictionary
4. Network Routing Algorithm.
8
9. 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
NULL9
13. WE CAN IMPLEMENT BINARY TREE USING
A) Dynamically created nodes.
struct node
{
int data;
struct node* left;
struct node* right
}
2
4 6
13
14. 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
15. 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
17. 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
JD
I
E KB G
A C
Root
H
17
21. SEARCH AN ELEMENT IN BST
bool Search( bstnode* root, data type)
{
if (root==NULL) return false;
else if(root->data == data) return true;
else if(root->data <= data)
return Search(root->left, data);
else
return Search(root->right, data);
}
12
155
14
7 173 13
8 11
Root
21
22. DLELETE A NODE FROM BST
There are three items to delete.
Case 1: No Child
Case 2: One Child
Case 3: Two Child
12
155
14
7 173 13
8 11
Root
22
23. CASE 1: NO CHILD
Remove to reference of the Node
From it’s parent &
Return NULL
12
155
14
7 173 13
8 11
Root
23
24. CASE 2: ONE CHILD
Find the Node to Delete
Link it’s parent to this only child.
Remain attached to the tree.
12
155
14
7 173 13
8 11
Root
24
25. 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
155
14
7 173 13
8 11
Root
25
26. CASE 3: TWO CHILD
2.
Find maximum in left
Copy the value in targetted node
Delete duplicate from left-subtree.
12
155
14
7 173 13
8 11
Root
26
27. 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