Upload
waleed-khan
View
215
Download
0
Embed Size (px)
Citation preview
8/9/2019 DS5Binary Trees
1/100
8/9/2019 DS5Binary Trees
2/100
8/9/2019 DS5Binary Trees
3/100
Outline• Definition
• Basic Terminology
• Binary Tree
– Representation
– Basic Operations – Applications
• Binary Search Tree
• Case Study: Huffman Algorithm• Heap
• General Trees
8/9/2019 DS5Binary Trees
4/100
8/9/2019 DS5Binary Trees
5/100
Directory Structure
8/9/2019 DS5Binary Trees
6/100
8/9/2019 DS5Binary Trees
7/100
Definition• A tree is a finite set of nodes, such that:
– There is a special node called the root. – The remaining nodes are partitioned into n 0
disjoint sets T1, T2, ......., Tn.
– Each of the sets T1, T2, ......., Tn is itself a tree(subtrees of root).
8/9/2019 DS5Binary Trees
8/100
Basic Terminology• Node
– item of information plus links to other nodes
• Parent and Child
– The subtrees of a node are called its children and node
itself is called a parent.
• Root
– a node without a parent
• Every node except the root has one parent
• A node can have any number of children
8/9/2019 DS5Binary Trees
9/100
A Tree
8/9/2019 DS5Binary Trees
10/100
Basic Terminology
• Leaves
– Nodes with no children• Sibling
– nodes with same parent
• Arc
– connection between a parent and a child
• Depth – Maximum number of levels in a tree
8/9/2019 DS5Binary Trees
11/100
A Tree of Depth 4Root
Node
Leaf
Arc
Level 1
Level 2
Level 3
Level 4
8/9/2019 DS5Binary Trees
12/100
• Degree
– number of subtrees of a node• The degree of tree is the maximum degree of
the nodes in the tree.
• Ancestors
– all the nodes along the path from root to the node.
• Forest – A set of n 0 disjoint trees
Basic Terminology
8/9/2019 DS5Binary Trees
13/100
Basic Terminology
• Depth of a node
– length of the unique path from the root to that node
– The depth of a tree is equal to the depth of the
deepest leaf
• Height of a node
– length of the longest path from that node to a leaf
– all leaves are at height 0 – The height of a tree is equal to the height of the root
8/9/2019 DS5Binary Trees
14/100
14
A binary tree is a finite set of nodes which
is either empty or consists of a root and
two disjoint binary trees called the left
subtree and the right subtree.
Binary Tree
8/9/2019 DS5Binary Trees
15/100
Binary Tree ExamplesBinary Trees
76
26
5021
85
9980
99
26
7621
85
80
50
26
76
85
99
8/9/2019 DS5Binary Trees
16/100
Strictly Binary TreeIf every non-leaf node has non-empty left and
right subtrees, the binary tree is termed a
strictly binary tree.
• A strictly binary tree with n leaves alwayscontains 2*n-1 nodes.
8/9/2019 DS5Binary Trees
17/100
Binary Tree Properties
• If a binary tree contains m nodes at level l,it contains at most 2*m nodes at level l+1.
• A binary tree can contain at most one node
at level 1 (the root), it can contain at most
2l-1
nodes at level l.
8/9/2019 DS5Binary Trees
18/100
Complete (Full) Binary TreeA complete binary tree (of depth d) is the
strictly binary tree all of whose leaves are atlevel d.
8/9/2019 DS5Binary Trees
19/100
Almost Complete Binary TreeA binary tree (of depth d) is an almost complete
binary tree if
1. Each leaf in the tree at level d or at level d-1.
2. For any node ‘nd’ in the tree with a right
descendent at level d, all the left descendents of‘nd’ that are leaves are also at level d.
8/9/2019 DS5Binary Trees
20/100
Minimum Number Of Nodes• Minimum number of nodes in a binary tree
whose depth is d.
• At least one node at each of d levels.
minimum number ofnodes is d
8/9/2019 DS5Binary Trees
21/100
Maximum Number Of Nodes• All possible nodes at d levels are present.
Maximum number of nodes
= 1 + 2 + 4 + 8 + … + 2d-1
= 2d
- 1
8/9/2019 DS5Binary Trees
22/100
Number Of Nodes & Depth
• Let tn be the number of nodes in a binarytree whose depth is d.
tn = 2d – 1
d = log2(tn+1)
8/9/2019 DS5Binary Trees
23/100
Nodes Numbering In A Complete Binary
Tree
• Number the nodes 1 through 2d – 1.
• Number by levels from top to bottom.
• Within a level number from left to right.1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
8/9/2019 DS5Binary Trees
24/100
Node Number Properties
• Parent of node i is node i / 2, unless i = 1.• Node 1 is the root and has no parent.
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
8/9/2019 DS5Binary Trees
25/100
Node Number Properties
• Left child of node i is node 2i, unless 2i > n,where n is the number of nodes.
• If 2i > n, node i has no left child.
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
8/9/2019 DS5Binary Trees
26/100
Node Number Properties
• Right child of node i is node 2i+1, unless 2i+1> n, where n is the number of nodes.
• If 2i+1 > n, node i has no right child.
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
8/9/2019 DS5Binary Trees
27/100
Binary Tree Representation
• Array Representation• Linked Representation
8/9/2019 DS5Binary Trees
28/100
Array Representation
• Number the nodes using the numbering schemefor a complete binary tree. The node that isnumbered i is stored in tree[i].
tree[]
0 5 10
a b c d e f g h i j
b
a
c
d e f g
h i j
1
2 3
4 5 6 7
8 9 10
8/9/2019 DS5Binary Trees
29/100
Right-Skewed Binary Tree
• An n node binary tree needs an array whoselength is between n+1 and 2n.
a
b
1
3
c
7
d15
tree[] 0 5 10a - b - - - c - - - - - - - 15d
8/9/2019 DS5Binary Trees
30/100
Linked Representationstruct TreeNode
{
ItemType info;
TreeNode *father; // left subtreeTreeNode *left; // left subtree
TreeNode *right; // right subtree
}
8/9/2019 DS5Binary Trees
31/100
Linked Representation Examplea
c b
d
f
e
g
hleftChildinfo
rightChild
root
8/9/2019 DS5Binary Trees
32/100
32
Implementing a Binary Tree with
Pointers and Dynamic Data
Q
V
T
K S
AE
L
8/9/2019 DS5Binary Trees
33/100
Algorithmic Notations
• If p is a pointer to a node of a binary tree, thefunction info(p) returns the contents of node.
• The functions left(p), right(p), father(p) and
brother(p) return pointers to the left son, the rightson, the father and brother respectively.
• These functions return NULL if node does nothave left, right son, a father or a brother.
8/9/2019 DS5Binary Trees
34/100
Basic Operations
• The logical functions isleft(p) andisright(p) return the value TRUE if node
pointed by p is a left or right son of some
other node in the tree, and FALSEotherwise.
8/9/2019 DS5Binary Trees
35/100
isLeft(p)
q = father (p);if (q == NULL)
return FALSE;
if (left (q) == p)
return TRUE;
return FALSE;
8/9/2019 DS5Binary Trees
36/100
brother(p)
q = father (p);
if (q == NULL) return NULL;
if (isleft (p)) return (right (q));
return (left (q));
8/9/2019 DS5Binary Trees
37/100
maketree(x)• creates a new binary tree consisting of a
single node with information field x andreturn a pointer to that node.
maketree (x)
{
p = getnode();
info(p) = x;
left(p) = NULL;
right(p) = NULL;
return (p);
}
8/9/2019 DS5Binary Trees
38/100
setLeft(p, x)• setleft(p,x) creates a left son of the node p with info x.
setleft (p, x)
{
if (p == NULL)
cout
8/9/2019 DS5Binary Trees
39/100
ADT of Binary TreeObjects:A finite set of nodes either empty or consisting of a
root node, left BinaryTree and right BinaryTree.Operations:
BinaryTree ();
BinaryTree (ElementType info);BinaryTree (BinaryTree lbt, ElementType info, BinaryTree rbt);
Boolean IsEmpty();
BinaryTree LChild();ElementType Data();
BinaryTree RChild();
8/9/2019 DS5Binary Trees
40/100
Binary Tree Traversal• Many binary tree operations are done by
performing a traversal of the binary tree.
• In a traversal, each element of the binary tree is
visited exactly once.
• During the visit of an element, all action withrespect to this element is taken.
8/9/2019 DS5Binary Trees
41/100
Binary Tree Traversal Methods
• Preorder Root, Left, Right
• Inorder Left, Root, Right
• Postorder Left, Right, Root
8/9/2019 DS5Binary Trees
42/100
8/9/2019 DS5Binary Trees
43/100
Preorder, Postorder and Inorder
8/9/2019 DS5Binary Trees
44/100
Applications of Binary Trees
8/9/2019 DS5Binary Trees
45/100
Finding Duplicates
Suppose, we want to find all duplicates in a
list of numbers.
• The first number is placed in the root.
• Each successive number is compared to the root.
– If it matches, we have a duplicate.
– If it is smaller, we examine left subtree.
– If it is greater, we examine right subtree.• If the subtree is empty, we do not have a duplicate and is
placed into a new node.
8/9/2019 DS5Binary Trees
46/100
46
A special kind of binary tree in which:
1. Each leaf node contains a single operand,
2. Each nonleaf node contains a single binary
operator, and
3. The root contains the operator that is to be
applied to the results of evaluating the
expressions in the left and right subtrees.
Binary Expression Tree
8/9/2019 DS5Binary Trees
47/100
47
A Two-Level Binary Expression
‘-’
‘8’ ‘5’
treePtr
INORDER TRAVERSAL: 8 - 5 has value 3
PREORDER TRAVERSAL: - 8 5
POSTORDER TRAVERSAL: 8 5 -
8/9/2019 DS5Binary Trees
48/100
48
A Binary Expression Tree
‘*’
‘+’
‘4’
‘3’
‘2’
What value does it have?
( 4 + 2 ) * 3 = 18
8/9/2019 DS5Binary Trees
49/100
49
A Binary Expression Tree
‘*’
‘+’
‘4’
‘3’
‘2’
What infix, prefix, postfix expressions does it represent?
8/9/2019 DS5Binary Trees
50/100
50
A Binary Expression Tree
‘*’
‘+’
‘4’
‘3’
‘2’
Infix: ( ( 4 + 2 ) * 3 )
Prefix: * + 4 2 3
Postfix: 4 2 + 3 * has operators in order used
8/9/2019 DS5Binary Trees
51/100
Expression Trees
• Leaves are operands (constants or variables)
• The other nodes (internal nodes) contain operators
8/9/2019 DS5Binary Trees
52/100
Preorder, Postorder and Inorder
• Preorder Traversal
– root, left, right – prefix expression: ++a*bc*+*defg
8/9/2019 DS5Binary Trees
53/100
Preorder, Postorder and Inorder
• Postorder Traversal
– left, right, root – postfix expression
• abc*+de*f+g*+
• Inorder Traversal
– left, root, right.
– infix expression• a+b*c+d*e+f*g
Evaluate
8/9/2019 DS5Binary Trees
54/100
54
va ua e
this binary expression tree
‘*’
‘-’
‘8’ ‘5’
What infix, prefix, postfix expressions does it represent?
‘/’
‘+’
‘4’
‘3’
‘2’
8/9/2019 DS5Binary Trees
55/100
55
A binary expression tree
Infix: ( ( 8 - 5 ) * ( ( 4 + 2 ) / 3 ) )Prefix: * - 8 5 / + 4 2 3
Postfix: 8 5 - 4 2 + 3 / * has operators in order used
‘*’
‘-’
‘8’ ‘5’
‘/’
‘+’
‘4’
‘3’
‘2’
8/9/2019 DS5Binary Trees
56/100
56
Binary Search Trees
8/9/2019 DS5Binary Trees
57/100
57
A special kind of binary tree in which:
1. Each node contains a distinct data value,
2. The key values in the tree can be compared using
“greater than” and “less than”, and
3. The key value of each node in the tree is
less than every key value in its right subtree, and
greater than every key value in its left subtree.
A Binary Search Tree (BST) is . . .
Bi S h T
8/9/2019 DS5Binary Trees
58/100
Binary Search Trees
A binary search tree Not a binary search tree
BST E l
8/9/2019 DS5Binary Trees
59/100
BST Example
Sorting using BST
8/9/2019 DS5Binary Trees
60/100
Sorting using BST
15, 6, 18, 3, 7, 17, 20, 2, 4, 13, 9
Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20
Sorting using Binary SearchTrees
8/9/2019 DS5Binary Trees
61/100
Sorting using Binary SearchTrees
• Read the numbers and insert them into a binary tree using the following rules:
– The first number is placed in the root.
– Each successive number is compared to the root:
– If it is smaller than the root, place it in the leftsubtree.
– If it is equal or greater than the root, place it inthe right subtree.
• If this tree is traversed in inorder (left, root,right) the numbers are printed in ascendingorder.
Searching BST
8/9/2019 DS5Binary Trees
62/100
Searching BST
• If we are searching for 15, then we are done.
• If we are searching for a key < 15, then weshould search in the left subtree.
• If we are searching for a key > 15, then we
should search in the right subtree.
8/9/2019 DS5Binary Trees
63/100
8/9/2019 DS5Binary Trees
64/100
64
Is ‘F’ in the binary search tree?‘J’
‘E’
‘A’ ‘H’
‘T’
‘M’
‘K’
‘V’
‘P’ ‘Z’‘D’
‘Q’‘L’‘B’
‘S’
8/9/2019 DS5Binary Trees
65/100
65
Implementation of
Binary Search Trees
8/9/2019 DS5Binary Trees
66/100
66
Each node contains two pointers
template< class ItemType >
struct TreeNode {
ItemType info; // Data member
TreeNode* left; // Pointer to left child
TreeNode* right; // Pointer to right child
};
left info right
NULL ‘A’ 6000
8/9/2019 DS5Binary Trees
67/100
67
TreeType CharBST;
‘J’
‘E’
‘A’
‘S’
‘H’
TreeType
~TreeType
IsEmpty
InsertItem
Private data:
root
RetrieveItem
PrintTree
// BINARY SEARCH TREE SPECIFICATION
8/9/2019 DS5Binary Trees
68/100
68
template< class ItemType >
class TreeType {
public:
TreeType ( ) ; // constructor ~TreeType ( ) ; // destructor
bool IsEmpty ( ) const ;
bool IsFull ( ) const ;
int NumberOfNodes ( ) const ;void InsertItem ( ItemType item ) ;
void DeleteItem (ItemType item ) ;
void RetrieveItem ( ItemType& item , bool& found ) ;
void PrintTree (ofstream& outFile) const ;
. . .
private:
TreeNode* root ;
};68
// SPECIFICATION (continued)
8/9/2019 DS5Binary Trees
69/100
69
// SPECIFICATION (continued)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // RECURSIVE PARTNERS OF MEMBER FUNCTIONS
template< class ItemType >
void PrintHelper ( TreeNode*
ptr, ofstream& outFile ) ;
template< class ItemType >
void InsertHelper ( TreeNode*& ptr, ItemType item ) ;
template< class ItemType >void RetrieveHelper ( TreeNode* ptr, ItemType& item,
bool& found ) ;
template< class ItemType >
void DestroyHelper ( TreeNode* ptr ) ;
69
S C O
8/9/2019 DS5Binary Trees
70/100
70
// BINARY SEARCH TREE IMPLEMENTATION
// OF MEMBER FUNCTIONS AND THEIR HELPER FUNCTIONS
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template< class ItemType >
TreeType :: TreeType ( ) // constructor
{
root = NULL ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template< class ItemType >
bool TreeType :: IsEmpty( ) const
{return ( root == NULL ) ;
}
70
template< class ItemType >
id T T I tIt ( It T it )
8/9/2019 DS5Binary Trees
71/100
71
void TreeType :: InsertItem ( ItemType item )
{
InsertHelper ( root, item ) ;
}
template< class ItemType >void InsertHelper ( TreeNode*& ptr, ItemType item )
{ if ( ptr == NULL )
{ // INSERT item HERE AS LEAF
ptr = new TreeNode ;ptr->right = NULL ;
ptr->left = NULL ;
ptr->info = item ;
}
else if ( item < ptr->info ) // GO LEFTInsertHelper( ptr->left , item ) ;
else if ( item > ptr->info ) // GO RIGHT
InsertHelper( ptr->right , item ) ;
}71
template< class ItemType >
void TreeType :: RetrieveItem ( ItemType& item,
8/9/2019 DS5Binary Trees
72/100
72
void TreeType :: RetrieveItem ( ItemType& item,
bool& found ){
RetrieveHelper ( root, item, found ) ;
}
template< class ItemType >
void RetrieveHelper ( TreeNode* ptr, ItemType& item,
bool& found)
{ if ( ptr == NULL )
found = false ;else if ( item < ptr->info ) // GO LEFT
RetrieveHelper( ptr->left , item, found ) ;
else if ( item > ptr->info ) // GO RIGHT
RetrieveHelper( ptr->right , item, found ) ;
else
{ item = ptr->info ;
found = true ;
}
}72
8/9/2019 DS5Binary Trees
73/100
8/9/2019 DS5Binary Trees
74/100
8/9/2019 DS5Binary Trees
75/100
75
Preorder Traversal: J E A H T M Y
‘J’
‘E’
‘A’ ‘H’
‘T’
‘M’ ‘Y’
tree
Print left subtree second Print right subtree last
Print first
Postorder Traversal: A H E M Y T J
8/9/2019 DS5Binary Trees
76/100
76
‘J’
‘E’
‘A’ ‘H’
‘T’
‘M’ ‘Y’
tree
Print left subtree first Print right subtree second
Print last
8/9/2019 DS5Binary Trees
77/100
8/9/2019 DS5Binary Trees
78/100
Case Study
Huffman Algorithm
Huffman Codes
8/9/2019 DS5Binary Trees
79/100
•Huffman codes are a way to compress data.
•They are widely used and effective, leading to 20
- 90% savings of space, depending on the data.
• A greedy algorithm uses the frequency of
occurrence of each character to build an optimal
code for representing each character as a binary
string.
8/9/2019 DS5Binary Trees
80/100
Optimal Codes
8/9/2019 DS5Binary Trees
81/100
• Strategy: Assign shorter codes to high frequencysymbols.
Symbol Code
A 0
B 110C 10
D 111
• Using this code the message ABACCDA wouldthen be encoded as 0110010101110, whichrequires 13 bits.
Remember…..
8/9/2019 DS5Binary Trees
82/100
• If variable-length codes are used, the
code for one symbol may not be a prefix
of the code for another.
8/9/2019 DS5Binary Trees
83/100
Example # 2
8/9/2019 DS5Binary Trees
84/100
Suppose we have 105 characters in a data file.
Normal storage: 8 bits per character (ASCII) -- 8x105 bits in
file. We want to compress the file and store it compactly.
Suppose only 6 characters appear in the file:
a b c d e f Total
freq 45 13 12 16 9 5 100
How can we represent the data in a compact way?
Fixed Length code: Each letter represented by an equal
number of bits.
Fixed Length code
8/9/2019 DS5Binary Trees
85/100
With a fixed length code, how many bits per character will we
need? At least 3 bits per character:
Example:
a 000
b 001
c 010
d 011
e 100
f 101
For a file with 105 characters, how many bits total would we
need to encode it? 3 x 105 bits.
Tree for variable length code
8/9/2019 DS5Binary Trees
86/100
a 0
b 101
c 100d 111
e 1101
f 1100
100
30
14
25
55a: 45
d: 16c: 12 b: 13
f: 5 e: 9
0
00
0
1
01
11
1
Variable length codes
8/9/2019 DS5Binary Trees
87/100
We can save space by assigning frequently occurring
characters short codes, and infrequently occurring characters
long codes.
a b c d e f Total
freq 45 13 12 16 9 5 100
Example:
a 0b 101
c 100
d 111
e 1101f 1100
Number of bits = (45*1 + 13*3 + 12*3 + 16*3 + 9*4
+5*4)*1000 = 2.24 x 105 bits (This is optimal)
Prefix Codes Decoding
8/9/2019 DS5Binary Trees
88/100
To encode: concatenate the code for each character:
abc = 0101100
To decode: There is no ambiguity, assign first character
that fits.
001011101= ?
0 (a) -> 0 (a) -> 101 (b) -> 1101 (e)
8/9/2019 DS5Binary Trees
89/100
Homework
8/9/2019 DS5Binary Trees
90/100
t
s nl
e i sp
146total
5100001newline
261311space
1640001t
15300000s
241210i
301501e
3010001a
Total BitsFrequencyCodeCharacter
H
8/9/2019 DS5Binary Trees
91/100
91
HeapA heap is a binary tree that satisfies the
following conditions:1. largest element in tree is located at the root
2. each node has a larger value than its children
3. tree is balanced and the leaves on the last level
are all as far left as possible
Valid Heaps
8/9/2019 DS5Binary Trees
92/100
Valid Heaps
root
Z
T
X
N
M
J L
root
Z
T
X
N
M
Invalid Heaps
8/9/2019 DS5Binary Trees
93/100
Invalid Heaps
root
X
T
Z
N
M
J L
Level 2 has a value higher than level 1
root
X
T
Z M
J
Nodes not all to left
Is this a heap?
8/9/2019 DS5Binary Trees
94/100
94
70
60
40 30
12
8 10
tree
Is this a heap?
Where is the largest element
in a heap always found?
8/9/2019 DS5Binary Trees
95/100
95
70
60
40 30
12
8
tree
in a heap always found?
Heap Applications
8/9/2019 DS5Binary Trees
96/100
• Great for priority queues
– highest priority item always at the root
– just pop it off and re-sort the heap
• Can be used to sort data (called heap sort) – put the data in a heap
– remove the root and re-sort
– keep repeating this until the heap is empty
C++ Representation of Trees
8/9/2019 DS5Binary Trees
97/100
#define MAX 20
struct treenode{
int info;
treenode *father;
treenode *son[MAX];};
struct treenode
{
int info;
treenode *father;
treenode *son;
treenode*next;
};
Implementation of a general Tree
8/9/2019 DS5Binary Trees
98/100
Tree Operations
8/9/2019 DS5Binary Trees
99/100
void setsons (NODEPTR p, NODEPTR list)
{if ( p == NULL || pson ! = NULL)
{
cout
8/9/2019 DS5Binary Trees
100/100
qif ( p == NULL)
{
cout