Analysis of Data Structures Binary Search Trees - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Analysis of Data Structures Binary Search Trees

Description:

Analyzing properties & running times of operations. Arrays (sorted) Linked lists (unsorted) ... Unsorted list. Balanced. search tree. Search. Insert/delete. T(n) ... – PowerPoint PPT presentation

Number of Views:269
Avg rating:3.0/5.0
Slides: 17
Provided by: esthe93
Category:

less

Transcript and Presenter's Notes

Title: Analysis of Data Structures Binary Search Trees


1
Analysis of Data StructuresBinary Search Trees
64
33
80
28
88
42
77
37
97
41
2
Goal
  • Analyzing data structures
  • Example binary search trees
  • Overview
  • Definition
  • Properties
  • Operations
  • Analyzing properties running times of
    operations

3
Storing and modifying data
  • Arrays (sorted)
  • Linked lists (unsorted)

fast searching, slow insertion
slow searching, fast insertion
30
21
6
14
28
48
6
48
14
21
28
30
4
Data structures for maintaining sets
Search
Insert/delete
Unsorted array
T(n)
T(1)
Sorted array
T(log n)
T(n)
Unsorted list
T(n)
T(1)
T(n)
T(n)
Sorted list
Balanced search tree
T(log n)
T(log n)
5
Trees
  • Each of the n nodes contains
  • data (number, object, etc..)
  • pointers to its children (also trees!)
  • Primitive operations
  • Accessing data O(1) time
  • Traversing link O(1) time

6
Binary trees
  • Every node has 2 children (can be dummies)

16
80
41
18
23
99
22
7
Binary search trees
  • Binary tree with comparable values
  • For a node with value x
  • Left subtree x
  • Right subtree x

6
4
8
1
5
6
9
8
Tree property height
  • Height h of a tree length of the longest path
  • 2 h n (why?)

64
33
80
75
28
42
88
77
5
97
37
41
9
Height of a binary tree
max
  • min

1 1 1 1 1
1 2 4 n/2
n
2log n
10
Operation searching for an element
  • Searching for 7 start at root
  • At every node
  • youve found it!
  • otherwise, choose left or right child
  • Until you find 7, or youre at a leaf node
  • What is the running time?

6
4
8
1
5
6
9
O(h)
11
Insertion
  • Search for 7
  • Replace leaf node
  • If already presentjust go on
  • What is the running time?

6
4
8
1
5
6
9
O(h)
12
Inorder tree traversal
  • Visit nodes sequentially

O(n) time
x
LC
RC
1, 4, 5, 6, 6, 8, 9 .
13
Removal
  • Search for 7
  • If node has at least one leaf node as a child
  • Delete node, and attach other child to parent

6
4
8
1
5
7
9
14
Removal - 2
  • Remove 8
  • Left child is a leaf,so attach right child to
    parent
  • But what if we want to remove 4?

6
4
1
5
15
Removal - 3
  • Find inorder successor
  • It always exists! (And its left childis a leaf
    node)
  • Replace yourself with this number
  • Remove successor in the previously described way
  • How (fast) can we find the inorder successor in
    this case?

6
4
5
8
1
6
9
5
Again O(h)
16
Overview
Write a Comment
User Comments (0)
About PowerShow.com