Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

# Saving this for later?

### Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Standard text messaging rates apply

# Trees

3,473
views

Published on

Published in: Education

1 Comment
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• very nice

Are you sure you want to  Yes  No
• Be the first to like this

Views
Total Views
3,473
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
158
1
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. Trees
Data Structures and Algorithms
• 2. Definition
Trees are used to impose a hierarchical structure on a collection of data items.
A tree is a set of one or more nodes T such that:
there is a specially designated node called a root
The remaining nodes are partitioned into n disjointed set of nodes T1, T2,…,Tn, each of which is a tree.
• 3. Basic Terminology
Degree of a Node of a Tree
The degree of a node of a tree is the number of subtrees having this node as a root. In other words, the degree is the number of descendants of a node. If the degree is zero, it is called a terminal or leaf node of a tree.
Degree of a Tree
The degree of a tree is defined as the maximum of degree of the nodes of the tree, that is, degree of tree = max (degree(node i) for I = 1 to n)
Level of a Node
We define the level of the node by taking the level of the root node as 1, and incrementing it by 1 as we move from the root towards the subtrees. So the level of all the descendants of the root nodes will be 2. The level of their descendants will be 3, and so on. We then define the depth of the tree to be the maximum value of the level of the node of the tree.
• 4. Binary Tree
A binary tree is a special case of tree as defined in the preceding section, in which no node of a tree can have a degree of more than 2. Therefore, a binary tree is a set of zero or more nodes T such that:
there is a specially designated node called the root of the tree
the remaining nodes are partitioned into two disjointed sets, T1 and T2, each of which is a binary tree. T1 is called the left subtree and T2 is called right subtree, or vice-versa
• 5. Skewed Binary Tree
• 6. Full binary tree
A full binary tree is a binary of depth k having 2k − 1 nodes. If it has < 2k − 1, it is not a full binary tree. For example, for k = 3, the number of nodes = 2k − 1 = 23 − 1 = 8 − 1 = 7.
A full binary tree with depth k = 3 is shown
• 7. Complete binary tree
A complete binary tree of depth k is a tree with n nodes in which these n nodes can be numbered sequentially from 1 to n, as if it would have been the first n nodes in a full binary tree of depth k.
A complete binary tree with depth k = 3 is shown
• 8. Representation of Binary Tree
BT are represented using 2 ways
Array representation
• 9. 1. Array Representation
we can map node i to the ith index in the array, and the parent of node i will get mapped at an index i/2, whereas the left child of node i gets mapped at an index 2i and the right child gets mapped at an index 2i + 1
• 10. 2. Linked List representation
we can use a linked representation, in which every node is represented as a structure with three fields: one for holding data, one for linking it with the left subtree, and the third for linking it with right subtree
• 11. Traversal of Binary Tree
There are 6 ways of traversal of binary tree
LDR
LRD
DLR
RDL
RLD
DRL
Where,
L stands for traversing the left subtree,
R stands for traversing the right subtree,
D stands for processing the data of the node
• 12. The order LDR is called as inorder;
the order LRD is called as postorder;
the order DLR is called as preorder.
The remaining three orders are not used.
• 13. Binary Search Tree
A binary search tree is a binary tree that may be empty, and every node must contain an identifier. An identifier of any node in the left subtree is less than the identifier of the root. An identifier of any node in the right subtree is greater than the identifier of the root. Both the left subtree and right subtree are binary search trees
• 14. DELETION OF A NODE FROM BINARY SEARCH TREE
Deletion of a node with two children
Deletion of a Node with One Child
Deletion of a Node with No Child
• 15. Deletion of a node with two children
• 16. Deletion of a Node with One Child
• 17. Deletion of a Node with No Child
• 18. Find or request more at http://amitbiswal.blogspot.com
Thank you