Presentation is loading. Please wait.

Presentation is loading. Please wait.

AVL TREES.

Similar presentations


Presentation on theme: "AVL TREES."— Presentation transcript:

1 AVL TREES

2 AVL TREES First-invented self-balancing binary search tree
Named after its two inventors, G.M. Adelson-Velsky and E.M. Landis, published it in their 1962 paper "An algorithm for the organization of information." AVL tree is a self balanced binary search tree. That means, an AVL tree is also a binary search tree but it is a balanced tree. In an AVL tree, balance factor of every node is either -1, 0 or +1.

3 AVL Properties An AVL tree is a balanced binary tree
To understand balance we need to understand the notion of Tree Height 55 Height 2 32 71 Height 1 64 86 Height 0

4 AVL Properties By default, nodes with no children have a height of Height of 0. 55 Height 2 Height 0 32 71 Height 1 Height 0 64 86 Height 0

5 AVL Properties But, we must also understand the concept of Sub-trees
Height = max(L.height, R.height) + 1 sub-tree L has a height of 0 sub-tree R has a height of 1 55 Height 2 Height 0 32 71 Height 1 Height 0 64 86 Height 0

6 AVL Properties Also empty sub-trees have a Height of -1
Height = max(L.height, R.height) + 1 44 Height = 2 = max(0, 1) + 1 58 Height = 1 = max(-1, 0) + 1 91 Height = 0 = max(-1,-1) + 1

7 AVL Properties Anyway, the AVL Balance Property is as follows...
For ALL nodes, the Height of the Left and Right Sub-trees can only differ by 1. P A Node L R

8

9 Single rotation : Rotate right

10 Right Rotate In RR Rotation every node moves one position to right from the current position.

11

12 Single rotation : left Rotate

13 Left-Rotate

14

15 Left-Right Rotate

16

17 Right-Left Rotate The RL Rotation is combination of single right rotation followed by single left rotation. In RL Roration, first every node moves one position to right then one position to left from the current position.

18

19 AVL tree? YES Each left sub-tree has height 1 greater than each right sub-tree NO Left sub-tree has height 3, but right sub-tree has height 1

20

21 Construct an AVL Tree by inserting numbers from 1 to 8.

22

23

24 SELF-ADJUSTING TREES

25 A strategy proposed by Brian Allen and Ian Munro and by James Bitner consists of two possibilities:
Single rotation. Rotate a child about its parent if an element in a child is accessed, unless it is the root (Figure 6.46a). Moving to the root. Repeat the child–parent rotation until the element being accessed is in the root (Figure 6.46b).  Restructuring a tree by using (a) a single rotation or (b) moving to the root when accessing node R.

26 Splay Tree

27 Splay Tree is a self - adjusted Binary Search Tree in which every operation on an element rearrange the tree so that the element is placed at the root position of the tree. Splaying an element is the process of bringing it to the root position by performing suitable rotation operations. In a splay tree, splaying an element rearrange all the elements in the tree so that splayed element is placed at root of the tree.

28 With the help of splaying an element we can bring most frequently used element closer to the root of the tree so that any operation on those element performed quickly. That means the splaying operation automatically brings more frequently used elements closer to the root of the tree.

29 Heap

30 Definition It is particular kind of binary tree which has the following two properties: Property #1 (Ordering): Nodes must be arranged in a order according to values based on Max heap or Min heap. Property #2 (Structural): All levels in a heap must full, except last level and nodes must be filled from left to right strictly. There are two types of heap data structures and they are as follows... Max Heap Min Heap

31

32 Max-heap Max heap is a specialized full binary tree in which every parent node contains greater or equal value than its child nodes. And last leaf node can be alone

33 Lets take example of 7 elements having values {1, 4, 3, 7, 8, ,9 ,10}.
we finally get a max- heapI I.e. 10,8,9,7,4,1

34 In the diagram below, initially 1st node (root node) is violating property of max-heap as it has smaller value than its children, so we are performing max_heapify function on this node having value 4.

35 Min Heap

36 Min Heap: In this type of heap, the value of parent node will always be less than or equal to the value of child node across the tree and the node with lowest value will be the root node of tree. As you can see in the above diagram, each node has a value smaller than the value of their children.

37 Example: Suppose you have elements stored in array Arr {4, 5, 1, 6, 7, 3, 2}. As you can see in the diagram below, the element at index 1 is violating the property of min -heap, so performing min_heapify(Arr, 1) will maintain the min-heap.

38 Consider elements in array {10, 8, 9, 7, 6, 5, 4} .

39 Applications of Heaps: 1) Heap sort: Heap Sort uses Binary Heap to sort an array in O(nLogn) time.
2) Priority Queue: Priority queues can be efficiently implemented using Binary Heap because it supports insert(), delete() and extractmax(), decreaseKey() operations in O(logn) time. Binomoial Heap and Fibonacci Heap are variations of Binary Heap. These variations perform union also efficiently. 3) Graph Algorithms: The priority queues are especially used in Graph Algorithms

40 Heap Operations When n is the number of elements (heap size),
Insertion  O(log2n) Deletion  O(log2n) Initialization  O(n)

41 Heap as Priority Queue:

42 Definition Priority Queue is similar to queue where we insert an element from the back and remove an element from front, but with a difference that the logical order of elements in the priority queue depends on the priority of the elements. The element with highest priority will be moved to the front of the queue and one with lowest priority will move to the back of the queue. Heaps can be used to implement priority queues.However, two procedures have to be implemented to enqueue and dequeue elements on a priority queue.

43 Enqueuing an element to a heap.
Initially there are 5 elements in priority queue. Insert Value 6

44 Dequeuing an element from a heap

45 Polish Notation and Expression Trees

46 A Binary Expression Tree
A special kind of binary tree in which: The leaves of a binary expression tree are operands, such as constants or variable names , and the other nodes contain operators Each leaf node contains a single operand Each nonleaf node contains a single binary operator The left and right subtrees of an operator node represent sub-expressions that must be evaluated before applying the operator at the root of the subtree.

47 Types of Polish Notations:
Infix expressions An operator appears between its operands Example: a + b Prefix expressions An operator appears before its operands Example: + a b Postfix expressions An operator appears after its operands Example: a b + Infix Expression Prefix Expression Postfix Expression A + B + A B A B + A + B * C + A * B C A B C * +

48 Easy to generate the infix, prefix, postfix expressions (how?)
* - 8 5 / z + 4 3 2 Infix: ( ( ) * ( ( ) / 3 ) ) Prefix: * / Postfix: / *


Download ppt "AVL TREES."

Similar presentations


Ads by Google