Trees: Anila Yasmeen
Trees: Anila Yasmeen
Anila Yasmeen
Outline
• Definition
• Basic Terminology
• Binary Tree
– Representation
– Basic Operations
– Applications
• Binary Search Tree
• Case Study: Huffman Algorithm
• Heap
• General Trees
Introduction
• Nonlinear Data Structure
• Represents Hierarchical Relation
• Recursive Data Structure
• Examples…?
Directory Structure
Organizational Structure
Owner
Manager Chef
6
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).
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
A Tree
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
A Tree of Depth 4
Root
Level 1 Node
Level 2
Arc
Level 3
Level 4
Leaf
Basic Terminology
• 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
• 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
Binary Tree
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.
14
Binary Tree Examples
Binary Trees
76 99
26 85 26
21 50 80 99 21 76
26
50 85
76
80
85
99
Strictly Binary Tree
If 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 always
contains 2*n-1 nodes.
Binary Tree Properties
minimum number of
nodes is d
Maximum Number Of Nodes
• All possible nodes at d levels are present.
=1+2+4+8+…+ 2d-1
= 2d - 1
Number Of Nodes & Depth
tn = 2d – 1
d = log2(tn+1)
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
Node Number Properties
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
2 3
4 5 6 7
8 9 10 11 12 13 14 15
2 3
4 5 6 7
8 9 10 11 12 13 14 15
• Array Representation
• Linked Representation
Array Representation
• Number the nodes using the numbering
scheme
for a complete binary tree. The node that is
numbered i is stored in tree[i].
1
a
2 3
b c
4 5 6 7
d e f g
8 9 10
h i j
tree[] a b c d e f g h i j
0 5 10
Right-Skewed Binary Tree
1
a
b 3
7
c
15
d
tree[] a - b - - - c - - - - - - - d
0 5 10 15
b c
d e
g
f
leftChild
info h
rightChild
Implementing a Binary Tree with
Pointers and Dynamic Data
V
Q L
E T A
K S
32
Algorithmic Notations
q = father (p);
if (q == NULL)
return FALSE;
if (left (q) == p)
return TRUE;
return FALSE;
brother(p)
q = father (p);
if (q == NULL) return
NULL;
if (isleft (p)) return (right (q));
return (left (q));
maketree(x)
• creates a new binary tree consisting of
asingle node with information field x and
return a pointer to that node.
maketree (x)
{
p = getnode();
info(p) = x;
left(p) = NULL;
right(p) = NULL;
return (p);
}
setLeft(p, x)
• setleft(p,x) creates a left son of the node p with info x.
setleft (p, x)
{
if (p == NULL)
cout << “Void Insertion”;
else if (left (p)!= NULL)
cout << “Invalid Insertion”;
else
left(p) = maketree (x);
}
ADT of Binary Tree
Objects:
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();
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.
46
A Two-Level Binary Expression
treePtr
‘-’
‘8’ ‘5’
PREORDER TRAVERSAL: - 8 5
POSTORDER TRAVERSAL: 8 5 -
47
A Binary Expression Tree
‘*’
‘+’ ‘3’
‘4’ ‘2’
( 4 + 2 ) * 3 = 18
48
A Binary Expression Tree
‘*’
‘+’ ‘3’
‘4’ ‘2’
49
A Binary Expression Tree
‘*’
‘+’ ‘3’
‘4’ ‘2’
Infix: ((4+2)*3)
Prefix: * + 4 2 3
• Preorder Traversal
– root, left, right
– prefix expression: ++a*bc*+*defg
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
this binary expression tree
‘*’
‘-’ ‘/’
‘4’ ‘2’
‘-’ ‘/’
‘4’ ‘2’
Infix: ((8-5)*((4+2)/3))
Prefix: *-85 /+423