Algs4 - Balanced Search Trees, 平衡二叉树

Balanced Search Trees

2-3 Search Trees

  • Allow 1 or 2 keys per node
    • 2-node: one key, two children
    • 3-node: two keys, three children
  • Perfect balance: Every path from root to null link has same length
  • Symmetric order

Red-Black BSTs

Operations

Implementation: RedBlackBST.java

  • Represent 2-3 tree as a BST
  • Use “internal” left-leaning links as “glue” for 3-nodes.
  • An equivalent definition
    • No node has two red links connected to it
    • Every path from root to null link has the same number of black links (perfect black balance)
    • Red links lean left
  • Search implementation is the same as elementary BST (ignore colors), other ops (eg., ceiling, rank, selection) are also identical.
  • Red-black BST representation: Each node is pointed to by precisely one link (from its parent) -> can encode color of links in nodes.
  • Left rotation. Orient a right-leaning (temporarily) red link to lean left
  • Right rotation. Orient a left-leaning red link to (temporarily) lean right.
  • Color flip. Recolor to split a (temporary) 4 node.

Insertion in a LLRB tree

  • Warmup 1. Insert into a tree with exactly 1 node.
  • Warmup 2. Insert into a 2-node at the bottom.
  • Same code handles all cases:
    • Right child red, left child black: rotate left.

    • Left child, left-left grandchild red: rotate right.

    • Both children red: flip colors.

      private Node put(Node h, Key key, Value val) {
      if (h == null) return new Node(key, val, RED);
      int cmp = key.compareTo(h.key);
      if      (cmp < 0) h.left = put(h.left, key, val);
      else if (cmp > 0) h.right = put(h.right, key, val);
      else              h.val = val;
      
      // very concise.
      if (isRed(h.right) && !isRed(h.left))     h = rotateLeft(h);
      if (isRed(h.left)  && isRed(h.left.left)) h = rotateRight(h);
      if (isRed(h.left)  && isRed(h.right))     flipColors(h);
      
      return h;
      }
      

B-Trees & B+ Trees

COMP 5511 slides

Geometric Applications of BSTs

Intersection among geometric objects
Applications: CAD, games, movies, virtual reality, databases…
Efficient solutions: Binary search trees (and extensions)

1d Range Search

  • Extension of ordered symbol table
    • Insert key-value pair, search for key k, delete key k.
    • Range search: find all keys between k1 and k2
    • Range count: number of keys between k1 and k2
  • Applications. Database queries
  • Geometric interpretation
    • Keys are point on a line
    • Find/count points in a given 1d interval.
  • Implementations
    • Unordered array. Fast insert, slow range search
    • Ordered array. Slow insert, binary search for k1 and k2 to do range search.
    • BST.
      • 1d range count -> rank() function.
      • 1d range search: Recursively find all keys in left, right subtree that could fall in range.

Line Segment Intersection

Given N horizontal and vertical line segments, find all intersections.
Sweep-line algorithm
Sweep vertical line from left to right

  • x-coordinate define events.
  • horizontal-segment(lef endpoint): insert y-coordinate into BST.
  • horizontal-segment(right endpoint): remove y-coordinate from BST.
  • vertical-segment: range search for interval of y-endpoints.

Kd-Trees (Exercise)

  • Extension of ordered symbol-table to 2d keys
    • Insert a 2d key
    • Delete a 2d key
    • Search for a 2d key
    • Range search: find all keys that lie in a 2d range
    • Range count: number of keys that lie in a 2d range
  • Geometric interpretation
    • Keys are point in the plane
    • Find/count points in a given h-vrectangle.
  • Applications. Networking, circuit design, databases.
  • Space-partitioning trees: applications

在这里插入图片描述

Implement the insert, contains, find the nearest point, and range search.
Kd-tree specification
Kd-Tree Implementation

Interval Search Trees

Interval search trees
Create BST, where each node stores an interval (lo, hi)

  • Use left endpoint as BST key
  • Store max endpoint in subtree rooted at node

To insert an interval (lo, hi)

  • Insert into BST, using lo as the key.
  • Update max in each node on search path.

To search for any one interval that intersect query interval (lo, hi):

  • If interval in node intersects query interval, return it.
  • Else if left subtree is null, go right.
  • Else if max endpoint in left subtree is less than lo, go right.
  • Else go left.

Implementation. Use a red-black BST to guarantee performance.

Rectangle Intersection

Goal. Find all intersection among a set of N orthogonal rectangles.
No-degeneracy assumption. All x and y coordinates are distinct.

Application: Microprocessor design became a geometric problem in Early 1970s.

Orthogonal rectangle intersection search: sweep-line algorithm
Sweep vertical line from left to right

  • x-coordinates of left and right endpoints define events.
  • Maintain set of rectangles that intersect the sweep line in an interval search tree (using y-intervals of rectangle).
  • Left endpoint: interval search for y-interval of rectangle; insert y-interval.
  • Right endpoint: remove y-interval.

Proposition. Sweep line algorithm takes time proportional to NlogN + RlogN to find R intersections among a set of N rectangles.
Pf.

  • Put x-coordinates on a PQ (or sort). <-- NlogN
  • Insert y-interval into ST. <-- NlogN
  • Delete y-interval from ST. <-- NlogN
  • Interval searches for y-intervals. <-- NlogN + RlogN

Bottom line Sweep line reduces 2d orthogonal rectangle intersection search to 1d interval search.

Summary Geometric application of BSTs.

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值