0% found this document useful (0 votes)
5 views

Trees

The document provides an overview of tree data structures, including key terminologies such as nodes, roots, leaves, and types of trees like binary trees and AVL trees. It explains different methods for representing binary trees in memory, traversal techniques, and operations such as insertion and deletion in binary search trees and AVL trees. Additionally, it introduces multiway search trees, highlighting their characteristics and structure.

Uploaded by

tryweb157
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Trees

The document provides an overview of tree data structures, including key terminologies such as nodes, roots, leaves, and types of trees like binary trees and AVL trees. It explains different methods for representing binary trees in memory, traversal techniques, and operations such as insertion and deletion in binary search trees and AVL trees. Additionally, it introduces multiway search trees, highlighting their characteristics and structure.

Uploaded by

tryweb157
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 65

Terminology of trees

• Tree is a hierarchical data structure which stores the information naturally in the form of hierarchy unlike
linear data structures like, Linked List, Stack, etc. A tree contains nodes(data) and connections(edges) which
should not form a cycle.
• Following are the few frequently used terminologies for Tree data structure.
• Node — A node is a structure which may contain a value or condition, or represent a separate data structure.
• Root — The top node in a tree, the prime ancestor.
• Child — A node directly connected to another node when moving away from the root, an immediate
descendant.
• Parent — The converse notion of a child, an immediate ancestor.
• Leaf — A node with no children.
• Internal node — A node with at least one child.
• Edge — The connection between one node and another.
• Depth — The distance between a node and the root.
• Level — the number of edges between a node and the root + 1
• Height — The number of edges on the longest path between a node and a descendant leaf.
• Breadth — The number of leaves.
• Sub Tree — A tree T is a tree consisting of a node in T and all of its descendants in T.
• Binary Tree — is a tree data structure in which each node has at most two children, which are referred to as
the left child and the right child.
• Binary Search Tree — is a special type of binary tree which has the following properties.
• The left subtree of a node contains only nodes with keys lesser than the node’s key.
• The right subtree of a node contains only nodes with keys greater than the node’s key.
• The left and right subtree each must also be a binary search tree.
a

b
c

d
e

y
Representing Binary Tree in memory
• Let T be a Binary Tree. There are two ways of representing T in
the memory as follow
1. Sequential Representation of Binary Tree.
2. Link Representation of Binary Tree.

Sequential representation of Binary Tree


Let us consider that we have a tree T. let our tree T is a binary tree
that us complete binary tree. Then there is an efficient way of
representing T in the memory called the sequential representation
or array representation of T. This representation uses only a linear
array TREE as follows:
3. The root N of T is stored in TREE [1].
4. If a node occupies TREE [k] then its left child is stored in TREE
[2 * k] and its right child is stored into TREE [2 * k + 1].
• Ad by Valueimpression
Representing Binary Tree in memory
• For Example:
• Consider the following Tree:

• D,H, B, A, E, C, F
• Its sequential representation is as follow:
Representing Binary Tree in memory
Linked Representation of Binary Tree

• Consider a Binary Tree T. T will be maintained in memory by


means of a linked list representation which uses three parallel
arrays; INFO, LEFT, and RIGHT pointer variable ROOT as
follows. In Binary Tree each node N of T will correspond to a
location k such that
1. LEFT [k] contains the location of the left child of node N.
2. INFO [k] contains the data at the node N.
3. RIGHT [k] contains the location of right child of node N.
Representing Binary Tree in memory
Representation of a node:
• In this representation of binary tree root will contain the location of the
root R of T. If any one of the subtree is empty, then the corresponding
pointer will contain the null value if the tree T itself is empty,
the ROOT will contain the null value.
• Example: Consider the binary tree T in the figure. A schematic diagram of
the linked list representation of T appears in the following figure
• Binary Tree

Linked Representation of the Binary Tree
Binary search trees
120,230,125,45,67,89,150,760,54,43,32,11,250,260,33,67,
Insertion in BST 120

45 230

43 125 760
67

32 89 150 250
54

11
33 260
Datastructuresandalgorithms
d

a t

c s u

l o

g m

h
traversals
• Inorder: left root right
• Preorder: root left right
• Postorder: left right root
Deletion from Binary search trees

Three cases:
1. When node is leaf node
2. When node has single child node
3. When node has both child nodes
Case 1: Delete 260
120

45 230

43 125 760
67

32 89 150 250
54

11
260
Case 1: Delete 260
120

45 230

43 125 760
67

32 89 150 250
54

11
260
Case 2: Delete 250
120

45 230

43 125 760
67

32 89 150 250
54

11
260
Case 2 : delete 250
120

45 230

43 125 760
67

32 89 150 260
54

11
Case 2: Delete 250
120

45 230

43 125 760
67

32 89 150 260
250
54

11
260

250 has single child so child move upwards and


take its parent’s position i.e. 260 will replace 250.
Case 3: Delete 230
120

45 230

43 125 760
67

32 89 150 250
54

11
260

Inorder: 11, 32,43, 45, 54, 67, 89, 120, 125, 150, 230, 250, 260, 760
230 has both child nodes so to delete 230 we need to find the inorder
successor of 230 and delete the inorder successor from its position
using case1 or case 2 and replace 230 by it. Inorder successor of 230 is
250 so first delete 250 from its position, moving 260 and replacing 230
by 250.
After deleting 230
120

45 250

43 125 760
67

32 89 150 260
54

11

120, 45, 43, 32, 11, 67, 54, 89, 250, 125, 150, 760, 260
11, 32, 43, 54, 89, 67, 45, 150, 125, 260, 760, 250, 120
Creating Binary tree from traversal
• Inorder: 4,2,1,7,5,8,3,6
• Preorder: 1,2,4,3,5,7,8,6 1

2 3

4 6
5

7 8
Creating Binary tree from traversal
• Postorder: 9,1,2,12,7,5,3,11,4,8
• Inorder: 9,5,1,7,2,12,8,4,3,11
8

5 4

11
9 7

1 3
12

2
AVL trees
• It is observed that BST's worst-case performance is closest to
linear search algorithms, that is Ο(n). In real-time data, we
cannot predict data pattern and their frequencies. So, a need
arises to balance out the existing BST.
• Left skewed
• Right skewed
AVL trees
• Named after their inventor Adelson, Velski & Landis, AVL trees are
height balancing binary search tree. AVL tree is a binary search tree in
which the difference of heights of left and right subtree of any node is less
than or equal to one. AVL tree checks the height of the left and the right
sub-trees and assures that the difference is not more than 1. This
difference is called the Balance Factor.
• If the difference in the height of left and right sub-trees is more than 1, the
tree is balanced using some rotation techniques.
An AVL tree can be defined as follows:
Let T be a non-empty binary tree with TL and TR as its left and right subtrees.
The tree is height balanced if:
• TL and TR are height balanced
• hL - hR <= 1, where hL and hR are the heights of TL and TR
• The Balance factor of a node in a binary tree can have value 1, -1, 0,
depending on whether the height of its left subtree is greater, less than or
equal to the height of the right subtree.

• Balance Factor (k) = height (left(k)) - height (right(k))


Balance Factor

Example
AVL Trees
• This factor indicates whether the tree is left-heavy (the height
of the left sub-tree is 1 greater than the right sub-tree),
balanced (both sub-trees are the same height) or right-heavy
(the height of the right sub-tree is 1 greater than the left sub-
tree). If the balance would be destroyed by an insertion, a
rotation is performed to correct the balance.
AVL Insertion and Rotations
• AVL Rotations
• To balance itself, an AVL tree may perform the
following four kinds of rotations −

• Left rotation: LL
• Right rotation: RR
• Left-Right rotation: LR
• Right-Left rotation: RL
• The first two rotations are single rotations and
the next two rotations are double rotations. To
have an unbalanced tree, we at least need a tree
of height 2.
Left Rotation
• If a tree becomes unbalanced, when a node is
inserted into the left subtree of the left
subtree, then we perform a single left rotation

Right Rotation
• AVL tree may become unbalanced, if a node is
inserted in the right subtree of the right
subtree. The tree then needs a right rotation.
Left-Right Rotation
• Double rotations are slightly complex version of already
explained versions of rotations. Let's first check how to
perform Right-left rotation. A right-left rotation is a
combination of left rotation followed by right rotation.
• A node has been inserted into the right subtree of the left
subtree. This makes C an unbalanced node. These
scenarios cause AVL tree to perform left-right rotation.
We first perform the left rotation on
the left subtree of C. This makes A,
the left subtree of B.

Node C is still unbalanced, however


now, it is because of the left-subtree
of the left-subtree.
• We shall now right-rotate the tree, making B
the new root node of this subtree. C now
becomes the right subtree of its own left
subtree.

Final tree
Example
Insert 12,13,17,18,19,20
Step 1: insert 12 12 BF=-1
Step 2: insert 13 BF=0
13

Step 3: insert 17.after inserting 17 bf of Becomes -2 so


RR rotation needs to be done.
Bf=-2
12

13 Bf=-1

17 Bf=0
Tree becomes
13
12 17
• 12,13,17,18,19,20
Step 4: insert 18 13
12 17 Bf=-1

Step 5 insert 19 18
Bf=0
BF of node 17 becomes -2 so
RR rotation needs to be 13
done on it.
12 17 Bf=-2
After rotation
Tree becomes 18 Bf=-1
13

12 18 19 Bf-0

17 19
• 12,13,17,18,19,20
BF of 13

becomes -2 so RR
rotation needs to be done
20
18

13 19

17 20
12
RL rotation
Let this be the tree after insertion of 25

30

20 50

28 80

25
Balance factor of node 20 becomes -2. insertion was in left
subtree of its right subtree so RL rotation which is a combination of LL
and RR rotation on node
30 30

50 25 50
20

80 28 80
25
20
28 28
Deletion in AVL trees
R0 rotation (Node B has balance factor 0 )
• If the node B has 0 balance factor, and the balance factor of node A
disturbed upon deleting the node X, then the tree will be
rebalanced by rotating tree using R0 rotation.
• The critical node A is moved to its right and the node B becomes
the root of the tree with T1 as its left sub-tree. The sub-trees T2 and
T3 becomes the left and right sub-tree of the node A.
Example
Deletion in AVL trees
• R1 Rotation (Node B has balance factor 1)
• R1 Rotation is to be performed if the balance factor of Node B is 1.
In R1 rotation, the critical node A is moved to its right having sub-
trees T2 and T3 as its left and right child respectively. T1 is to be
placed as the left sub-tree of the node B.
Deletion in AVL trees
Deletion in AVL trees
R-1 Rotation (Node B has balance factor -1)

• R-1 rotation is to be performed if the node B has balance factor -1. This
case is treated in the same way as LR rotation. In this case, the node C,
which is the right child of node B, becomes the root node of the tree with
B and A as its left and right children respectively.
• The sub-trees T1, T2 becomes the left and right sub-trees of B whereas,
T3, T4 become the left and right sub-trees of A.
Deletion in AVL trees
Example
• Delete 20

• Deleting 20 tree becomes and balance factor of 18 becomes 2


so it needs to be balanced
18

19
13

12 17

10
Example
• R1 rotation needed

13

12 18

10
17 19
Multiway search trees: m-way trees
• A multiway tree is a tree that can have more than two children
• A multiway tree of order m88 (or an **m-way tree) is one in which a tree
can have m children.
• An m-way search tree is a m-way tree in which:
• a) Each node has m children and m-1 key fields
• b) The keys in each node are in ascending order.
• c) The keys in the first i children are smaller than the ith key
• d) The keys in the last m-i children are larger than the ith key
Multiway search trees: m-way trees
• A binary search tree has one value in each node and two subtrees. This
notion easily generalizes to an M-way search tree, which has (M-1)
values per node and M subtrees. M is called the degree of the tree. A
binary search tree, therefore, has degree 2.

• In fact, it is not necessary for every node to contain exactly (M-1) values
and have exactly M subtrees. In an M-way subtree a node can have
anywhere from 1 to (M-1) values, and the number of (non-empty)
subtrees can range from 0 (for a leaf) to 1+(the number of values). M is
thus a fixed upper limit on how much data can be stored in a node.

• The values in a node are stored in ascending order, V1 < V2 < ... Vk (k <=
M-1) and the subtrees are placed between adjacent values, with one
additional subtree at each end. We can thus associate with each value
a `left' and `right' subtree, with the right subtree of Vi being the same
as the left subtree of V(i+1). All the values in V1's left subtree are less
than V1 ; all the values in Vk's subtree are greater than Vk; and all the
values in the subtree between V(i) and V(i+1) are greater than V(i) and
less than V(i+1).
m-way search trees
• M-way search trees give the same advantages to m-way trees
that binary search trees gave to binary trees - they provide fast
information retrieval and update.
• However, they also have the same problems that binary search
trees had - they can become unbalanced, which means that the
construction of the tree becomes of vital importance.
• In m-way search tree, each sub-tree is also a m-way search tree
and follows the same rules.
• An extension of a multiway search tree of order m is a B-tree of
order m.
• This type of tree will be used when the data to be
accessed/stored is located on secondary storage devices
because they allow for large amounts of data to be stored in a
node.
B trees
• A B-tree is a tree data structure that keeps data sorted and allows searches, insertions,
and deletions in logarithmic amortized time. Unlike self-balancing binary search trees, it is
optimized for systems that read and write large blocks of data. It is most commonly used
in database and file systems.
• Every B-tree depends on a positive constant integer called MINIMUM, which is used to
determine how many elements are held in a single node.
• Rule 1: The root can have as few as one element (or even no elements if it also has no
children); every other node has at least MINIMUM elements.
• Rule 2: The maximum number of elements in a node is twice the value of MINIMUM.
• Rule 3: The elements of each B-tree node are stored in a partially filled array, sorted from
the smallest element (at index 0) to the largest element (at the final used position of the
array).
• Rule 4: The number of subtrees below a nonleaf node is always one more than the
number of elements in the node.
– Subtree 0, subtree 1, ...
• Rule 5: For any nonleaf node:
– An element at index i is greater than all the elements in subtree number i of the node, and
– An element at index i is less than all the elements in subtree number i + 1 of the node.
• Rule 6: Every leaf in a B-tree has the same depth. Thus it ensures that a B-tree avoids the
problem of a unbalanced tree.
B trees
Deletion
30

8, 25 40, 60

2, 7 9, 17, 20 26, 29 31, 33 48, 50 70, 80

Deletion from a B tree: From leaf node


Case 1: If leaf node has more than minimum delete it
from the node. eg. Delete 9.
Deletion
30

8, 25 40, 60

2, 7 9, 17, 20 26, 29 31, 33 48, 50 70, 80

Case 2 : If leaf node has only minimum, then we can borrow from its neighboring
nodes. Eg. delete 2. node only has minimum number of key values deleting 2 will
violate the condition of b tree. So 9 moves up in the parent and intermediate parent
i.e 8 moves down 30

9, 25 40, 60

2, 8 9, 17, 20 26, 29 31, 33 48, 50 70, 80


Example
5 way b tree: 120,130,140,10,20,30,40,50,60,200,300
Insert 120,130,140 120, 130,140

Insert 10
10, 120, 130,140

Insert 20: node is already full so it will split and tree becomes
10, 20 120, 130,140

120

10, 20 130, 140


5 way b tree: 120,130,140,10,20,30,40,50,60,200,300

Insert 30, 40

120

10,20,30,40 130,140
, 50
Insert 50: it will be inserted in node 10,20,30,40
Which is already full so splitting it we get
30,120

10,20 130, 140


BST Traversal
• Postorder(root)
1. if (root = NULL) return
2. Postorder(node->left)
3. Postorder(node->right)
4. Print root->info
BST Traversal

Inorder(root)
1. if (root =NULL) return
2. Inorder(node->left);
3. Print root->info
4. Inorder(root->right)

BST Traversal

Preorder(root)
1. if (root =NULL) return;
2. Print root->info
3. Inorder(node->left);
4.Inorder(root->right)

Searching in a Binary Search Tree
• Search (root, item)
1. If root->info= item or root= null then return
root
2. If item < root -> data
Return search(root -> left, item)
• Else Return search(root -> right,item)
• [End of if]
• [End of if]
• Step 2: end
Insertion in a Binary Search tree
• Insert(p, int item)

1. Create newnode
2. Newnode->info=item, newnode->left=null, newnode->right=null
3. if (p= NULL) then p =newnode and return
4. if (item < p->item)
p->left = insert(p->left, item);
else if (item> p->info)
p->right = insert(p->right, item);
else if (item=p->info)
write duplicate entry and exit
5. return p;

You might also like