9953330565 Low Rate Call Girls In Rohini Delhi NCR
Unit 2_2 Binary Tree as ADT_General Tree.pdf
1. Data Structures
Sanjivani Rural Education Society’s
Sanjivani College of Engineering, Kopargaon-423603
(An Autonomous Institute Affiliated to Savitribai Phule Pune University, Pune)
NAAC ‘A’ Grade Accredited, ISO 9001:2015 Certified
Department of Information Technology
(NBAAccredited)
Ms. K. D. Patil
Assistant Professor
2. Tree
• General Trees, Tree Terminology, Binary Trees, Use binary trees,
Conversion of general tree to binary tree, Array Based
representation of Binary Tree, Binary tree as an ADT, Binary tree
traversals - recursive and non-recursive algorithms, Construction
of tree from its traversals, Huffman coding algorithm
Data Structures Mrs. Kanchan Patil Department of Information Technology
3. Array Based Representation of Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Let’s consider a Binary tree of 7 nodes
4. Array Based Representation of Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• For the array representation of binary tree, we have to give numbering
to the corresponding nodes.
• Start from the root node and move from left to right at every level.
5. Array Based Representation of Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• we have numbered from zero you can simply place the corresponding
number in the matching index of an array.
6. Array Based Representation of Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Now Let’s consider a Binary tree
7. Array Based Representation of Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Make the child node of the leaf nodes as NULL
8. Array Based Representation of Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Now number the nodes as done above for the array representation of
binary tree.
9. Array Based Representation of Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• we have numbered from zero you can simply place the corresponding
number in the matching index of an array.
10. Linked List Representation of Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• We use a double linked list to represent a binary tree. In a double linked
list, every node consists of three fields.
• First field for storing left child address, second for storing actual data and
third for storing right child address.
11. Linked List Representation of Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
12. Binary Tree as an ADT
• Binary Tree have a maximum of two children
• we can assign direct pointers to them
• The declaration of tree nodes is same as in structure to that for doubly linked lists,
in that a node is a structure including the key information plus two pointers (left and
right) to other nodes
• Binary Tree node declaration
class Node {
int value;
Node left;
Node right;
Node(int value) {
this.value = value;
right = null;
left = null;
}
}
13. Binary Tree as an ADT
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Binary Tree class declaration
class binary_tree
{ node left, right;
public:
btree()
{
root=NULL;
}
Isempty();
void create();
void display();
void insert(node,node);
void inorder(node);
void longestPath();
int depth(node);
void display_leaves(node temp);
node copy(node first);
void copytree();
};
14. General Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• A General Tree is a tree in which each node can have an unlimited out-degree
• Each node may have as many children as is necessary to satisfy its requirements
• It is also referred as N-array tree
• Examples:
15. Changing General Tree to Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• A General Tree we can use following two relations
• Parent to Child
• Sibling to Sibling
• Rules are as follows:
• The root of the Binary Tree is the Root of the Generic Tree
• The left child of a node in the Generic Tree is the Left child of that node in the
Binary Tree
• The right sibling of any node in the Generic Tree is the Right child of that node in
the Binary Tree
16. Changing General Tree to Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Steps to Convert General Tree to Binary Tree
• Step I:
• Identify the branch from parent to its first and leftmost child
• Step II:
• Connect siblings starting with leftmost child using a branch for each siblings to
its right siblings.
• Step III:
• Remove all unneeded branches from the parent to its children. You will get the
final binary tree
17. Example
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Step I:
• Identify the branch from parent to its first and
leftmost child
• Step II:
• Connect siblings starting with leftmost child using
a branch for each siblings to its right siblings.
• Step III:
• Remove all unneeded branches from the parent
to its children. You will get the final binary tree
21. Inserting into General Trees
Data Structures Mrs. Kanchan Patil Department of Information Technology
• To insert a node into a general tree, the user must supply the parent of the
node.
• The new node can be inserted using 3 different ways:
• FIFO insertion
• LIFO insertion
• Key sequenced insertion
22. Inserting into General Trees
Data Structures Mrs. Kanchan Patil Department of Information Technology
• FIFO insertion:
• Insert a node at the end of the sibling list as we insert a node at the rear of
a queue
23. Inserting into General Trees
Data Structures Mrs. Kanchan Patil Department of Information Technology
• LIFO insertion:
• Insert a node at the beginning of the sibling list
24. Inserting into General Trees
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Key sequenced insertion:
• Places the new node in key sequence among the sibling nodes
• It is most common insertion rule in general trees
• It is similar to the insertion rule in a general ordered linked list
25. References
Data Structures Mrs. Kanchan Patil Department of Information Technology
• R. Lafore, “Data structures and Algorithms in Java”, Pearson education, ISBN: 9788
131718124.
• Michael Goodrich, Roberto Tamassia, Michael H. Goldwasser, “Data Structures and
Algorithms in Java”, 6th edition, wiley publication, ISBN: 978-1-118-77133-4
• R. Gilberg, B. Forouzan, “Data Structure: A Pseudo code approach with C++”, Cengage
Learning.
• Sartaj Sahni, “Data Structures, Algorithms and Applications in C++”, 2 nd Edition,
Universities Press.
• E. Horowitz, S. Sahni, S. Anderson-freed, “Fundamentals of Data Structures in C”, 2 nd
Edition, University Press, ISBN 978-81-7371-605-8.
26. Data Structures Mrs. Kanchan Patil Department of Information Technology
Thank You!!!
Happy Learning!!!