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
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.
h
Pat
• Depth: The Depth of a node
ni is the length of the unique
path from the root to ni.
•A is the ancestor of G.
•K is the descendant of D
Tree Terminology
Application of Tree
• Storing naturally hierarchical data – File system.
• 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
• 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.
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
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
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)
Example:
Example:
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.
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.
• 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
-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.
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
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.
v. The number of keys in each internal node is one less than the number of its
child nodes.
Delete: T
Deletion from a B-Tree(cont’d)
Delete: R
Deletion from a B-Tree(cont’d)
Delete: E