4. What is tree
Terminologies of tree
Edge and cycle
Principle of tree
Binary tree and binary search tree
Construction of binary search tree from given
array
Insertion and deletion of node from BST
Algorithm for insertion and deletion in BST
applications of binary tree
Outlines
6. Tree
Tree is data structure that is non linear and can
be used to represents data in hierarchy
between those elements. For example:
organization structure, family tree, and the
tournament.
7. Terminology of Tree
• node : each element of tree
• Edges: lines connecting the nodes are called edges
• Root : has no parent, top most element
• leaf : has no child also called external node
• Parent if any node has child
• Sibling nodes that share same parent
8. Components of Tree
A
B C D
E F G
Root(head)
Leaf
Level
0
1
2
Left Subtree
Node/Vertex
Edge/Link
9. Terminology of Tree
• Degree number of child of particular node is called degree
• Of that node.
• level : level of a node is distance of that node from root.
• depth of tree: the number of edges from the root to node
in a path
height of tree: total number edges from the node to the
the deepest leaf.
• Ancestor :Total no of parents and grand parents for specific
node
• Descendant :no of child and grand child in same path
10. Every child must be connected with its parent
node
Disconnection are not allowed in binary tree
Every node must have only single parent
If any node have more than one parent than it
creates cycle
Principle of tree
cycle
11. Binary tree
A binary tree is simply a tree in which each node can have at
most two children. It may be 0, 1 or 2.
Root node
left sub tree
Right sub tree
Binary Tree
13. Cont…
Left Child: The node on the left of any node is called left child.
Right child: The node on the right of any node is called right
child.
Left Sub tree: sub tree attached to left side of root node is called
left sub tree.
Right Sub tree: sub tree attached to right side of root node is
called right sub tree.
The node of a binary tree is divided into three parts :
Left child Address Left Info Right Right child Address
14. Linked Representation using linkedlist
Ptr -> Info = Data
Ptr -> Left = Left Child
Ptr -> Right = Right Child
A
B C
11
32
D E
54
F G
76
E.g.
Left Info Right
Binary tree Representation using linkedlist
Tree is store in single array
15. From General Tree (Program)
Binary Tree
(Linked List)
A
B
D
E F
C
I G
H
Head
Binary Tree
A
B
H
CD
E F
GI
16. Types of Binary Tree
Full Binary Tree Complete Binary Tree
A
B
G
C
D E F
A
B C
D E
• All nodes (except leaf)
have two children.
• Each subtree has same
length of path.
• All nodes (except leaf)
have two children.
• Each subtree can has
different length of path.
17. Binary search tree
A binary Search Tree is special kind if tree that
satisfied the following conditions
• If value of inserted node is bigger than parent
then it will be right subtree.
• If value of inserted node is smaller than parent
then it will be left subtree.
• This tree is known as binary search tree or
ordered binary tree.
• It used for searching
18. Construction of binary Search Tree (BST)
1. Construction of binary Search Tree (BST)
Step.1: Initially tree is empty ,place the first element at the root.
Step.2: Compare the next element with root
if element is grater than or equal to root then place it in
right child position.
else
place it in the left child position.
Step.3: Repeat the process (step 2) for the next element until
end of elements or nodes.
20. ALGORITHM FOR SEARCHING IN BST
Algorithm :
Search BST(key,targetkey)
1) START
2) If root==NULL
3) Return value not found
4) End if
5) If(target key<root)
6) Return searchBST (leftsubtree, targetkey)
7) else if(targetkey > root)
8) Return searchBST (rightsubtree, targetkey)
9) ELSE
10)FOUND target key
11)Return root
12)End if
13)End SearchBST
21. Example of searching in BST
23
4418
5220 35
12
E.g. if we have to search 12 than go to left sub
tree
22. Binary SEARCH TREE (INSERTION)
For insert data in binary tree two conditions we have
to faced
1. IF tree is empty than insert to a new node make them root
node.
2. If tree is not empty than all inserts take place at a leaf or at a
leaf like node (a tree that has only one NULL subtree
23. Example of insertion in BST
23
4418
5220 35
12
23
4418
5220 35
12
Before insertion
19
After insertion
24. Binary SEARCH TREE (INSERTION)
Algorithm
1. START
2. If root==NULL
3. Root=newnode
4. If (newnode<root)
5. Return addBST(leftsubtree,newnode)
6. ELSE
7. Return addBST(rightsubtree,newnode)
8. End if
9. End addBST
25. Traversal of a binary tree is to access every node of binary
tree at most once
• Tree traversal ways
a) Pre0rder
b) In Order
c) Post Order
BST Traversal
26. 1. start
2. Visit root
3. Visit left sub-tree
4. Visit right sub-tree
5. stop
5
2 1
3 8
97
5 12 973 8
Preorder
start
Left sub
tree
Right sub
tree
27. 1. Visit left sub-tree
2. Visit root
3. Visit right sub-tree
5
2 1
3 8
97
2 1 3 5 7 9 8
Inorder
2 1
3 8
97
Right sub
tree
29. When we delete a node, we need to consider
how we take care of the children of the deleted
node.
Delete Node
30. Three cases:
(1) the node is a leaf
Delete it immediately
(2) the node has one child
Adjust a pointer from the parent to bypass that node
Delete Cont…..
31. 3)the node has 2 children
replace the key of that node with the
minimum element at the right sub tree
delete the minimum element
Has either no child or only right child because
if it has a left child, that left child would be
smaller and would have been chosen. So
invoke case 1 or 2.
32. Binary tree Applications
File System(storing naturally hierarchical
data)
Organize data(searching,insertion,deletion
Binary search tree used for this purpose
Telephone dictionary
Arithmetic operations
Network routing etc