0% found this document useful (0 votes)
49 views54 pages

Trees: Anila Yasmeen

The document discusses various topics related to binary trees including: 1. It defines a binary tree as a nonlinear data structure that represents a hierarchical relationship with nodes that may have up to two children. 2. Basic terminology for binary trees such as root, leaf nodes, parent, child, and sibling are introduced. 3. Methods for representing binary trees using arrays and linked nodes are described.

Uploaded by

Anila Yasmeen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views54 pages

Trees: Anila Yasmeen

The document discusses various topics related to binary trees including: 1. It defines a binary tree as a nonlinear data structure that represents a hierarchical relationship with nodes that may have up to two children. 2. Basic terminology for binary trees such as root, leaf nodes, parent, child, and sibling are introduced. 3. Methods for representing binary trees using arrays and linked nodes are described.

Uploaded by

Anila Yasmeen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

Trees

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

Waiter Waiter Cook Helper

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

• 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.
Complete (Full) Binary Tree
A complete binary tree (of depth d) is the
strictly binary tree all of whose leaves are
at level d.
Almost Complete Binary Tree
A 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.
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 of
nodes is d
Maximum Number Of Nodes
• All possible nodes at d levels are present.

Maximum number of nodes

=1+2+4+8+…+ 2d-1
= 2d - 1
Number Of Nodes & Depth

• Let tn be the number of nodes in a binary


tree whose depth is d.

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

• Parent of node i is node i / 2, unless i = 1.


• Node 1 is the root and has no parent.
Node Number Properties
1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

• 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.
Node Number Properties
1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

• 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.
Binary Tree Representation

• 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

• An n node binary tree needs an array whose


length is between n+1 and 2n.
Linked Representation
struct TreeNode
{
ItemType info;
TreeNode *father; // left subtree
TreeNode *left; // left subtree
TreeNode *right; // right subtree
}
Linked Representation Example
root a

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

• If p is a pointer to a node of a binary tree, the


function 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 right
son, the father and brother respectively.
• These functions return NULL if node does not
have left, right son, a father or a brother.
Basic Operations

• The logical functions isleft(p) and


isright(p) return the value TRUE node
if pointed by p is a left or right son some
of other node in the tree, and
FALSE otherwise.
isLeft(p)

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.

• During the visit of an element, all action with


respect to this element is taken.
Binary Tree Traversal Methods

• Preorder Root, Left, Right

• Inorder Left, Root, Right

• Postorder Left, Right, Root


Preorder, Postorder and Inorder
Applications of Binary Trees
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.
Binary Expression Tree
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.

46
A Two-Level Binary Expression
treePtr

‘-’

‘8’ ‘5’

INORDER TRAVERSAL: 8 - 5 has value 3

PREORDER TRAVERSAL: - 8 5

POSTORDER TRAVERSAL: 8 5 -

47
A Binary Expression Tree

‘*’

‘+’ ‘3’

‘4’ ‘2’

What value does it have?

( 4 + 2 ) * 3 = 18
48
A Binary Expression Tree

‘*’

‘+’ ‘3’

‘4’ ‘2’

What infix, prefix, postfix expressions does it represent?

49
A Binary Expression Tree

‘*’

‘+’ ‘3’

‘4’ ‘2’

Infix: ((4+2)*3)
Prefix: * + 4 2 3

Postfix: 4 2 + 3 * has operators in order used


50
Expression Trees

• Leaves are operands (constants or variables)


• The other nodes (internal nodes) contain
operators
Preorder, Postorder and Inorder

• 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
‘*’

‘-’ ‘/’

‘8’ ‘5’ ‘+’ ‘3’

‘4’ ‘2’

What infix, prefix, postfix expressions does it represent?


54
A binary expression tree
‘*’

‘-’ ‘/’

‘8’ ‘5’ ‘+’ ‘3’

‘4’ ‘2’

Infix: ((8-5)*((4+2)/3))
Prefix: *-85 /+423

Postfix: 8 5 - 4 2 + 3 / * has operators in order used


55

You might also like