A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005...

17
A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005 Computer Systems Research

Transcript of A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005...

Page 1: A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005 Computer Systems Research.

A Study of Balanced Search Trees:Brainstorming a New Balanced Search Tree

Anthony Kim, 2005

Computer Systems Research

Page 2: A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005 Computer Systems Research.

Abstract

This project investigates three different balanced search trees for their advantages and disadvantages, thus ultimately their efficiency. Run time and memory space management are two main aspects under the study. Statistical analysis is provided to distinguish subtle differences if there is any. A new balanced search tree is suggested and compared with the four balanced search trees under study,. Balanced search trees are implemented in C++ extensively using pointers and structs.

Page 3: A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005 Computer Systems Research.

Introduction

● A simple binary search tree has some disadvantages, specifically from its dependence on the incoming data, that significantly affects its tree structure hence its performance. (Ex. linear tree)● An optimal search tree is one that tries to minimize its height given some data. (Ex. Red-black tree has 2lg(n+1) height max)● Some of balanced search trees are red-black tree, AVL tree, weight-balanced tree, and B tree.

Page 4: A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005 Computer Systems Research.

Background

● Simple vocabs: nodes, edges, children, parent, root● Printing tree using recursion: pre-, in-, and post-order traversal● Basic binary search tree functions: insertion, lookup, deletion (only first two apply in this project)● Rotating functions: the key player in balancing (Left rotation and right rotation.)

Page 5: A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005 Computer Systems Research.

Some Balanced Search (Red-black tree)

● Four properties● The root of the tree is colored black.● All paths from the root to the leaves agree on the

number of black nodes.● No path from the root to a leaf may contatin two

consecutive nodes colored red.● Every path from a node to a leaf (of the

descendants) has the same number of black nodes● Has height at most 2lg(n+1)

Page 6: A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005 Computer Systems Research.

Some Balanced Search Tree(Weight & height balanced search tree.)

● Weight balanced tree and height balanced tree are very similar.● Weight balanced tree (Height balanced tree) has one property

● At each node, the difference between weight(height) of left subtree and weight(height) of right subtree is less than the threshold value.

●Supposedly yield height lg(n) at most

Page 7: A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005 Computer Systems Research.

A New Balanced Search TreeMedian-weight-mix tree

● Assumption on statistical data● Give lower bound and upper bound of total data

input, random behavior is assumed, meaning data points will be evenly distributed throughout the interval

● Multiple “crests” is assumed to be present in the interval.

● Each node will have a key (data number), an interval (with lower and upper bounds of its assigned interval) and weights of left subtree and right subtree.

Page 8: A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005 Computer Systems Research.

A New Balanced Search TreeMedian-weight-mix tree

● Algorithm● The weights of each subtree are calculated based

on constants R and S● R = the importance of focusing frequency heavy

data points● S = the importance of focusing frequency weak

data points● Left/right rotations to balanced R to S ratio

Page 9: A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005 Computer Systems Research.

Testing Methodology● 14 Randomly generated test cases (test case size

ranges 20 – 10,000)● 4 Real test scores of math competition etc.● Things I am looking for

– Total Run Time– Average Time Retrieval– Height of Tree– Average Retrieval Depth

Page 10: A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005 Computer Systems Research.

Test Runs (Height-balanced Tree)

Page 11: A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005 Computer Systems Research.

Results

Total Run Time

Test Case Redblack Height Weight MWM

1 0 0 0 0

2 0 0 0 0

3 N/A 0 0 0

4 N/A 0.01 0 0

5 N/A 0.01 0.04 0

6 N/A 0.01 0 0.01

7 N/A 0.01 0 0.02

8 N/A 0.01 0.02 0.02

9 N/A 0.02 0.02 0.02

10 N/A 0.03 0.02 0.05

11 N/A 0.03 0.04 0.05

12 N/A 0.03 0.04 0.05

13 N/A 0.04 0.04 0.05

14 N/A 0.03 0.04 0.05

101 0 0 0 0

102 0 0 0 0

103 N/A 0.08 0.41 N/A

104 0.02 0.02 0.05 0.02

Average retrieval time

Test Case Redblack Height Weight MWM

1 0 0 0 0

2 0 0 0 0

3 N/A 0 0 0

4 N/A 0 0 0

5 N/A 0 0.000001 0

6 N/A 0 0 0

7 N/A 0.000002 0 0

8 N/A 0.000002 0 0.000004

9 N/A 0 0 0

10 N/A 0.000001 0.000002 0.000001

11 N/A 0.000002 0.000001 0.000001

12 N/A 0.000002 0.000002 0.000001

13 N/A 0.000002 0.000002 0.000001

14 N/A 0.000001 0.000002 0

101 0 0 0 0

102 0 0 0 0

103 N/A 0.0000007 0.00000086 N/A

104 0.000181 0.00000089 0 0.00000093

Page 12: A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005 Computer Systems Research.

ResultsDepth

Test Case Redblack Height Weight MWM

1 7 4 4 4

2 12 6 9 7

3 N/A 8 10 10

4 N/A 11 16 13

5 N/A 12 24 14

6 N/A 13 29 17

7 N/A 14 31 20

8 N/A 14 31 19

9 N/A 15 38 18

10 N/A 16 38 23

11 N/A 16 37 22

12 N/A 16 37 22

13 N/A 16 36 23

14 N/A 16 33 24

101 101 7 18 9

102 38 6 8 7

103 N/A 16 42 N/A

104 5596 13 24 12

Average Retrieval Depth

Test Case Redblack Height Weight MWM

1 3.5 2.35 2.35 2.35

2 5.68 3.9 5.08 4.42

3 N/A 4.85 5.44 5.31

4 N/A 7.01 8.684 7.422

5 N/A 8.217 11.823 8.821

6 N/A 9.2445 13.721 9.9635

7 N/A 10.3778 14.674 12.0244

8 N/A 10.4804 14.52 11.6468

9 N/A 10.4662 15.1646 11.6474

10 N/A 11.5749 18.5899 13.2171

11 N/A 11.5864 15.6778 13.0158

12 N/A 11.6456 16.4298 13.1199

13 N/A 11.6748 16.5699 13.3742

14 N/A 11.5393 17.7741 13.1296

101 49.0291 4.87379 8.50971 5.18932

102 18.5625 4.4 4.55 4.7375

103 N/A 0.000765942 0.00065652 N/A

104 2368.98 1.68751 2.70135 2.62517

Page 13: A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005 Computer Systems Research.

Result

● Total run time and average retrieval time data did not make any sense.

● Hard to time processes on fast computers● Red-black tree segmentation faulted for large test

cases >500, so it provided no experimental data

Page 14: A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005 Computer Systems Research.

Result (Height)Height

y = 5.3315Ln(x) - 12.735

y = 2.9492Ln(x) - 5.1306

y = 1.8939Ln(x) - 1.517

0

5

10

15

20

25

30

35

40

0 2000 4000 6000 8000 10000 12000

Data size

Hei

gh

t

Height Weight MWM

Page 15: A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005 Computer Systems Research.

Result (Average retrieval depth)Average Retrieval Depth

y = 2.3718Ln(x) - 4.9972

y = 1.7446Ln(x) - 3.0532

y = 1.4998Ln(x) - 2.2646

0

5

10

15

20

0 2000 4000 6000 8000 10000 12000

Data size

Dep

th

Redblack Height Weight MWM

Page 16: A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005 Computer Systems Research.

Analysis

● All balanced search trees show logarithmic characteristics for height and average retrieval depth as expected. (except red-black tree)

● Height-balanced tree seems to perform the best among three working balanced search trees.

● Median-weight-mix tree’s logarithmic line lies between height-balanced tree’s line and weight-balanced tree’s line.

Page 17: A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005 Computer Systems Research.

Conclusion

● The project experimentally showed that balanced binary search trees show logarithmic characteristics.

● Median-weight-mix tree’s performance is an intermediate between height-balanced tree’s and weight-balanced tree’s.

● More studies should be done on other balanced search trees or variants of search trees studied in this project