Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 2P03 Week 51 Representation of an AVL Node class AVLNode { AVLnode left; AVLnode right; int height; int height(AVLNode T) { return T == null? -1 :

Similar presentations


Presentation on theme: "COSC 2P03 Week 51 Representation of an AVL Node class AVLNode { AVLnode left; AVLnode right; int height; int height(AVLNode T) { return T == null? -1 :"— Presentation transcript:

1 COSC 2P03 Week 51 Representation of an AVL Node class AVLNode { AVLnode left; AVLnode right; int height; int height(AVLNode T) { return T == null? -1 : T.height; } … };

2 COSC 2P03 Week 52 AVL insert AVLNode insert(AVLNode T, AVLNode newNode) { if(T == null) T = newNode; else if(newNode.info < T.info)// insert in left subtree { T.left = insert(T.left, newNode); if(height(T.left) - height(T.right) == 2) if(newNode.info < T.left.info)//left subtree of T.left T = rotateWithLeftChild(T); else//right subtree of T.left T = doubleWithLeftChild(T); } else// insert in right subtree { T.right = insert(T.right, newNode); if(height(T.right) - height(T.left) == 2) if(newNode.info > T.right.info)// right subtree of T.right T = rotateWithRightChild(T); else// left subtree of T.right T = doubleWithRightChild(T); } T.height = max(height(T.left), height(T.right)) + 1; return T; }

3 COSC 2P03 Week 53 Single rotation – left-left Insertion in left subtree of k2.left caused an imbalance at k2 : need to rebalance from k2 down. AVLNode rotateWithLeftChild(AVLNode k2) { AVLNode k1 = k2.left; k2.left = k1.right; k1.right = k2; k2.height = max(height(k2.left, height(k2.right)) + 1; k1.height = max(height(k1.left), k2.height) + 1; return k1; } Note: right-right case is symmetric to above (left↔right).

4 COSC 2P03 Week 54 Double rotation – right-left Insertion in right subtree of left child caused imbalance at k3 : need to rebalance from k3 down. AVLNode doubleWithLeftChild(AVLNode k3) { k3.left = rotateWithRightChild(k3.left); return rotateWithLeftChild(k3); } Note: left-right case is symmetric to above (left↔right).

5 B Trees (section 4.7 of textbook) Commonly used for organizing data on disk –Disk access time (time to read or write a block) dominates cost –Very important to minimize number of disk accesses Some notes on terminology: –This textbook uses the name B tree, but elsewhere they are known as B+ trees –In this textbook, the order of a B tree is the maximum number of children per node. –Elsewhere, order refers to the minimum number of children in index nodes other than the root. COSC 2P03 Week 55

6 B tree definitions A B-tree of order M is an M-ary tree such that: 1.Data items are only in the leaves 2.Non-leaf (index) nodes store up to M-1 keys: –key i determines the smallest possible key in subtree i+1. 3.The root is either a leaf or has between 2 and M children Every node other than the root is at least half-full: 4.All non-leaf nodes (except root) have at least  M/2  children 5.All leaves are at the same depth and have between  L/2  and L records. COSC 2P03 Week 56

7 B tree and Binary Search Tree comparison Binary search trees: nodes have 0, 1 or 2 children and 1 key B-trees: non-leaf nodes have up to M children: a node with d keys has d+1 children Binary search trees: data is stored in both leaf and non-leaf nodes, and for any given index node N: –N’s left subtree contains only items with keys < N’s key –N’s right subtree contains only items with keys > N’s key B-trees: non-leaf nodes contain up to M-1 keys (k 1, …, k M-1 ): –Subtree to left of k 1 contains only items with keys < k 1 –Subtree between k i and k i+1 contains only items with keys <k i+1 and ≥k i –Subtree to right of k M-1 contains only items with keys ≥k M-1 COSC 2P03 Week 57

8 8 B-tree Example M=5 and L=3 100150200 103050110120130160180220240260 3103050100110120130150160180200220240260 7153580105115125140155165190210225250270 2090170230275

9 COSC 2P03 Week 59 B-tree example: insert 40 100150200 103050110120130160180220240260 3103050100110120130150160180200220240260 7153580105115125140155165190210225250270 204090170230275

10 COSC 2P03 Week 510 B-tree example: insert 70 100150200 10305080110120130160180220240260 310305080100110120130150160180200220240260 715357090105115125140155165190210225250270 2040170230275

11 COSC 2P03 Week 511 B-tree example: insert 25 30100150200 10205080110120130160180220240260 31020305080100110120130150160180200220240260 71525357090105115125140155165190210225250270 40170230275

12 COSC 2P03 Week 512 B-tree example: insert 235 30100150200 10205080110120130160180220230240260 31020305080100110120130150160180200220230240260 71525357090105115125140155165190210225235250270 40170275

13 COSC 2P03 Week 513 B-tree example: insert 280 30100200240 10205080110120130160180220230260275 31020305080100110120130150160180200220230240260275 71525357090105115125140155165190210225235250270280 40170 150

14 COSC 2P03 Week 514 Heaps A heap is a binary tree that satisfies all of the following properties: Structure property: It is a complete binary tree Heap-order property: Every node satisfies the heap condition: –The key of every node n must be smaller than (or equal to) the keys of its children, i.e. n.info  n.left.info and n.info  n.right.info


Download ppt "COSC 2P03 Week 51 Representation of an AVL Node class AVLNode { AVLnode left; AVLnode right; int height; int height(AVLNode T) { return T == null? -1 :"

Similar presentations


Ads by Google