• Tidak ada hasil yang ditemukan

COL106: Data Structures and Algorithms - IIT Delhi

N/A
N/A
Protected

Academic year: 2025

Membagikan "COL106: Data Structures and Algorithms - IIT Delhi"

Copied!
31
0
0

Teks penuh

(1)

Ragesh Jaiswal, IIT Delhi

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

(2)
(3)

Binary Search Trees

Consider the following implementation:

Code class Node{

public int key;

public String value;

public Node leftChild;

public Node rightChild;

public Node parent;

}

public class BST{

public int size;

public Node root;

public BST(){

size = 0;root = null;

}

public boolean isLeaf(Node N){//To be written}

public String get(int k){//To be written}

public void put(int k, String v){//To be written}

public void remove(int k){//To be written}

}

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

(4)

What is the worst case running time of each of the following operations?

get(k):

put(k, v):

remove(k):

(5)

Binary Search Trees

What is the worst case running time of each of the following operations?

get(k): O(n) put(k, v): O(n) remove(k): O(n)

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

(6)

What is the worst case running time of each of the following operations when the BST isbalanced?

get(k):

put(k, v):

remove(k):

A BST is perfectly balanced if for every internal node, there are equal number of nodes in its left and right sub-trees.

(7)

Binary Search Trees

What is the worst case running time of each of the following operations when the BST isbalanced?

get(k): O(logn) put(k, v): O(logn) remove(k): O(logn)

So, our next goal shall be to build balancedbinary search trees.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

(8)

Suppose we start with an empty BST and insert the keys 1,2,3,4, then the BST obtained is shown below.

(9)

Balanced Binary Search Trees

Suppose we start with an empty BST and insert the keys 1,2,3,4, then the BST obtained is shown below.

This tree is not balanced. Can you think of a way to balance this tree?

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

(10)

Suppose we start with an empty BST and insert the keys 1,2,3,4, then the BST obtained is shown below.

This tree is not balanced. Can you think of a way to balance this tree?

(11)

Balanced Binary Search Trees

Rotation for tree balancing.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

(12)

Tri-node restructuring for a nodex, its parenty, and its grandparentz.

Figure : Case #1

(13)

Balanced Binary Search Trees

Tri-node restructuring for a nodex, its parenty, and its grandparentz.

Figure : Case #2

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

(14)

Tri-node restructuring for a nodex, its parenty, and its grandparentz.

Figure : Case #3

(15)

Balanced Binary Search Trees

Tri-node restructuring for a nodex, its parenty, and its grandparentz.

Figure : Case #4

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

(16)

AVL Tree: An AVL tree is a binary search tree that satisfies the following property:

Height balance property: For every internal node of the tree, the heights of its children differ by at most 1.

(17)

Balanced Binary Search TreesAVL Trees

AVL Tree: An AVL tree is a binary search tree that satisfies the following property:

Height balance property: For every internal node of the tree, the heights of its children differ by at most 1.

Is the binary search tree below an AVL tree?

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

(18)

AVL Tree: An AVL tree is a binary search tree that satisfies the following property:

Height balance property: For every internal node of the tree, the heights of its children differ by at most 1.

Is the binary search tree below an AVL tree? No

(19)

Balanced Binary Search TreesAVL Trees

AVL Tree: An AVL tree is a binary search tree that satisfies the following property:

Height balance property: For every internal node of the tree, the heights of its children differ by at most 1.

Is the binary search tree below an AVL tree?

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

(20)

AVL Tree: An AVL tree is a binary search tree that satisfies the following property:

Height balance property: For every internal node of the tree, the heights of its children differ by at most 1.

Is the binary search tree below an AVL tree? Yes

(21)

Balanced Binary Search TreesAVL Trees

AVL Tree: An AVL tree is a binary search tree that satisfies the following property:

Height balance property: For every internal node of the tree, the heights of its children differ by at most 1.

Claim: The height of any AVL tree storingnnodes is O(logn).

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

(22)

AVL Tree: An AVL tree is a binary search tree that satisfies the following property:

Height balance property: For every internal node of the tree, the heights of its children differ by at most 1.

Claim: The height of any AVL tree storingnnodes is O(logn).

Letn(h) denote the minimum number of internal nodes in an AVL tree with heighth.

Try writing a recurrence relation forn(h) and solving it to get a lower bound.

(23)

Balanced Binary Search TreesAVL Trees

AVL Tree: An AVL tree is a binary search tree that satisfies the following property:

Height balance property: For every internal node of the tree, the heights of its children differ by at most 1.

Question: How do we performget(k) operation on an AVL tree?

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

(24)

AVL Tree: An AVL tree is a binary search tree that satisfies the following property:

Height balance property: For every internal node of the tree, the heights of its children differ by at most 1.

Question: How do we performget(k) operation on an AVL tree?

The same as BST

(25)

Balanced Binary Search TreesAVL Trees

AVL Tree: An AVL tree is a binary search tree that satisfies the following property:

Height balance property: For every internal node of the tree, the heights of its children differ by at most 1.

Question: How do we performput(k, v) operation on an AVL tree?

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

(26)

AVL Tree: An AVL tree is a binary search tree that satisfies the following property:

Height balance property: For every internal node of the tree, the heights of its children differ by at most 1.

Question: How do we performput(k, v)operation on an AVL tree?

Same as in BST. However, you also have to make sure that after insertion, the height balance property is maintained.

Consider inserting an entry with key 32 in the Tree below.

(27)

Balanced Binary Search TreesAVL Trees

AVL Tree: An AVL tree is a binary search tree that satisfies the following property:

Height balance property: For every internal node of the tree, the heights of its children differ by at most 1.

Question: How do we performput(k, v)operation on an AVL tree?

Same as in BST. However, you also have to make sure that after insertion, the height balance property is maintained.

Consider inserting an entry with key 32 in the Tree below.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

(28)

Question: How do we performput(k, v) operation on an AVL tree?

Algorithm

//p denotes the node that is inserted.

BalanceAfterPut(Node p)

- While going up fromp, letz denote the first node for which the height balance property is not satisfied.

- Lety be the child of z with greater height.

- Letx be the child of y with greater height.

- Perform a tri-node restructuring w.r.t. nodesx,y,z.

(29)

Balanced Binary Search TreesAVL Trees

Algorithm

//p denotes the node that is inserted.

BalanceAfterPut(Node p)

- While going up fromp, letzdenote the first node for which the height balance property is not satisfied.

- Letybe the child ofzwith greater height.

- Letxbe the child ofywith greater height.

- Perform a tri-node restructuring w.r.t. nodesx,y,z.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

(30)

Algorithm

//p denotes the node that is inserted.

BalanceAfterPut(Node p)

- While going up fromp, letzdenote the first node for which the height balance property is not satisfied.

- Letybe the child ofzwith greater height.

- Letxbe the child ofy with greater height.

- Perform a tri-node restructuring w.r.t. nodesx,y,z.

(31)

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

Referensi

Dokumen terkait

Fungsi deletenode( ) akan menghapus node pada posisi curr jika linked list tidak kosong dan memindahkan pointer curr ke posisi node pertama, atau curr bernilai NULL apabila linked

By using another type of data structure, such as a hash table (see Chapter 8, Hash Tables ) or a binary tree (see Chapter 9, Trees ) we can search the data considerably

Since the search tree guarantees that the left child has a value that is less than or equal to the parent and the right child has a value greater than or equal to the value of

Double Linked List 10.Linked List Collection dan Circular Linked List 11.Stack 12.Queues dan Priority Queues 13.Binary Tree dan Algoritma Traversal Tree 14.Binary Search Trees

Linked Structure for General Trees When representing a binary tree with a linked structure, each node explicitly maintains fields left and right as references to individual children..

return true; // success } // end delete // --- // returns node with next-highest value after delNode // goes to right child, then right child’s left descendents private Node

An optimal binary search tree is a BST, which has minimal expected cost of locating each node Search time of an element in a BST is On, whereas in a Balanced-BST search time is Olog

Various Trees 2 Binary Search Tree: A binary tree in which the left subtree of a node contains only values less than or equal to the node’s value the right subtree of a node