Docsity

Binary Search Tree: Properties, Operations, and Insertion Algorithm, Slides of Data Structures and Algorithms

Toggle sidebar
bg1
•
1
Li V N
Li D t St t
Li
near
V
s.
N
on-
Li
near
D
a
t
a
St
ruc
t
ure
Linear D.S Non-Linear D.S
One Path/Street
E.
g
., Stacks,
divergence in the
Road
g
Queues, Linked Lists,
etc
E.g., Trees, graphs,
etc
2
Binary search tree property
10
14
7
16
59
62 8
For any node x, let y be a node in the
left tree of x, then key[y] ≤ key[x].
Likewise, if y is a node in the right
subtree of x then ke
y[
x
]
≤ke
y[y]
.
5/30/2008
3
y[ ] y[y]
Binary Search Tree
Binary
Search
Tree
•
A
binary search treebinary search tree
A
binary
search
treebinary
search
tree
property that the item in its root has
a larger key than each item in the left subtree and
–
a
larger
key
than
each
item
in
the
left
subtree
,
and
– a smaller key than each item in its right subtree.
4
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Binary Search Tree: Properties, Operations, and Insertion Algorithm and more Slides Data Structures and Algorithms in PDF only on Docsity!

1

Li^

V^ N

Li

D t^

St^

t

Linear Vs. Non-Linear Data Structure

Linear D.S

Non-Linear D.S

„^ One Path/Street „^ E.g., Stacks,

„^ divergence in theRoad

g Queues, Linked Lists,etc

„^ E.g., Trees, graphs,etc

2

Binary search tree property

For any node x, let y be a node in the^ left tree of x, then key[y]

≤^ key[x]. Likewise, if y is a node in the rightsubtree of x then key[x]

≤key[y].

5/30/2008^3 y[ ]^ y[y]

Binary Search TreeBinary Search Tree•^ A^ binary search treebinary search tree

is either empty or has the

A^ binary

search tree

binary search tree

is^ either empty or has the

property that the item in its root hasa larger key than each item in the left subtree and– a larger key than each item in the left subtree, and– a smaller key than each item in its right subtree.

4

docsity.com

Example

t root 10

leaf (^862)

leaf

leafleaf leaf

5/30/2008^5

Operations

Search 10

MinimumMaximum 14 7

MaximumPredecessorSuccessor 16

SuccessorInsertDelete

(^862)

Delete

5/30/2008^6

Binary search tree property

For any node x, let y be a node in the^ left tree of x, then key[y]

≤^ key[x]. Likewise, if y is a node in the rightsubtree of x then key[x]

≤key[y].

5/30/2008^7 y[ ]^ y[y]

Binary search tree property

x^7

y

z (^862) For any node x, let y be a node in the y

left tree of x, then key[y]

≤^ key[x]. Likewise, if z is a node in the rightsubtree of x then key[x]

≤key[z].

5/30/2008^8

y[ ]^ y[ ] docsity.com

Tree traversals

Preorder(node x) 10

(^ ) If x ≠ NILprint(x) 14 7

print(x)Preorder(x

→left) Preorder(x

→right) 16

5

Preorder(x

→right)

5/30/2008^13

Tree traversals

Preorder(node x) 10

(^ )

If x^ ≠^ NILprint(x) 14 7

print(x)Preorder(x

→left) Preorder(x

→right) 16

5

Preorder(x

→right)

5/30/2008^14

Tree traversals

Postorder(node x) 10

(^ )

If x^ ≠^ NILPostorder(x

→left)

14 7

Postorder(x

left) Postorder(x

→right) print(x) 16

print(x)

(^862)

5/30/2008^15

Tree traversals

Postorder(node x) 10

(^ )

If x^ ≠^ NILPostorder(x

→left)

14 7

Postorder(x

left) Postorder(x

→right) print(x) 16

print(x)

(^862) 2,6,5,8,9,7,16,14,

5/30/2008^16 docsity.com

Tree traversals

What is the running 10

g time? 14 7

5/30/2008^17

Minimum and Maximum

5/30/2008^18

Minimum and Maximum

Minimum(node x)while x^ →

left^ ≠^ NIL 14 7

do x^ ←^ x

→left return x 16

return x

(^862)

5/30/2008^19

Minimum and Maximum

Minimum(node x)• while x

→^ left^ ≠^

NIL

-^ do x

←^ x→left

  • return x 16

Maximum(node x)

(^862)

  • while x^

→^ right^ ≠

NIL

-^ do x

←^ x→right

  • return x• return x

5/30/2008^20 docsity.com

Searching a Binary Search TreeSearching

a Binary Search Tree

•Assumethe target

g

key is 42.

25

Successor

The successor of x is the nodewith the smallest key greater 10

with the smallest key greaterthan key[x]. 14 7 x

5/30/2008^26

Successor(node x)if^ right

≠^ NIL

Successor

if x→right

≠^ NIL then return Minimum(x

→right) y^ ←^ x→p 10

y^ x^ p while y≠NIL and x==y

→right x^ ←^ y 14 7

y^ ←^ y→p return y 16

5

9 x (^862)

5/30/2008^27

Successor(node x)if^ right

≠^ NIL

Successor

if x→right

≠^ NIL then return Minimum(x

→right) y^ ←^ x→p 10

y^ x^ p while y≠NIL and x==y

→right x^ ←^ y 14 7

y^ ←^ y→p return y 16

5

9 x (^862)

Search, Minimum, Maximum,S^

ll^ i^ O(h) ti Successor all run in O(h) time,where h is the height of thecorresponding binary search tree.

5/30/2008^28 docsity.com

Building a Binary Search Tree^ •^ If the tree is empty^ – Insert the new key in the root node•^ else if the root’s key matches the new keyelse if the root s key matches the new key– Skip insertion^ •^ else if the new key is smaller than root’s key– Insert the new key in the left subtreeInsert the new key in the left subtree•^ else

29

  • Insert the new key in the right subtree

Insertion

Insert a new node z with key[z]=vinto a tree T. 10

into a tree T. 14 7

9.5z

5/30/2008^30

Insertion

Insert(T,z)y^ ←^ NIL x 10

x^ ←^ root(T)While x^ ≠

NIL

x^14 7

y^ ←^ x if key[z] < key[x] 16

if key[z]^

key[x]then x ←^ x→leftelse x ←^ x→right

else x^ ←^

x→right z→p^ ←^ y 9.5z

5/30/2008^31

Insertion

Insert(T,z)y^ ←^ NIL y x 10

x^ ←^ root(T)While x^ ≠

NIL

x^14 7

y^ ←^ x if key[z] < key[x] 16

if key[z]^

key[x]then x ←^ x→leftelse x ←^ x→right

else x^ ←

x→right z→p^ ←^ y 9.5z

5/30/2008^32 docsity.com

Insertion

Insert(T,z)y^ ←^ NIL

10

x^ ←^ root(T)While x^ ≠

NIL

y^ ←^ x if key[z] < key[x] y

if key[z]^

key[x]then x ←^ x→leftelse x ←^ x→right

y (^862)

else x^ ←^

x→right z→p^ ←^ y 9.5z

5/30/2008^37

Insertion

Insert(T,z)y^ ←^ NIL

10

x^ ←^ root(T)While x^ ≠

NIL

y^ ←^ x if key[z] < key[x] y

if key[z]^

key[x]then x ←^ x→leftelse x ←^ x→right

y (^862)

else x^ ←

x→right z→p^ ←^ y If^ NIL th

t[T] 9.5z

If y = NIL then root[T]

←z else if key[z] < key [y]

5/30/2008^38 then y→left

←^ z else y→right

←^ z

Building a Binary Search Tree •Assume 40, 20, 10, 50, 65, 45, 30 areinserted in order.

39

Deletion

z

z^

z

z

(^862) z

5/30/2008^40 docsity.com

Deletion

(^862) z

5/30/2008^41

Deletion

XX^862 z

5/30/2008^42

Deletion

z 14 7

z^16

5

5/30/2008^43

Deletion

z X^14 7

z X^16

5

5/30/2008^44 docsity.com

  1. if z→left=NIL or z

→right=NIL

2.^ then y

←^ z

Deletion

3.^ else y

←^ Successor(z)

  1. if y→left

≠NIL

10

5.^ then x

←^ y→left

6.^ else x

←^ y→right 14 7

  1. if x^ ≠^ NIL8.^ then x

→p^ ←^ y→

p

z z

y 5

  1. if y→p = NIL10.^ then root[T]

←^ x 11 l^ if

(^ )

l ft 16 (^62)

11.^ else if y = (y

→p)→left

12.^ then (y

→p)→left^

←^ x 13 else (y

p)^ right

x

8 y

13.^ else (y

→p)→right

←^ x 14.if y≠z 15 then key[z]

←^ key[y]

z y

5/30/2008^49

15.^ then key[z]

←^ key[y]

16.^ copy y’s data into z17 return y

  1. if z→left=NIL or z

→right=NIL

2.^ then y

←^ z

Deletion

3.^ else y

←^ Successor(z)

  1. if y→left

≠NIL

10

5.^ then x

←^ y→left

6.^ else x

←^ y→right 14 7

  1. if x^ ≠^ NIL8.^ then x

→p^ ←^ y→

p

z z

y 5

  1. if y→p = NIL10.^ then root[T]

←^ x 11 l^ if

(^ )

l ft (^16) x (^62)

11.^ else if y = (y

→p)→left

12.^ then (y

→p)→left^

←^ x 13 else (y

p)^ right

x

8 y

13.^ else (y

→p)→right

←^ x 14.if y≠z 15 then key[z]

←^ key[y]

z

x y NIL^

5/30/2008^50

15.^ then key[z]

←^ key[y]

16.^ copy y’s data into z17 return y

x=NIL

  1. if z→left=NIL or z

→right=NIL

2.^ then y

←^ z

Deletion

3.^ else y

←^ Successor(z)

  1. if y→left

≠NIL

10

5.^ then x

←^ y→left

6.^ else x

←^ y→right 14 7

  1. if x^ ≠^ NIL8.^ then x

→p^ ←^ y→

p

z z

y 5

  1. if y→p = NIL10.^ then root[T]

←^ x 11 l^ if

(^ )

l ft (^16) x (^62)

11.^ else if y = (y

→p)→left

12.^ then (y

→p)→left^

←^ x 13 else (y

p)^ right

x

8 y

13.^ else (y

→p)→right

←^ x 14.if y≠z 15 then key[z]

←^ key[y]

z

x y x=NIL

5/30/2008^51

15.^ then key[z]

←^ key[y]

16.^ copy y’s data into z17 return y

x NIL

  1. if z→left=NIL or z

→right=NIL

2.^ then y

←^ z

Deletion

3.^ else y

←^ Successor(z)

  1. if y→left

≠NIL

10

5.^ then x

←^ y→left

6.^ else x

←^ y→right X^14 7

  1. if x^ ≠^ NIL8.^ then x

→p^ ←^ y→

p

z z

y^ X 5

  1. if y→p = NIL10.^ then root[T]

←^ x 11 l^ if

(^ )

l ft (^16) x X 6 2

11.^ else if y = (y

→p)→left

12.^ then (y

→p)→left^

←^ x 13 else (y

p)^ right

x

X 8 y

13.^ else (y

→p)→right

←^ x 14.if y≠z 15 then key[z]

←^ key[y]

8.5^ x

5/30/2008^52

15.^ then key[z]

←^ key[y]

16.^ copy y’s data into z17 return y

docsity.com

  1. if z→left=NIL or z

→right=NIL

2.^ then y

←^ z

Deletion

3.^ else y

←^ Successor(z)

  1. if y→left

≠NIL

10

5.^ then x

←^ y→left

6.^ else x

←^ y→right 14 7

  1. if x^ ≠^ NIL8.^ then x

→p^ ←^ y→

p

z z

y 5

  1. if y→p = NIL10.^ then root[T]

←^ x 11 l^ if

(^ )

l ft (^16) x (^62)

11.^ else if y = (y

→p)→left

12.^ then (y

→p)→left^

←^ x 13 else (y

p)^ right

x

8 y

13.^ else (y

→p)→right

←^ x 14.if y≠z 15 then key[z]

←^ key[y]

8.5^ x

5/30/2008^53

15.^ then key[z]

←^ key[y]

16.^ copy y’s data into z17 return y

  1. if z→left=NIL or z

→right=NIL

2.^ then y

←^ z

Deletion

3.^ else y

←^ Successor(z)

  1. if y→left

≠NIL

10

5.^ then x

←^ y→left

6.^ else x

←^ y→right 14 8

  1. if x^ ≠^ NIL8.^ then x

→p^ ←^ y→

p

z z

y 5

  1. if y→p = NIL10.^ then root[T]

←^ x 11 l^ if

(^ )

l ft (^16) x (^62)

11.^ else if y = (y

→p)→left

12.^ then (y

→p)→left^

←^ x 13 else (y

p)^ right

x

8 y

13.^ else (y

→p)→right

←^ x 14.if y≠z 15 then key[z]

←^ key[y]

8.5^ x

5/30/2008^54

15.^ then key[z]

←^ key[y]

16.^ copy y’s data into z17 return y 1. if z→left=NIL or z

→right=NIL

2.^ then y

←^ z

Deletion

3.^ else y

←^ Successor(z)

  1. if y→left

≠NIL

10

5.^ then x

←^ y→left

6.^ else x

←^ y→right

8

  1. if x^ ≠^ NIL8.^ then x

→p^ ←^ y→

p

z z

y 5

  1. if y→p = NIL10.^ then root[T]

←^ x 11 l^ if

(^ )

l ft (^16) x (^62)

11.^ else if y = (y

→p)→left

12.^ then (y

→p)→left^

←^ x 13 else (y

p)^ right

x

y

13.^ else (y

→p)→right

←^ x 14.if y≠z 15 then key[z]

←^ key[y]

8.5^ x

5/30/2008^55

15.^ then key[z]

←^ key[y]

16.^ copy y’s data into z17 return y

Reference: • Binhai Zhu

Computer Science

Binhai Zhu,

Computer Science

Department, Montana State UniversityP^

Addi^

W^ l

• Pearson Addison-Wesley.• Data Structure through C in depth• Data Structure through C in depth.• Algorithms in C, Robert Sedgewickg^

g

• Algorithm Design, Michael T.Goodrich

5/30/2008^56 docsity.com