Vous êtes sur la page 1sur 59

Trees and

Binary Trees
Objective of the sesson
• Tree
– Basic Terminologies
• Binary Tree
– Binary Tree – Representation
• Linear Data Structure and Non- Linear Data
Structure
Nature View of a Tree
leaves

branches
root
Computer Scientist’s View

root

leaves

branches
nodes
TREES
• Trees are one of the important non- Linear data
structure.
• A tree is a Multilevel data structure that represent a
hierarchical relationship between the Set of
individual elements called nodes.
• Each tree structure starts with a node Which is called
the root node of the Tree.
Tree Terminology
• Root: node without parent (A)
• Siblings: nodes share the same • Degree of a tree: the
parent maximum number of its node.
• Internal node: node with at least • Subtree: tree consisting of a
one child (A, B, C, F) node and its descendants
• External node (leaf ): node
without children (E, I, J, K, G, H, D)
• Ancestors of a node: parent, A
grandparent, grand-grandparent,
etc.
• Descendant of a node: child,
grandchild, grand-grandchild, etc. B C D
• Depth of a node: number of
ancestors
• Height of a tree: maximum depth E F G H
of any node (3)
• Degree of a node: the number of
its children
I J K
subtree
Binary Tree
• A tree in which every node can have a
maximum of two children is called as Binary
Tree.
Full Binary Tree
• A binary tree in which every node has either
two or zero number of children is called Full
Binary Tree
Complete Binary Tree
• A binary tree in which every internal node has exactly two children
and all leaf nodes are at same level is called Complete Binary Tree.
Complete binary tree is also called as Perfect Binary Tree
• Number of nodes = 2d+1 – 1
• Number of leaf nodes = 2d
• Where, d – Depth of the tree
Left Skewed and Right Skewed Trees

• Binary tree has only left sub trees - Left


Skewed Trees
• Binary tree has only right sub trees - Right
Skewed Trees
Binary Tree Representation
1. Sequential representation using arrays
2. List representation using Linked list
Sequential representation

• To represent a binary tree of depth ‘d' using array


representation, we need one dimensional array with a
maximum size of 2d+1 - 1.
Sequential representation
• Advantages:
– Direct access to all nodes (Random access)
• Disadvantages:
– Height of tree should be known
– Memory may be wasted
– Insertion and deletion of a node is difficult
List representation
struct node
{
int data;
struct node *left;
struct node *rifgt;
}
List representation
• Advantages:
– Height of tree need not be known
– No memory wastage
– Insertion and deletion of a node is done without
affecting other nodes
• Disadvantages:
– Direct access to node is difficult
– Additional memory required for storing address of
left and right node
Non-Linear Data Structure
• In a non linear data structure , the Elements are not
arranged in sequence.
• The data members are arranged in any Manner. The
data items are not processed one After another.
• Trees and graphs are examples of non linear data
structure.
Linear Data Structure
Vs
Non-Linear Data Structure
• In linear data structures, data elements are
organized sequentially and therefore they are
easy to implement in the computer’s memory.
• In nonlinear data structures, a data element
can be attached to several other data
elements to represent specific relationships
that exist among them.
Convert the following General Tree to Binary tree

1.Root node of the General tree will be the Root node


of Binary tree.

2.LST of the root node in a General tree will be the


LST of Binary trees root node.

3.Closest sibling of LST in the general tree will be the


RST of Roots LST in binary tree.
Convert the following General Tree to Binary tree
Expression Tree
Binary Tree Traversal Methods

• In a traversal of a binary tree, each element of


the binary tree is visited exactly once.
• During the visit of an element, all action (make
a clone, display, evaluate the operator, etc.)
with respect to this element is taken.
Binary Tree Traversal Methods
• Preorder
• Inorder
• Postorder
• Level order
Preorder Traversal
void preOrder(treePointer ptr)
{
if (ptr != NULL)
{
visit(t);
preOrder(ptr->leftChild);
preOrder(ptr->rightChild);
}
}
Preorder Example (Visit = print)
a

b c

abc
Preorder Example (Visit = print)
a

b c
f
d e
g h i j

abdghei cf j
Preorder Of Expression Tree
/

* +
e f
+ -
a b c d

/ * +a b - c d +e f

Gives prefix form of expression!


Inorder Traversal

void inOrder(treePointer ptr)


{
if (ptr != NULL)
{
inOrder(ptr->leftChild);
visit(ptr);
inOrder(ptr->rightChild);
}
}
Inorder Example (Visit = print)
a

b c

bac
Inorder Example (Visit = print)
a

b c
f
d e
g h i j

gdhbei af j c
Inorder By Projection (Squishing)
a

b c
f
d e
g h i j

g d h b e i a f jc
Inorder Of Expression Tree
/

* +
e f
+ -
a b c d

a + b * c - d/ e + f

Gives infix form of expression (sans parentheses)!


Postorder Traversal

void postOrder(treePointer ptr)


{
if (ptr != NULL)
{
postOrder(ptr->leftChild);
postOrder(ptr->rightChild);
visit(t);
}
}
Postorder Example (Visit = print)
a

b c

bca
Postorder Example (Visit = print)
a

b c
f
d e
g h i j

ghdi ebj f ca
Postorder Of Expression Tree
/

* +
e f
+ -
a b c d

a b +c d - * e f + /

Gives postfix form of expression!


Traversal Applications
a

b c
f
d e
g h i j

• Make a clone.
• Determine height.
•Determine number of nodes.
Level Order
Let ptr be a pointer to the tree root.
while (ptr != NULL)
{
visit node pointed at by ptr and put its children
on a FIFO queue;
if FIFO queue is empty, set ptr = NULL;
otherwise, delete a node from the FIFO queue
and call it ptr;
}
Level-Order Example (Visit = print)
a

b c
f
d e
g h i j

abcdef ghi j
Binary Tree Construction
• Suppose that the elements in a binary tree
are distinct.
• Can you construct the binary tree from
which a given traversal sequence came?
• When a traversal sequence has more than
one element, the binary tree is not
uniquely defined.
• Therefore, the tree from which the
sequence was obtained cannot be
reconstructed uniquely.
Some Examples
preorder a a
= ab b b

inorder = ab b a
a b

postorder = b b
ab
a a

level order = ab a a
b b
Binary Tree Construction

• Can you construct the binary tree,


given two traversal sequences?
• Depends on which two sequences are
given.
Preorder And Postorder

preorder = ab a a
postorder = ba b b

• Preorder and postorder do not uniquely define a


binary tree.
• Nor do preorder and level order (same example).
• Nor do postorder and level order (same example).
Inorder And Preorder
• inorder = g d h b e i a f j c
• preorder = a b d g h e i c f j
• Scan the preorder left to right using the
inorder to separate left and right subtrees.
• a is the root of the tree; gdhbei are in the
left subtree; fjc are in the right subtree.
a

gdhbei fjc
Inorder And Preorder
a

gdhbei fjc

• preorder = a b d g h e i c f j
• b is the next root; gdh are in the left
subtree; ei are in the right subtree.
a

b fjc
gdh ei
Inorder And Preorder
a

b fjc
gdh ei
• preorder = a b d g h e i c f j
• d is the next root; g is in the left
subtree; h is in the right subtree.
a
b fjc
d ei
g h
Inorder And Postorder

• Scan postorder from right to left using


inorder to separate left and right subtrees.
• inorder = g d h b e i a f j c
• postorder = g h d i e b j f c a
• Tree root is a; gdhbei are in left subtree; fjc
are in right subtree.
Inorder And Level Order

• Scan level order from left to right using


inorder to separate left and right subtrees.
• inorder = g d h b e i a f j c
• level order = a b c d e f g h i j
• Tree root is a; gdhbei are in left subtree; fjc
are in right subtree.
Binary Search Trees (BST)
• A data structure for efficient searching,
insertion and deletion
• Binary search tree property
– For every node X
– All the keys in its left
subtree are smaller than
the key value in X
– All the keys in its right
subtree are larger than the
key value in X
Binary Search Trees

A binary search tree Not a binary search tree


Searching BST
• If we are searching for 15, then we are done.
• If we are searching for a key < 15, then we
should search in the left subtree.
• If we are searching for a key > 15, then we
should search in the right subtree.
Inorder Traversal of BST
• Inorder traversal of BST prints out all the
keys in sorted order

Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20


Insertion
• Proceed down the tree as you would with a find
• If X is found, do nothing (or update something)
• Otherwise, insert X at the last spot on the path traversed

• Time complexity = O(height of the tree)


void insert(int x, BinaryNode* t)
{
if (t==NULL) t = new
BinaryNode(x,NULL,NULL);
if (x<t->element) insert(x,t->left);
if (x>t->element) insert(x,t->right);
}
Deletion
• When we delete a node, we need to consider
how we take care of the children of the
deleted node.
– This has to be done such that the property of the
search tree is maintained.
Deletion under Different Cases
• Case 1: the node is a leaf
– Delete it immediately
• Case 2: the node has one child
– Adjust a pointer from the parent to bypass that
node
Deletion Case 3
• Case 3: the node has 2 children
– Replace the key of that node with the minimum
element at the right subtree
– Delete that minimum element
• Has either no child or only right child because if it has a
left child, that left child would be smaller and would have
been chosen. So invoke case 1 or 2.

• Time complexity = O(height of the tree)


void remove(double x, BinaryNode
* t)
{
if (t==NULL) return;
if (x<t->element) remove(x,t-
>left);
else if (t->element < x) remove
(x, t->right);
else if (t->left != NULL && t-
>right != NULL) // two children
{
t->element = finMin(t->right) ->element;
remove(t->element,t->right);
}
else
{
Binarynode* oldNode = t;
t = (t->left != NULL) ? t->left : t-
>right;
delete oldNode;
}
}

Vous aimerez peut-être aussi