Top Banner
Binary Search Tree
43

Binary Search Tree and AVL

Aug 20, 2015

Download

Education

Katang Isip
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Binary Search Tree and AVL

Binary Search Tree

Page 2: Binary Search Tree and AVL

PreliminariesSearch

ArrayListStack?Queue?For large amounts of input, the linear access

time of lists is prohibitive

Page 3: Binary Search Tree and AVL

PreliminariesTree

A collection of nodesThe collection can be emptyOtherwise, the tree consists of a distinguished

node r, called the root, and zero or more (sub)trees T1, T2, T3, …, Tk, each of whose roots are connected by a directed edge to r.

The root of each subtree is said to be a child of r, and r is the parent of each subtree root

Page 4: Binary Search Tree and AVL

Preliminaries

root

T1 T2 T3 T4 Tk

Page 5: Binary Search Tree and AVL

Preliminaries

TreeA tree is a collection of n nodes, one of which

is the root, and n-1 edges.Each edge connects some node to its parent and

every node except the root has one parent

Page 6: Binary Search Tree and AVL

Preliminaries

A

B C D FE G

H I J L M N O

P Q

LEAF LEAF

LEAF LEAF

LEAVES

LEAVES

INTERNAL NODES

ROOT

INTERNAL NODES

Page 7: Binary Search Tree and AVL

Binary TreeA binary tree is a tree in which no node

can have more than two children

root

TL TR

Page 8: Binary Search Tree and AVL

Binary Tree

A

D E

H I J L

P Q

Page 9: Binary Search Tree and AVL

ImplementationNodeclass node{public:

int item;node *left;node *right;node(int x) { item = x; left = right = NULL; }node( ) { item = 0; left = right = NULL; }

};

Page 10: Binary Search Tree and AVL

Expression Trees+

+ *

a *

b c

+ g

* f

d e

(a+b*c)+((d*e+f)*g)

Page 11: Binary Search Tree and AVL

Binary Search TreeAn application of binary trees is their use in

searchingLet us assume that each node in the tree is

assigned a key value, and assume that this is an integer

The property that makes a binary tree into a binary search tree is that for every node, X, in the tree, the values of all keys in the left subtree are smaller than the key value in X, and the values of all keys in the right subtree are larger than the key value in X.

Page 12: Binary Search Tree and AVL

Binary Search Tree

6

2 8

1 4

3

6

2 8

1 4

3 7

Page 13: Binary Search Tree and AVL

Binary Search Treeclass BST{private:

int size;node *root;

public:BST() {size = 0; root = NULL;}void insert(int);bool delete(int);bool search(int);int minimum();int maximum();

};

Page 14: Binary Search Tree and AVL

Binary Search Tree (Search)

6

2 8

1 4

3

20

10 50

Search for 10

Page 15: Binary Search Tree and AVL

Binary Search Tree (Search)bool BST::search(int x){

node *tmp = root;while(tmp!=NULL){

if(x == tmp->item)return true;

if(x < tmp->item)tmp = tmp->left;

elsetmp = tmp->right;

}return false;

}

Page 16: Binary Search Tree and AVL

Binary Search Tree (Minimum)

6

2 8

1 4

3

20

10 50-5

-1

Page 17: Binary Search Tree and AVL

Binary Search Tree (Minimum)

int BST::minimum(){

node *tmp = root;

while(tmp->left != NULL)

tmp = tmp -> left;

return temp->item;

}

Page 18: Binary Search Tree and AVL

Binary Search Tree (Insert)

6

2 8

1 4

3

20

10 50

Insert 20, 10, 50

Page 19: Binary Search Tree and AVL

Binary Search Tree (Insert)

Let’s insert 6, 2, 4, 3, 1, 8 and 11 in an empty BST

6

2 8

1 4

3

11

Page 20: Binary Search Tree and AVL

Binary Search Tree (Insert)Try inserting 1, 2, 3, and 4 in an empty

BST.1

2

3

4

Page 21: Binary Search Tree and AVL

Binary Search Tree (Insert)void BST::insert(int x){

node *n = new node(x);node *tmp = root;if(tmp = NULL)

root = n;else{

node *tmp2;while(tmp!=NULL){

tmp2 = tmp;if(x < tmp->item)

tmp = tmp->left;else

tmp = tmp->right;}if(x < tmp2->item)

tmp2->left = n;else

tmp2->right = n;}

}

Page 22: Binary Search Tree and AVL

BST (Delete)In deletion, we don’t ask for a position.

We ask for the actual item that has to be deleted.

6

2 8

1 4

3

Deleting a leaf

11

Deleting a node with one child

Deleting a node with two children

Page 23: Binary Search Tree and AVL

Deleting a Leaf (-1)

6

2 8

1 4

3

20

10 50-5

-1

Page 24: Binary Search Tree and AVL

Deleting a Node with a Child(-5)

6

2 8

1 4

3

20

10 50-5

-1

Page 25: Binary Search Tree and AVL

Deleting a node with two children (2)

6

2 8

1 4

3

20

10 50-5

-1

3

Page 26: Binary Search Tree and AVL

DeleteNode Code

bool BST::deleteNode(int x){

node *del = searchNode(x);

if(del->left == NULL && del->right==NULL)

delete del; //leaf

else{

}

}

Page 27: Binary Search Tree and AVL

One Child

if(del->left==NULL)

del = del->right;

else

if(del->right==NULL)

del = del->left;

Page 28: Binary Search Tree and AVL

Two Children

else{

node *ptr = minimum(del->right);

int x = ptr->item;

deleteNode(x);

del->item = x;

}

Page 29: Binary Search Tree and AVL

Running of Operations

Linear

1

2

3

4

Page 30: Binary Search Tree and AVL

Discussion We did not achieve our goal of log n. Can we improve? Always keep the tree balanced A

D E

H I J L

P Q

Page 31: Binary Search Tree and AVL

Adelson-Velski Landis (AVL) Tree

An AVL tree is a binary search tree where every node of the tree satisfies the following property:The height of the left and right subtrees of a

node differs by at most one.

Page 32: Binary Search Tree and AVL

Adelson-Velski Landis (AVL) Tree

Page 33: Binary Search Tree and AVL

Adelson-Velski Landis (AVL) Tree

In order to properly implement the AVL, the node has to be redefined.

class node{public:

int item;node *left;node *right;int height;node(int x) { item = x; left = right = NULL; }node( ) { item = 0; left = right = NULL; }

};

Page 34: Binary Search Tree and AVL

Adelson-Velski Landis (AVL) Tree

What kinds of violations may occur in a regular BST that will result in height imbalance?

1

2

3

3

2

1

3

1

2

Page 35: Binary Search Tree and AVL

Right Rotate

1

2

3

2

31

Page 36: Binary Search Tree and AVL

Left-Rotate

2

31

Page 37: Binary Search Tree and AVL

Left-Right Rotate

3

1

2

2

31

Page 38: Binary Search Tree and AVL

Right-Left Rotate

3

2

1

3

2

1

2

31

Page 39: Binary Search Tree and AVL

Challenge

Insert the following items in an AVL10, 20, 30, 40, 50, 60, 70, 80, 71, 61, 51, 41,

31, 21, 11

Page 40: Binary Search Tree and AVL

Right Rotate

void BST::rightRotate(node *r){

node *p = r->left;

r->left = p->right;

p->right = r;

//fill in the missing code

}

Page 41: Binary Search Tree and AVL

Left Rotate

void BST::leftRotate(node *r){

node *p = r->right;

r->right= p->left;

p->left= r;

//fill in the missing code

}

Page 42: Binary Search Tree and AVL

Other Rotations

I leave this as an exercise

Page 43: Binary Search Tree and AVL

Insertvoid BST::insert(int x){

//do insert as in BST

current = x;

set current to balanced

do{

previous = x;

update height of current

lh = height of the left subtree of current

rh = height of the left subtree of current

set current as left leaning, right leaning, or balanced

if(violation occurs)

perform corresponding rotation

}while(??);

}