efficient binary search trees...balanced binary search trees • avl tree – adelson-velskii and...

34
Efficient Binary Search Trees Binary Search Tree height can be as large as N Complexity: Search, Insert, Delete O(n) We want a tree with small height A binary tree with N node has height at least Θ(log N) Our goal keep the height of a binary search tree O(log N)

Upload: others

Post on 21-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

Efficient Binary Search Trees

•  Binary Search Tree •  height can be as large as N •  Complexity: Search, Insert, Delete

•  O(n)

•  We want a tree with small height •  A binary tree with N node has height at least

•  Θ(log N)

•  Our goal •  keep the height of a binary search tree O(log N)

Page 2: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

balanced binary search trees

•  AVL tree – Adelson-Velskii and Landis

•  Red-black tree

Page 3: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

AVL Tree

•  an empty tree is height-balanced

•  If T is a nonempty binary tree with TL and TR as its left and right subtrees respectively, then T is height-balanced iff

•  (1) TL and TR are height-balanced and

•  (2) | hL- hR | ≤ 1 where hL and hR are the heights of TL and TR respectively

Page 4: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

Balance Factor

•  The balance factor, BF(T), of a node T in a binary tree – hL- hR

•  For any node T in an AVL tree – BF(T)= -1, 0, or 1.

Page 5: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

AVL Tree?

0

0 0 1

1 -1 -1 2

0 0

0

1 -1

Page 6: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

MAR

MAY

MAR

(a) Insert MAR (b) Insert MAY

-1

0

0

Page 7: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

•  Insertion may leads to unbalancing! •  Rebalance it!

MAR

(c) Insert NOV

-2

NOV

MAY

-1

0

MAR

-1

MAY

0

Page 8: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

•  LL – BF(A) = 2 – Caused by insertion to the

left-subtree of A’s left-child

A

B

C A

B

C

A

B

C A

B

C

•  RR – BF(A) = -2 – Caused by insertion to the

right-subtree of A’s right-child

Page 9: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

A

B

C A B

C

•  LR – BF(A) = 2 – Caused by insertion to the

right-subtree of A’s left-child

•  RL – BF(A) = -2 – Caused by insertion to the left

-subtree of A’s right-child

A

B

C

A

B

C

A B

C

Page 10: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

0 13

0 37

0 24

Build an AVL Tree ( 13,24,37,90,53)

0 13

0 37

-1 13

0 24 -1 24

-2 13

0 90

-1 24

-1 37

0 53

1 90

-2 37

0 37

0 90

0 53

0 37

0 90

0 53

Page 11: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

BL

BR

AR

h h+2 LL

0 B

0 A

BL

BR

AR

h+2

+2 A

+1 B

Page 12: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

0 B

0 A

AL

BL

BR

h+2 RR

-2 A

-1 B

AL

BL

BR

Page 13: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

+2 A

-1 B LR(a)

0 C

0 A 0

B

0 C

Page 14: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

+2 A

-1 B

BL

CL

AR

h h+2 LR(b)

0 C

-1 A

BL

CR

AR

h+2 +1 C

CR

0 B

CL

Page 15: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

+2 A

-1 B

BL

CL

AR

h h+2 LR(c)

0 C

0 A

BL

CR

AR

h+2 -1 C

CR

+1 B

CL

Page 16: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

LR

LL

RR

RL Insert 16,3,7,11,9,26,18

Page 17: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

Notations

•  The height of the subtree involved in the rotation is the same after rebalancing as it was before

•  The only nodes whose BF can change are those in the subtree that is rotated.

Page 18: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

Notations

•  Node A –  the nearest ancestor of Y, whose BF becomes ±2

–  the nearest ancestor with BF= ±1 before insertion.

•  Before the insertion, the BF’s of all nodes on the path from A to the new insertion point must have been 0

•  To complete the rotation, the parent of A, F is also needed (Why?)

Page 19: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

Notations

•  Whether or not the restructuring is needed, the BF’s of several nodes change

•  Let A be the nearest ancestor of the new node with BF=±1 before the insertion –  If no such an A, let A be the root.

– The BF’s of nodes from A to the parent of the new node will change to ±1

Page 20: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

•  template <class K, class E> class AvlNode { •  friend class AVL<K, E>; •  public: •  AvlNode(const K& k, const E& e) •  {key=k; element=e; bf=0; •  leftChild=rightChild=0;} •  private: •  K key; •  E element •  int bf;; •  AvlNode<K, E> *leftChild, *rightChild; •  };

Page 21: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

•  template <class K, class E> •  class AVL { •  public: •  AVL(): root(0) { }; •  E& Search(const K&) const; •  void Insert(const K&, const E&); •  void Delete(const K&); •  private: •  AvlNode<K, E> *root; •  };

Page 22: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

•  template <class K, class E> •  void AVL<K, E>::Insert(const K& k, const E& e){ •  if (!root) { // empty tree •  root=new AvlNode<K, E>(k, e); •  return; •  } •  // phase 1: Locate insertion point for e. •  AvlNode<K, E> *a=root, // most recent node with

BF±1 •  *pa, // parent of a •  *p=root, // p move through the tree •  *pp=0; // parent of p

Page 23: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

•  while (p) { // search for insertion point for x •  if (p→bf) •  {a=p; pa=pp;} •  if (k<p→key) •  {pp=p; p=p→leftChild;} •  else if (k>p→key) •  {pp=p; p=p→rightChild;} •  else •  {p→element=e; return;} // k in the tree •  } // end of while

Page 24: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

•  // phase 2: Insert and rebalance. k is not in the tree •  // will be inserted as the appropriate child of pp. •  AvlNode<K, E> *y=new AvlNode<K, E>(k,

e); •  if (k<pp→key) •  pp→leftChild=y; // as left child •  else •  pp→rightChild=y; // as right child

Page 25: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

•  // Adjust BF’s of nodes on path from a to pp. •  // d=+1 implies k is inserted in the left subtree of •  // a and d=-1 in the right. •  // The BF of a will be changed later. •  int d; •  AvlNode<k, E> *b, // child of a •  *c; // child of b •  if (k>a→key) •  { b=p=a→rightChild; d=-1;} •  else •  { b=p=a→leftChild; d=1;}

Page 26: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

•  while (p!=y) •  if (k>p→key) { // height of right increases by 1 •  p→bf= -1; •  p=p→rightChild; •  } •  else { // height of left increases by 1 •  p→bf= 1; •  p=p→leftChild; •  }

Page 27: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

•  // Is tree unbalanced? •  if (!(a→bf) || !(a→bf +d)) { •  // tree still balanced •  a→bf +=d; return; •  } •  //tree unbalanced, determine rotation type •  if (d==1) { // left imbalance

Page 28: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

•  if (b→bf==1) { // type LL •  a→leftChild=b→rightChild; •  b→rightChild=a; •  a→bf=0; b→bf=0; •  }

BL

BR

AR

h h+2 LL

0 B

0 A

BL

BR

AR

h+2

+2 A

+1 B

Page 29: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

•  else { // type LR •  c=b→rightChild; •  b→rightChild=c→leftChild; •  a→leftChild=c→rightChild; •  c→leftChild=b; •  c→rightChild=a;

+2 A

-1 B

BL

CL

AR

h h+2 LR(b)

0 C

-1 A

BL

CR

AR

h+2 +1 C

CR

0 B

CL

Page 30: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

•  switch (c→bf) { •  case 1: // LR(b) •  a→bf=-1; b→bf=0; •  break;

+2 A

-1 B

BL

CL

AR

z h h+2 LR(b)

0 C

-1 A

BL

CR

AR

h+2 +1 C

CR

0 B

CL

Page 31: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

•  case -1: // LR(c) •  b→bf=1; a→bf=0; •  break;

+2 A

-1 B

BL

CL

AR

h h+2 LR(c)

0 C

0 A

BL

CR

AR

h+2 -1 C

CR

+1 B

CL

Page 32: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

•  case 0: // LR(a) •  b→bf=0; a→bf=0; •  break; •  } •  c→bf=0; b=c; // b is the new root •  } // end of LR •  } // end of left imbalance

+2 A

-1 B LR(a)

0 C

0 A 0

B

0 C

Page 33: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

•  else { // right imbalance •  // symmetric to left imbalance •  } •  // Subtree with root b has been rebalanced. •  if (!pa) •  root=b; // A has no parent and a is the root •  else if (a==pa→leftChild) •  pa→leftChild=b; •  else pa→rightChild=b; •  return; •  } // end of AVL::Insert

Page 34: Efficient Binary Search Trees...balanced binary search trees • AVL tree – Adelson-Velskii and Landis • Red-black tree AVL Tree • an empty tree is height-balanced • If T is

Analysis

•  If h is the height of the tree before insertion, the time to insert a new key is O(h).

•  In case of AVL tree, h can be at most O(log n), so the insertion time is O(log n).

Exercises: P578-3, 5, 9