Docsity

Primary Memory - Data Structures and Advanced Programming - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Primary Memory - Data Structures and Advanced Programming - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Lecture #22: Tree Representation and Searching

0

1... 2... 3...

(a) Embedded child pointers (+ optional parent pointers)

0

1... 2... 3...

(b) Array of child pointers (+ optional parent pointers)

0

1 2 3

0

1 2 3

......... (c) child/sibling pointers

0 1 2 3 · · ·

(d) pre-order array (complete trees)

Divide and Conquer

  • Much (most?) computation is devoted to finding things in response to various forms of query.
  • Linear search for response can be expensive, especially when data set is too large for primary memory.
  • Preferable to have criteria for dividing data to be searched into pieces recursively
  • Remember figure for lg N algorithms: at 1μsec per comparison, could

process 10300000 items in 1 sec.

  • Tree is a natural framework for the representation:

decision data

no yes

decision data

low mid high

Finding

  • Searching for 50 and 49:

42

19

16 25

30

60

50 91

/** Node in T containing L,

  • or null if none */ static BST find(BST T, Object L) { if (T == null) return T; if (L.keyequals (T.label())) return T; else if (L ≺ T.label()) return find(T.left(), L); else return find(T.right (), L); }
  • Dashed boxes show which node labels we look at.
  • Number looked at proportional to height of tree.

Inserting

  • Inserting 27

42

19

16

25

30

27

60

50 91

/** Insert L in T, replacing existing

  • value if present, and returning
  • new tree. */ BST insert(BST T, Object L) { if (T == null) return new BST(L); if (L.keyequals (T.label())) T.setLabel (L); else if (L ≺ T.label()) T.setLeft(insert (T.left (), L)); else T.setRight(insert (T.right (), L)); return T; }
  • Starred edges are set (to themselves, unless initially null).
  • Again, time proportional to height.

A Leap Ahead: Quadtrees

  • Want to index information about locations so that items can be re- trieved by position.
  • Quadtrees do so using standard data-structuring trick: Divide and Conquer.
  • Idea: divide (2D) space into four quadrants, and store items in the appropriate quadrant. Repeat this recursively with each quadrant that contains more than one item.
  • Original definition: a quadtree is either
    • Empty, or
    • An item at some position (x, y), called the root, plus
    • four quadtrees, each containing only items that are northwest,

northeast, southwest, and southeast of (x, y).

  • Big idea is that if you are looking for point (x′, y′) and the root is not the point you are looking for, you can narrow down which of the four

subtrees of the root to look in by comparing coordinates (x, y) with

(x′, y′).

Classical Quadtree: Example

• A

• B

• C

• D

• E

A

B E

C

D

• D

• A

• B

• C

• E^

D

C B A

E

Example of PR Quadtree

A •

B •

  • C
    • D

0

A B

D C

Navigating PR Quadtrees

  • To find an item at (x, y) in quadtree T ,

1. If (x, y) is outside the bounding rectangle of T , or T is empty,

then (x, y) is not in T.

2. Otherwise, if T contains a single item, compare it to (x, y).

3. Otherwise, T consists of four quadtrees. Recursively look for

(x, y) in each (however, step #1 above will cause all but one of

these bounding boxes to reject the point immediately).

  • Similar procedure works when looking for all items within some rect-

angle, R:

1. If R does not intersect the bounding rectangle of T , or T is

empty, then there are no items in R.

2. Otherwise, if T contains a single item, return it if it is in R, and

otherwise nothing.

3. Otherwise, T consists of four quadtrees. Recursively look for

points in R in each one of them.