Sei sulla pagina 1di 86

Trees

Tree
• Definition :
• A tree is a finite set of one or
more nodes such that
– There is a specially designated
node called root.
– The remaining nodes are
partitioned into n>=0 disjoint
set T1,…,Tn, where each of
these sets is a tree. T1,…,Tn are
called the sub trees of the root.
• Every node in the tree is the
• Top node is called root.
root of some sub tree. • Except the root , each node has a
parent.
• Each node has 0 or more children.
Tree Terminology
• Node: Each element of a Node
Root
tree is called a node.
• Root: First node in the
hierarchical arrangement. Node

• Parent: Parent of a node is


the immediate predecessor
of a node.
• Child: Each immediate
successor of a node.
• Siblings: The child nodes of
a given parent node. •A is the root node.
•B is the parent of E and F.
•B, C, D are children of A
•H, I, J are siblings.
Tree Terminology
Level 0
• Degree of a node: The
number of sub trees of a
node in a given tree. Level 1

Level 2
• Level: The entire tree
structure is leveled in such a
way that the root node is
always at level 0. Then, its
Level 3
immediate children are at
level 1, and their immediate •Degree of node A is 3.
children at level2 and so on •Degree of node B is 2.
up to the terminal nodes. •. Degree of node D is 3.
•Degree of node H is 0.
Tree Terminology
• Edge: Is a connecting line
of two nodes.

• Forest: If we delete the


root and its edges
connecting the node at level
1, we obtain a set of disjoint
trees. A set of disjoint tree
is known as forest.
Tree Terminology
• Path: sequence of consecutive
edges from the source node to
the destination node.

h
Pat
• Depth: The Depth of a node
ni is the length of the unique
path from the root to ni.

• Height: The height of node •Depth of root is 0.


•Height of B is 2.
ni is the length of the longest •Depth of E is 2.
•Height of E is 1.
•Depth of B is 1.
path from ni to a leaf. •Height of all leaves
are 0.
Tree Terminology
• Ancestor & Descendant:
If there is a path from n1 to
n2 then n1 is an ancestor of
n2 and n2 is a descendant
of n1.

•A is the ancestor of G.
•K is the descendant of D
Tree Terminology
Application of Tree
• Storing naturally hierarchical data – File system.

• Can be used to evaluate arithmetic expressions.

• Organize data for quick search, insertion, deletion-


Binary Search Tree.

• Dictionary.
Binary Tree
• In Binary Tree, no node can have more than two children.
• Tree is defined as a finite set of elements, called nodes, such
as:
i. T is empty(called the null tree or empty tree), or
ii. T contains a special node R, called root node of T, and
the remaining nodes of T form an ordered pair of disjoint
binary trees T1 and T2, and they are called left and right
subtree of R.
iii. If T1 is non-empty then its root is called the left successor
of R, similarly if T2 is non-empty then its root is called
the right successor of R.
Binary Tree

Tree T consists of 10 nodes represented by A, B, C, D, E, F, G, H, I and J.

• B is the left successor and C is the right successor of the node A.

• The left subtree of the root A consists of the nodes B, D, E, H, and I and right
subtree of A consists of the nodes C, F, G and J.
Strictly Binary Tree
• The tree is said to be
strictly binary tree, if
every non-leaf node in a
binary tree has non
empty left and right
subtrees.

• A strictly binary tree with


n leaves always contains
2n-1 nodes.
Strictly Binary Tree
Is this tree a strictly binary tree?

Above tree is not a strictly binary tree.

Note: Every node in the strictly binary tree can have either no children or
two children. They are also called 2-tree or extended binary tree.
Complete Binary Tree
A complete binary tree at depth ‘d’ is the strictly binary
tree, where all the leaves are at level d.
Level 0

Level 1

Level 2

Fig: Complete Binary Tree of depth 2


Binary Tree
Note:
– A binary tree with n nodes, n>0, has exactly n-1 edges.

– A binary tree of depth d, d>0, has at least d and at most 2d-1


nodes in it.

– If a binary tree contains n nodes at level 1, then it contains at


most 2n nodes at level l+1.

– A complete binary tree of depth d is the binary tree of depth


d contains exactly 2l nodes at each level l between 0 and d.
Left Skewed & Right Skewed Binary Tree
• If a binary tree has only left sub • If a binary tree has only right sub
trees, then it is called left skewed trees, then it is called right
binary tree. skewed binary tree.
Binary Tree Representation
1. Sequential representation using array.
2. Linked list representation.

1. Array Representation:
- An array can be used to store the nodes of a binary tree.
- The nodes stored in an array of memory can be accessed sequentially.
Array Representation

Fig: Binary tree of depth 3

Fig: Array representation of the binary tree


Array Representation (cont’d)
• To identify the father, the left child and right
child of an arbitrary node:
1. The father of a node having index n can be
obtained by (n-1) / 2.
Example:
To find father node index of D, where array index n=3 is
(n-1) / 2 = (3-1) / 2 = 2/2 = 1
i.e., a[1] is the father of D, which is B.
Array Representation (cont’d)
2. The left child of a node having index n can be
obtained by (2n + 1).
Example:
To find the left child of C, where array index n=2. Then it can
be obtained by
(2n + 1) = 2*2 +1 = 4+1 = 5
i.e., A[5] is the left child of C, which is NULL. So no left child
for C.
3. The right child of a node having index n can be
obtained by (2n + 2).
Array Representation (cont’d)
Example:
To find the right child of B, where array index n=1. Then
(2n + 2) = 2*1 +2 = 4
i.e., A[4] is the right child of B, which is E.

4. If the left child is at array index n, then its right


brother is at (n+1). Similarly, if the right child is at
index n, then its left brother is at (n-1).
Linked list representation
• In linked list, every element is represented as nodes. A
node consists of three fields such as:
a. Left Child (LChild)
b. Information of the node (Info)
c. Right Child (RChild)

a. The LChild holds the address of the left child node of the
Parent node.
b. The Info holds the information of every node.
c. The RChild holds the address of the right child node of the
Parent node.
Linked list representation (cont’d)

Fig: Linked list representation of the


Fig: Binary tree binary tree
Operations on Binary Tree

• Create an empty binary tree


• Traversing a binary tree
• Inserting a new node
• Deleting a node
• Searching for a node
Traversing a Binary Tree
• Traversal is the process of going through a tree in such a way that each
node is visited once and only once.

• Traversal in a binary tree involves three kinds of basic activities


– Visiting the root
– Traverse the left subtree
– Traverse the right subtree

• Three standard ways of traversing a binary tree:


• Preorder Traversal (Root-Left-Right)
• Inorder Traversal (Left-Root-Right)
• Postorder Traversal (Left-Right-Root)
Preorder Traversal (Root-Left-Right)
• To traverse a non-empty binary
tree in preorder following steps
are processed:

1. Visit the root node.

2. Traverse the left subtree in


Preorder (recursively).

3. Traverse the right subtree in


Preorder (recursively).
Number 1 denote the first node in
preorder traversal and 7 denote the
last node.
Preorder Traversal (Root-Left-Right)
Example:

The preorder traversal of the above binary tree is A, B, D, E, H, I,


C, F, G, J.
Inorder Traversal (Left-Root-Right)
• To traverse a non-empty binary
tree in preorder following steps
are processed:

1. Traverse the left subtree in


Inorder (recursively).

2. Visit the root node.

3. Traverse the right subtree in


Inorder (recursively). Number 1 denote the first node
in inorder traversal and 7
denote the last node.
Inorder Traversal (Left-Root-Right)

Example:

The inorder traversal of the above binary tree is D, B, H,


E, I, A, F, C, J, G.
Postorder Traversal (Left-Right-Root)
• To traverse a non-empty binary
tree in postorder following steps
are processed:

1. Traverse the left subtree in


Postorder (recursively).

2. Traverse the right subtree in


Postorder (recursively).

3. Visit the root node.


Number 1 denote the first
node in postorder traversal
and 7 denote the last node.
Postorder Traversal (Left-Right-Root)

Example:

The postorder traversal of the above binary tree is D, H,


I, E, B, F, J, G, C, A.
Constructing Binary Tree
Constructing Binary Tree (cont’d)
Constructing Binary Tree (cont’d)
Constructing Binary Tree (cont’d)
Constructing Binary Tree (cont’d)
Constructing Binary Tree (cont’d)
Constructing Binary Tree (cont’d)
Constructing Binary Tree (cont’d)
Constructing Binary Tree (cont’d)
Constructing Binary Tree (cont’d)
Constructing Binary Tree (cont’d)
Maximum number of nodes at any level l

• The maximum number of nodes at any level l


is less than or equal to 2l where l=0,1,2,3,…l-1.
What is the total number of nodes N of a full tree with height h?
What is the height h of a full tree with N
nodes?
Expression Tree
• A binary tree built with operands and operators.
Also known as a parse tree.

• Binary tree associated with an arithmetic expression


– Internal nodes are operators.
– The leaves of an expression are operands.
Example : Expression Tree
• Arithmetic expression tree for the expression:
(2 * (a - 1) + (3 * b))
Binary Search Tree
• A binary tree T is termed as binary search tree if each node n
of T satisfies the following property:
 The value at n is greater than the value of all nodes in its
left subtree and
 The value at n is less than the values of all nodes in its right
subtree.
 The left and right subtree of a BST also BST.
Binary Search Trees (cont’d)
• Example
Difference between Binary Tree and Binary
Search Tree
• A binary tree is simply a tree in which each node can have at
most two children.

A binary search tree is a binary tree in which the nodes are


assigned values, with the following restrictions:

1. No duplicate values.
2. The left sub tree of a node can only have values less than the node.
3. The right sub tree of a node can only have values greater than the
node.
4. The left sub tree of a node is a binary search tree.
5. The right sub tree of a node is a binary search tree.
BST Operations
• Four basic BST operations
– Traversal

– Search

– Insertion

– Deletion
How to search a node in BST
1. The value is compared with the value of the root. If they are
equal, search terminates successfully.

2. If we meet an empty sub tree, which indicates that search is


unsuccessful.

3. If the given value is less than the value of the root then
search begins at the left subtree of the current root,
otherwise the right.
BST Search Algorithm
• Search(Root, Left, Right, Info, Item, Loc, Par)
Suppose an Item of information is given. This procedure finds the location
Loc of item in T and also the location Par of the parent of Item.
1. [Tree empty? ]
If Root = NULL, then: Set Loc:=NULL and Par := NULL, and Return.
2. [Item at Root?]
if Item = Info[Root], then set Loc := Root and Par : = NULL, and Return.
3. [Initialize pointers Ptr and Save]
If Item < Info[Root], then
set Ptr := Left[Root] and Save := Root.
Else:
Set Ptr := Right[Root] and save := Root.
[End of if Structure.]
BST Search Algorithm(cont’d)
4. steps 5 and 6 while Ptr ≠ NULL
5. [Item found?]
If Item = Info[Ptr], then
set Loc =: Ptr and Par := Save and Return.
6. If Item < Info[Ptr], then
set Save := Ptr and Ptr:= Left[Ptr].
Else:
Set Save := Ptr and Ptr := Right[Ptr].
[End of If Structure.]
[End of step 4 loop.]
7. [Search Unsuccessful.]
Set Loc := NULL and Par := Save.
8. Exit
Inserting in a BST
• Suppose an Item of information is given. The insertion
algorithm uses following procedure to insert the Item as a
new node in its appropriate place in the tree.
a. Compare Item with the Root node N of the tree.
i. If Item < N, proceed to the left child of N.
ii. If Item > N, proceed to the right child of N.
b. Repeat step a) until one of the following occurs
i. If we meet a node N such that Item = N, then Exit.
ii. We meet an empty subtree , which indicates that the
search is unsuccessful, and we insert Item in the place
of empty subtree.
BST Insertion Algorithm
• Insert(Info, Left, Right, Root, Node, Item, Loc)
A binary search tree T is in memory and an Item of
Information is given. This algorithm finds the location Loc of
Item in T and adds Item as a new node in T at location Loc.
1. Call Search(Info, Left, Right, Root, Item, Loc, Par)
2. If Loc ≠ NULL, then Exit.
3. [Copy Item into new node]
a. Create a new node named as Node.
b. Set Info[Node] = Item.
c. Set Loc := Node, Left[Node] := NULL and Right[Node] := NULL.
BST Insertion Algorithm(cont’d)
4. [Add Item to tree]
If Par = NULL, then
Set Root := Node.
Else if Item < Info[Par], then
Set Left[Par] := Node.
Else
Set Right[Par] := Node.
[End of If structure]
5. Exit
Deleting in a BST
• Suppose T is a binary search tree, and an Item of information is
given.
The deletion algorithm first uses procedure to find the location of
the node N which contains Item and also the location of the parent
node P(N). There are three ways to delete N from the tree:
Case 1: N has no children. Then N is deleted from T by simply
replacing the location of N in the parent node P(N) by the null
pointer.

Case 2: N has exactly one child. Then N is deleted from T by simply


replacing the location of N in P(N) by the location of the only one
child of N.
Deleting in a BST(cont’d)
Case 3: N has two children. Let S(N) denote the inorder
successor of N. Then N is deleted from T by first deleting S(N)
from T and then replacing node in T by the node S(N).
Binary Search Tree - Time Complexity
• Best case running time of BST is O(log N).
• If the input to BST in a sorted (ascending or descending )
manner, it will then look like this-

• It is observed that BST’s worst case performance is closest to


linear search algorithms, that is O(N).

• Problem : Lack of Balance


- It needs to compare depths of left and right
subtree.
AVL Trees
Balanced Binary Search Trees
• AVL tree is a self-balancing binary search tree.

• AVL tree is named after its two soviet inventors,


Adelson-Velskii and Landis, who published it in their
1962 paper.

• In an AVL tree, the height of the two child subtrees of


any node differ by at most one; if at any time they
differ by more than one, rebalancing is done.
AVL Trees
Balanced Binary Search Trees
• An AVL tree has the following properties:
– Sub trees of each node can differ by at most 1 in their height.
– Every sub trees is an AVL tree.

• Balance Factor:
In a binary tree the balance factor of a node N is defined to be the
height difference

Balance Factor (N) = Height of the left subtree – Height of the right
subtree.
A binary tree is called AVL tree iff
Balance Factor (N) { -1, 0. +1}
holds for every node N in the tree.
Example: AVL Tree

• If the difference in the height of left and right sub-


trees is more than 1, the tree is balanced using some
rotational techniques.
AVL Rotations
• To balance itself, an AVL tree may perform the
following four kinds of rotations −
– Left rotation
– Right rotation
– Left-Right rotation
– Right-Left rotation
Left Rotation
• If a tree becomes unbalanced, when a node is
inserted into the right subtree of the right subtree,
then we perform a single left rotation

-2

-1
Right Rotation
• AVL tree may become unbalanced, if a node is
inserted in the left subtree of the left subtree. The
tree then needs a right rotation.
Left-Right Rotation
• A left-right rotation is a combination of left
rotation followed by right rotation.

We first perform the


left rotation

on the left
subtree of C.
This makes A, the left
subtree of B.
Left-Right Rotation(cont’d)

Node C is still unbalanced. The tree needs a right The tree is now balanced.
rotation.
Right-Left Rotation
• Right-Left Rotation is a combination of right rotation
followed by left rotation.

-2

A node has been inserted into First, we perform the right rotation
the left subtree of the right along C node, making C the right
subtree. This makes A, an subtree of its own left subtree B.
unbalanced node with balance
factor -2.
Right-Left Rotation(cont’d)
-2

-1

Now, B becomes the right A becomes the left


A left rotation is performed.
subtree of A. subtree of its right
subtree B.
Node A is still unbalanced
because of the right The tree is now
subtree of its right subtree balanced.
and requires a left rotation.
B-Tree
• A B-tree of order m, is an m-way search tree in which

i. The root is either a leaf-node, or it has at least two child nodes and at most m
child nodes.

ii. The internal nodes except the root have at least [m/2] child nodes and at most
m child nodes.

iii. All leaves are on the same level.

iv. A leaf node contains no more than m-1 keys.

v. The number of keys in each internal node is one less than the number of its
child nodes.

• The number m should always be odd.


B-Tree
Example : B-Tree
Constructing a B-Tree
Constructing a B-Tree(cont’d)
CNGAHEKQMFWLTZDPRXYS
Constructing a B-Tree(cont’d)
CNGAHEKQMFWLTZDPRXYS
Constructing a B-Tree(cont’d)
CNGAHEKQMFWLTZDPRXYS
Constructing a B-Tree(cont’d)
CNGAHEKQMFWLTZDPRXYS
Constructing a B-Tree(cont’d)
CNGAHEKQMFWLTZDPRXYS
Constructing a B-Tree(cont’d)
CNGAHEKQMFWLTZDPRXYS
Constructing a B-Tree(cont’d)
CNGAHEKQMFWLTZDPRXYS
Constructing a B-Tree(cont’d)
CNGAHEKQMFWLTZDPRXYS
Deletion from a B-Tree
Delete: H
Deletion from a B-Tree(cont’d)

Delete: T
Deletion from a B-Tree(cont’d)
Delete: R
Deletion from a B-Tree(cont’d)

Delete: E

Potrebbero piacerti anche