Submit Search
Upload
358 33 powerpoint-slides_10-trees_chapter-10
•
4 likes
•
2,616 views
S
sumitbardhan
Follow
Reema Thareja Data Structure on trees
Read less
Read more
Education
Technology
Business
Report
Share
Report
Share
1 of 18
Recommended
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
sumitbardhan
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9
sumitbardhan
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
Red black tree
Red black tree
Rajendran
AVL Tree Data Structure
AVL Tree Data Structure
Afaq Mansoor Khan
trees in data structure
trees in data structure
shameen khan
Tree in data structure
Tree in data structure
Äshïsh Jäïn
Recommended
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
sumitbardhan
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9
sumitbardhan
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
Red black tree
Red black tree
Rajendran
AVL Tree Data Structure
AVL Tree Data Structure
Afaq Mansoor Khan
trees in data structure
trees in data structure
shameen khan
Tree in data structure
Tree in data structure
Äshïsh Jäïn
Binary Search Tree
Binary Search Tree
Abhishek L.R
358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8
sumitbardhan
358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13
sumitbardhan
Linked list
Linked list
KalaivaniKS1
1.5 binary search tree
1.5 binary search tree
Krish_ver2
Tree Traversal
Tree Traversal
MdIsrailFakirGUB
Abstract Data Types
Abstract Data Types
karthikeyanC40
My lectures circular queue
My lectures circular queue
Senthil Kumar
Binary Search Tree
Binary Search Tree
sagar yadav
Data Structure (Tree)
Data Structure (Tree)
Adam Mukharil Bachtiar
Abstract data types
Abstract data types
Poojith Chowdhary
Tree traversal techniques
Tree traversal techniques
Syed Zaid Irshad
single linked list
single linked list
Sathasivam Rangasamy
Tree-In Data Structure
Tree-In Data Structure
United International University
Spanning trees
Spanning trees
Shareb Ismaeel
Expression trees
Expression trees
Salman Vadsarya
Graph Data Structure
Graph Data Structure
Keno benti
Queue
Queue
Allana Institute of management sciences
1.7 avl tree
1.7 avl tree
Krish_ver2
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
Lecture 5 tree.pptx
Lecture 5 tree.pptx
Abirami A
Module - 5_Trees.pdf
Module - 5_Trees.pdf
AnuradhaJadiya1
More Related Content
What's hot
Binary Search Tree
Binary Search Tree
Abhishek L.R
358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8
sumitbardhan
358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13
sumitbardhan
Linked list
Linked list
KalaivaniKS1
1.5 binary search tree
1.5 binary search tree
Krish_ver2
Tree Traversal
Tree Traversal
MdIsrailFakirGUB
Abstract Data Types
Abstract Data Types
karthikeyanC40
My lectures circular queue
My lectures circular queue
Senthil Kumar
Binary Search Tree
Binary Search Tree
sagar yadav
Data Structure (Tree)
Data Structure (Tree)
Adam Mukharil Bachtiar
Abstract data types
Abstract data types
Poojith Chowdhary
Tree traversal techniques
Tree traversal techniques
Syed Zaid Irshad
single linked list
single linked list
Sathasivam Rangasamy
Tree-In Data Structure
Tree-In Data Structure
United International University
Spanning trees
Spanning trees
Shareb Ismaeel
Expression trees
Expression trees
Salman Vadsarya
Graph Data Structure
Graph Data Structure
Keno benti
Queue
Queue
Allana Institute of management sciences
1.7 avl tree
1.7 avl tree
Krish_ver2
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
What's hot
(20)
Binary Search Tree
Binary Search Tree
358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13
Linked list
Linked list
1.5 binary search tree
1.5 binary search tree
Tree Traversal
Tree Traversal
Abstract Data Types
Abstract Data Types
My lectures circular queue
My lectures circular queue
Binary Search Tree
Binary Search Tree
Data Structure (Tree)
Data Structure (Tree)
Abstract data types
Abstract data types
Tree traversal techniques
Tree traversal techniques
single linked list
single linked list
Tree-In Data Structure
Tree-In Data Structure
Spanning trees
Spanning trees
Expression trees
Expression trees
Graph Data Structure
Graph Data Structure
Queue
Queue
1.7 avl tree
1.7 avl tree
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Similar to 358 33 powerpoint-slides_10-trees_chapter-10
Lecture 5 tree.pptx
Lecture 5 tree.pptx
Abirami A
Module - 5_Trees.pdf
Module - 5_Trees.pdf
AnuradhaJadiya1
Tree.pptx
Tree.pptx
worldchannel
Introduction to tree ds
Introduction to tree ds
Viji B
Tree
Tree
invertis university
Unit 5 Tree.pptx
Unit 5 Tree.pptx
SurajSharma266169
Lecture 21_Trees - I.pptx
Lecture 21_Trees - I.pptx
fizzaahmed9
7 chapter4 trees_binary
7 chapter4 trees_binary
SSE_AndyLi
Binary tree
Binary tree
Rajendran
data_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.ppt
ssuser5c874e
Data Structures 4
Data Structures 4
Dr.Umadevi V
Tree chapter
Tree chapter
LJ Projects
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
Rajitha Reddy Alugati
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
Aakash deep Singhal
Unit 6 tree
Unit 6 tree
Dabbal Singh Mahara
Data structure using c module 2
Data structure using c module 2
smruti sarangi
Data structures 3
Data structures 3
Parthipan Parthi
Dsc++ unit 3 notes
Dsc++ unit 3 notes
Guru Nanak Institute Of Tech
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
nakulvarshney371
Unit 3,4.docx
Unit 3,4.docx
Revathiparamanathan
Similar to 358 33 powerpoint-slides_10-trees_chapter-10
(20)
Lecture 5 tree.pptx
Lecture 5 tree.pptx
Module - 5_Trees.pdf
Module - 5_Trees.pdf
Tree.pptx
Tree.pptx
Introduction to tree ds
Introduction to tree ds
Tree
Tree
Unit 5 Tree.pptx
Unit 5 Tree.pptx
Lecture 21_Trees - I.pptx
Lecture 21_Trees - I.pptx
7 chapter4 trees_binary
7 chapter4 trees_binary
Binary tree
Binary tree
data_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.ppt
Data Structures 4
Data Structures 4
Tree chapter
Tree chapter
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
Unit 6 tree
Unit 6 tree
Data structure using c module 2
Data structure using c module 2
Data structures 3
Data structures 3
Dsc++ unit 3 notes
Dsc++ unit 3 notes
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
Unit 3,4.docx
Unit 3,4.docx
More from sumitbardhan
358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_15-hashing-collision_chapter-15
sumitbardhan
358 33 powerpoint-slides_12-heaps_chapter-12
358 33 powerpoint-slides_12-heaps_chapter-12
sumitbardhan
358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7
sumitbardhan
358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6
sumitbardhan
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5
sumitbardhan
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
sumitbardhan
358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3
sumitbardhan
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2
sumitbardhan
358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1
sumitbardhan
358 33 powerpoint-slides_16-files-their-organization_chapter-16
358 33 powerpoint-slides_16-files-their-organization_chapter-16
sumitbardhan
Algorithm analysis
Algorithm analysis
sumitbardhan
More from sumitbardhan
(11)
358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_12-heaps_chapter-12
358 33 powerpoint-slides_12-heaps_chapter-12
358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_16-files-their-organization_chapter-16
358 33 powerpoint-slides_16-files-their-organization_chapter-16
Algorithm analysis
Algorithm analysis
Recently uploaded
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
BP KOIRALA INSTITUTE OF HELATH SCIENCS,, NEPAL
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Quiz Club NITW
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
Vanessa Camilleri
Expanded definition: technical and operational
Expanded definition: technical and operational
ssuser3e220a
ClimART Action | eTwinning Project
ClimART Action | eTwinning Project
jordimapav
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
mary850239
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young minds
Pooky Knightsmith
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Quiz Club NITW
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
Sri Sairam College Of Engineering Bengaluru
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP Module
Celine George
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
RicaMaeCastro1
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
Quiz Club NITW
Concurrency Control in Database Management system
Concurrency Control in Database Management system
Christalin Nelson
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
MichelleTuguinay1
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Jemuel Francisco
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
Mr Bounab Samir
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
DhatriParmar
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
deepaannamalai16
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
DhatriParmar
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
Excellence Foundation for South Sudan
Recently uploaded
(20)
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
Expanded definition: technical and operational
Expanded definition: technical and operational
ClimART Action | eTwinning Project
ClimART Action | eTwinning Project
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young minds
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP Module
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
Concurrency Control in Database Management system
Concurrency Control in Database Management system
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
358 33 powerpoint-slides_10-trees_chapter-10
1.
© Oxford University
Press 2011. All rights reserved. Data Structures Using CData Structures Using C Reema TharejaReema Thareja, Assistant Professor, Institute of Information Technology and Management, New Delhi
2.
© Oxford University
Press 2011. All rights reserved. CHAPTER 10 TREES
3.
© Oxford University
Press 2011. All rights reserved. BINARY TREES • A binary tree is a data structure which is defined as a collection of elements called nodes. Every node contains a "left" pointer, a "right" pointer, and a data element. Every binary tree has a root element pointed by a "root" pointer. The root element is the topmost node in the tree. If root = NULL, then it means the tree is empty. • If the root node R is not NULL, then the two trees T1 and T2 are called the left and right subtrees of R. if T1 is non-empty, then T1 is said to be the left successor of R. likewise, if T2 is non-empty then, it is called the right successor of R. 8 1 32 4 5 6 7 1 2 1 0 1 1 ROOT NODE T2T1 9 In a binary tree every node has 0, 1 or at the most 2 successors. A node that has no successors or 0 successors is called the leaf node or the terminal node.
4.
© Oxford University
Press 2011. All rights reserved. KEY TERMS • Sibling: If N is any node in T that has left successor S1 and right successor S2, then N is called the parent of S1 and S2. Correspondingly, S1 and S2 are called the left child and the right child of N. Also, S1 and S2 are said to be siblings. Every node other than the root node has a parent. In other words, all nodes that are at the same level and share the same parent are called siblings (brothers). Level number: Every node in the binary tree is assigned a level number. The root node is defined to be at level 0. The left and right child of the root node has a level number 1. Similarly, every node is at one level higher than its parents. So all child nodes are defined to have level number as parent’s level number + 1. • Degree: Degree of a node is equal to the number of children that a node has. The degree of a leaf node is zero. In-degree of a node is the number of edges arriving at that node. The root node is the only node that has an in-degree equal to zero. Similarly, Out-degree of a node is the number of edges leaving that node. Leaf node: A leaf node has no children.
5.
© Oxford University
Press 2011. All rights reserved. KEY TERMS contd. Similar binary trees: Given two binary trees T and T’ are said to be similar if both of these trees have the same structure. A CB D E F HG I J TREE T’ TREE T” Copies of binary trees: Two binary trees T and T’ are said to be copies if they have similar structure and same contents at the corresponding nodes. A CB D E A CB D E TREE T’ TREE T”
6.
© Oxford University
Press 2011. All rights reserved. KEY TERMS contd. • Directed edge: Line drawn from a node N to any of its successor is called a directed edge. A binary tree of n nodes have exactly n – 1 edges (because, every node except the root node is connected to its parent via an edge). • Path: A sequence of consecutive edges is called a path. • Depth: The depth of a node N is given as the length of the path from the root R to the node N. The depth of the root node is zero. The height/depth of a tree is defined as the length of the path from the root node to the deepest node in the tree. A tree with only a root node has a height of zero. A binary tree of height h, has at least h nodes and at most 2h – 1 nodes. This is because every level will have at least one node and can have at most 2 nodes. So, if every level has two nodes then a tree with height h will have at the most 2h – 1 nodes as at level 0, there is only one element called the root. The height of a binary tree with n nodes is at least n and at most log2(n+1) • Ancestor and descendant nodes: Ancestors of a node are all the nodes along the path from the root to that node. Similarly, descendants of a node are all the nodes along the path from that node to the leaf node. • Binary trees are commonly used to implement binary search trees, expression trees, tournament trees and binary heaps.
7.
© Oxford University
Press 2011. All rights reserved. Complete Binary Trees • A complete binary tree is a binary tree which satisfies two properties. First, in a complete binary tree every level, except possibly the last, is completely filled. Second, all nodes appear as far left as possible • In a complete binary tree Tn, there are exactly n nodes and level r of T can have at most 2r nodes. • The formula to find the parent, left child and right child can be given as- if K is a parent node, then its left child can be calculated as 2 * K and its right child can be calculated as 2 * K + 1. For example, the children of node 4 are 8 (2*4) and 9 (2* 4 + 1). Similarly, the parent of the node K can be calculated as | K/2 |. Given the node 4, its parent can be calculated as | 4/2 | = 2. The height of a tree Tn having exactly n nodes is given as, • Hn = | log2 n + 1 | • This means, if a tree T has 10,00,000 nodes then its height is 21. 1 32 4 8 5 6 7 1 3 1 0 1 1 9 1 2
8.
© Oxford University
Press 2011. All rights reserved. Extended Binary Trees • A binary tree T is said to be an extended binary tree (or a 2-tree) if each node in the tree has either no child or exactly two children. Figure shows how an ordinary binary tree is converted into an extended binary tree. • In an extended binary tree nodes that have two children are called internal nodes and nodes that have no child or zero children are called internal nodes. In the figure internal nodes are represented using a circle and external nodes are represented using squares. • To convert a binary tree into an extended tree, every empty sub-tree is replaced by a new node. The original nodes in the tree are the internal nodes and the new nodes added are called the external nodes. Binary tree Extended binary tree
9.
© Oxford University
Press 2011. All rights reserved. Representation of Binary Trees in Memory • In computer’s memory, a binary tree can be maintained either using a linked representation (as in case of a linked list) or using sequential representation (as in case of single arrays). Linked Representation of Binary TreesLinked Representation of Binary Trees • In linked representation of binary tree, every node will have three parts, the data element, a pointer to the left node and a pointer to the right node. So in C, the binary tree is built with a node type given as below. struct node { struct node* left; int data; struct node* right; }; 1 2 3 4 5 6 7 X 8 X X 9 X X 10 X X 11 X X 12 X
10.
© Oxford University
Press 2011. All rights reserved. Sequential Representation of Binary Trees • Sequential representation of trees is done using single or one dimensional array. Though, it is the simplest technique for memory representation but it is very inefficient as it requires a lot of memory space. A sequential binary tree follows the rules given below: • One dimensional array called TREE, will be used. • The root of the tree will be stored in the first location. That is, TREE[0] will store the data of the root element. • The children of a node K, will be stored in location (2*K) and (2*K+1). • The maximum size of the array TREE is given as (2d+1 -1), where d is the depth of the tree. • An empty tree or sub-tree is specified using NULL. If TREE[0] = NULL, then the tree is empty. 3515 12 17 2 1 3 9 4 51 6 1 8 3 6 20 0 20 1 2 15 3 35 4 12 5 17 6 21 7 39 8 9 10 16 11 18 12 13 14 36 15 45 16 17 18
11.
© Oxford University
Press 2011. All rights reserved. EXPRESSION TREES • Binary trees are widely used to store algebraic expressions. For example, consider the algebraic expression Exp given as, • Exp = (a – b ) + ( c * d) • This expression can be represented using a binary tree as shown in figure + *- a b c d
12.
© Oxford University
Press 2011. All rights reserved. TOURNAMENT TREES • In a tournament tree (also called a selection tree), each external node represents a player and each internal node represents the winner of the match played between the players represented by its children nodes. These tournament trees are also called winner trees because they are being used to record the winner at each level. We can also have a loser tree that records the loser at each level. a ea a a d e g fc db e hg
13.
© Oxford University
Press 2011. All rights reserved. TRAVERSING OF A BINARY TREE • Traversing a binary tree is the process of visiting each node in the tree exactly once, in a systematic way. Unlike linear data structures in which the elements are traversed sequentially, tree is a non-linear data structure in which the elements can be traversed in many different ways. There are different algorithms for tree traversals. These algorithms differ in the order in which the nodes are visited. In this section, we will read about these algorithms. • Pre-order algorithm To traverse a non-empty binary tree in preorder, the following operations are performed recursively at each node. The algorithm starts with the root node of the tree and continues by, • Visiting the root node. • Traversing the left subtree. • Traversing the right subtree. A CB D E F IH GA, B, D, C, E, F, G, H and I
14.
© Oxford University
Press 2011. All rights reserved. In-order algorithm To traverse a non-empty binary tree in in-order, the following operations are performed recursively at each node. The algorithm starts with the root node of the tree and continues by, • Traversing the left subtree. • Visiting the root node. • Traversing the right subtree. Post-order algorithm To traverse a non-empty binary tree in post-order, the following operations are performed recursively at each node. The algorithm starts with the root node of the tree and continues by, • Traversing the left subtree. • Traversing the right subtree. • Visiting the root node. A CB D E F IH G B, D, A, E, H, G, I, F AND C. D, B, H, I, G, F, E, C and A.
15.
© Oxford University
Press 2011. All rights reserved. HUFFMAN’S TREE • Huffman coding is an entropy encoding algorithm developed by David A. Huffman that is widely used as a lossless data compression technique. The Huffman coding algorithm uses a variable-length code table to encode a source character where the variable-length code table is derived in a particular way based on the estimated probability of occurrence for each possible value of the source character. • The key idea behind the Huffman algorithm is that it encodes the most common characters using shorter strings of bits than are used for less common source characters. • The algorithm works by creating a binary tree of nodes that are stored in a regular array. The size of this array depends on the number of number of nodes in the tree. • The external path length of a binary tree is defined as the sum of all path lengths summed over each path from the root to the external node. The internal path length is also defined in the same manner. The internal path length of a binary tree is defined as the sum of all path lengths summed over each path from the root to the internal node.
16.
© Oxford University
Press 2011. All rights reserved. • The internal path length, LI = 0 + 1 + 2 + 1 + 2 + 3 + 3 = 12 • The external path length, LE = 2 + 3 + 3 + 2 + 4 + 4 + 4 + 4 = 26 • Note that, LI + 2 * n = 12 + 2 * 7 = 12 + 14 = 26 = LE • Hence, the formula is LI + 2n = LE, where n is the number of internal nodes. • Now if the tree with n external nodes is assigned a weight, then the weighted path length, P is defined as the sum of the weighted path lengths. • Therefore, P = W1L1 + W2L2 + …. + WnLn, • where, Wi and Li are the weight and path length of the external node Ni. The Huffman Algorithm Step 1: Create a leaf node for each character. Add the character and its weight or frequency of occurrence to the priority queue. Step 2: Repeat Steps 3 to 5 while the total number of nodes in the queue is greater than 1 Step 3: Remove two nodes that have the lowest weight (or highest priority) Step 4: Create a new internal node by merging these two nodes as children and with weight equal to the sum of the two nodes' weights. Step 5: Add the newly created node to the queue.
17.
© Oxford University
Press 2011. All rights reserved. Data Coding • When we want to code our data (character) using bits, then the basic formula is that we can use r bits to code 2r characters. For example, if we r =1, then two characters can be coded. If these two characters are A and B then A can be coded as 0 and B can be coded as 1 and vice versa. • Now if we have to code the data string, ABBBBBBAAAACDEFGGGGH, then the corresponding code would be- • 000001001001001001001000000000000010011100101110110110110111 • This coding scheme has fixed length code because every character is being coded using the same number of bits. Although this technique of coding is simple but coding the data can be made more efficient by using a variable length code. • To do variable length encoding, we build a Huffman tree first. First arrange all the characters in a priority queue in which the character with highest frequency of occurrence has the lowest weight and thus the highest priority. Then create a Huffman tree Cod e Characte r 00 A 01 B 10 C 11 D Code Character 000 A 001 B 010 C 011 D 100 E 101 F 110 G 111 H
18.
© Oxford University
Press 2011. All rights reserved. Data Coding contd. • In the Huffman tree, grey nodes (internal nodes) contain the cumulative weights of its child nodes. Every left branch is coded with ‘0’ and every right branch is coded with a ‘1’. So characters A, E, R, W, X Y and Z are coded as shown in Table E R Z A X W 1 1 1 11 1 0 0 0 0 0 0 Y Characters with their codes Character Code A 00 E 01 R 11 W 1010 X 1000 Y 1001 Z 1011 Thus, we see that frequent characters have a shorted code and infrequent characters have a longer code.