0% found this document useful (0 votes)
38 views16 pages

Ads 221fa04669

Uploaded by

keshrimuskan997
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)
38 views16 pages

Ads 221fa04669

Uploaded by

keshrimuskan997
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/ 16

ADVANCE DATA STRUCTURE

PRESENTED BY:-
MUSKAN KESHRI
221FA04669
SUBMITTED TO:-
SURESH BABU SIR
 QUESTION:-
Q7) Input Data: 10, 9, 2, 90, 53, 4, 64, 95, 59, 85, 90

By using the above set of data, perform the following:

i. Construct the AVL Tree .

ii. Construct the Splay Tree

ii. Calculate the total number of rotation required to construct the


above two trees.
 CONSTRUCTION OF AVL TREE:-

Insert Insert 9: Insert 2:


10: Insert Insert 9 as Insert 90:
Insert 2 as Insert 90 as
the node the left the left
as the child of 10. the right
child of 9. child of 10
root. Perform
right
rotation at
Insert 53: Insert 4:
Insert 53 as Insert 4 as the Insert 64: Insert
the left child of right child of 2. 64 as the right
90. Perform left child of 53.
rotation at 10. Perform left
rotation at 53.
•Insert 95:
Insert 95 as Insert 59: Insert
the right child Insert 85:
59 as the left
of 90. Insert 85 as
child of 64.
Balance the the left child of
Balance the tree
tree as 90.
as necessary.
necessary.
Insert 90: 90 is already present, so no
changes are needed.

Total Rotations for AVL


Tree:
4 rotations are required:
1 Right, 1 Left, and 2 Right-
Left rotations.

FINAL AVL TREE


 CONSTRUCTION OF SPLAY
TREE:-

Insert 10: Insert 9: Insert Insert 2:


Insert the Insert 90: Insert 53:
9 as the left Insert 2 as Insert 90 as
node as the Insert 53 as
child of 10. the left the right child
root since the left child
Then, splay 9 to child of 9. of 10. Then,
the tree is of 90. Then,
the root. Then, splay splay 90 to the
empty. splay 53 to
2 to the root. the root.
root.
Insert 4: Insert 64:
Insert 4 as Insert 64 as the Insert 95:
the left child right child of 53. Insert 95 as the
of 9. Then, Then, splay 64 right child of 90.
splay 4 to to the root. Then, splay 95
the root. to the root.
Insert 59:
Insert 59 as the
left child of 64.
Then, splay 59 Insert 85: Insert 85
to the root. as the left child of Insert
Total Rotations for 95. Then, splay 85
Splay Tree: 90: 90 is
to the root. already
•19 rotations
(Summing up all the present.
 CODE FOR AVL TREE:-
class Node: update height(root) balance =
getBalance(root)
int value
if balance > 1 and key <
Node left, right , root.left.value: // Left-Left Case
int height return rightRotate(root)
function insert(Node root, int key): if balance < -1 and key >
if root is null: root.right.value:
// Right-Right Case
return new Node(key)
return leftRotate(root)
if key < root.value:
if balance > 1 and key >
root.left = insert(root.left, key) root.left.value:
else if key > root.value: // Left-Right Case
root.right = insert(root.right, key) root.left =leftRotate(root.left)
return rightRotate(root) Node x = y.left
if balance < -1 and key < Node T2 = x.right
root.right.value: x.right = y
// Right-Left Case y.left = T2
root.right = update height(y)
rightRotate(root.right)
update height(x)
return leftRotate(root)
return x
return root
function leftRotate(Node x):
function rightRotate(Node Node y = x.right
y):
Node T2 = y.left
y.left = x
function getBalance(Node
x.right = T2 node):
update height(x) if node is null:
update height(y) return 0
return y return height(node.left) -
function update height(node.right)
height(Node node):
node.height = 1 +
max(height(node.left),
height(node.right))
 CODE FOR SPLAY TREE:-
class Node: return root
int value if key < root.left.value: // Zig-
Node left, right Zig Case (Left-Left)
function splay(Node root, int root.left.left =
key): splay(root.left.left, key)
if root is null or root.value == root = rightRotate(root)
key: else if key > root.left.value: //
return root Zig-Zag Case (Left-Right)
if key < root.value: root.left.right=splay(root.left.r
ight, key)
if root.left is null:
root = leftRotate(root)
if root.left.right is not null:
else if key <
root.left = root.right.value: // Zag-Zig
leftRotate(root.left) Case (Right-Left)
return root.left is null ? root.right.left =
root : rightRotate(root) splay(root.right.left, key)
else: if root.right.lef
if root.right is null: root.right =
return root rightRotate(root.right)
if key > root.right.value: // return root.right is null ?
Zag-Zag Case (Right-Right) root : leftRotate(root)
root.right.right = function rightRotate(Node
splay(root.right.right, key) y): Node x = y.left
if root is null:
Node T2 = x.right
return new Node(key)
x.right = y
root = splay(root, key)
y.left = T2
if root.value == key:
return x
return root
function leftRotate(Node x):
Node y = x.right
Node newNode = new
Node(key) if key <
Node T2 = y.left root.value:
y.left = x newNode.right = root
x.right = T2 newNode.left = root.left
return y root.left = null
function insert(Node root, int
else:
key):
newNode.left = root
newNode.right = root.right
root.right = null
return newNode

You might also like