Binary Search Trees - PowerPoint PPT Presentation

About This Presentation
Title:

Binary Search Trees

Description:

Binary Search Trees Binary Trees ... – PowerPoint PPT presentation

Number of Views:329
Avg rating:3.0/5.0
Slides: 64
Provided by: Uma92
Category:
Tags: binary | search | trees

less

Transcript and Presenter's Notes

Title: Binary Search Trees


1
Binary Search Trees
  • ??????? ???? ?? ? ???????? ??

2
???? ?????? Binary Trees
  • ????? ???????
  • ???? ???? ????? ?????? ???
  • ???? ?? ??????? ??? ?? ?????? ?????? ?????
    ?????? ???
  • ????? ?????? ??? ?? ?? ???? ?? 1 ? ?? ????? 2? ??
    ????? ?????? ????? ?? ?? ????.

56
??? ???? ?? ???? ??? ?
3
?????? ???? ??????
  • ?????? ???? ????? ?? ??? ( ??? ??? ?????) ?? ???
    ?????? ????
  • ??????? Recursive
  • ?????? Iterative
  • ?????? ??????? ?????? ??? ? ?????? ??? (???????)
    ?? ? ???? ??
  • Inorder 1-????? ??? ??? 2-???? ? 3- ????? ???
    ????
  • Preorder 1- ????? 2- ????? ??? ??? 3- ????? ???
    ????
  • Postorder1-????? ??? ??? 2- ????? ??? ????? 3-
    ????

4
?????? ???????? ????
  • (a b) (c d) e f/gh 3.25
  • ?? ????? ????? ?? ?? ??? ??? ??? ???
  • ??????.Operators (, -, /, ).
  • ?????? ??
  • Operands (a, b, c, d, e, f, g, h, 3.25, (a b),
    (c d), etc.).
  • ????? ???? ???? ???? ? Delimiters ((, )).

5
???? ?? ?????
  • ????? ?????????? ?? ?? ????? ???? ????
  • ????? ??? ?? ???? ? ??????? ??????? ???? ???
  • ???????? ??????? ?? ?????? ???? ?????
  • a b
  • c / d
  • e - f
  • ???????? ?????? ?? ?????? ???? ?????
  • g
  • - h
  • ????? ???????? ????
  • ????? ????????
  • ????? ???????
  • ????? ??????

6
????? ????????
  • ?????? ??? ?? ????? ???? ?? ????
  • a b
  • a b c
  • a b / c
  • (a b) (c d) e f/gh 3.25
  • ????? ? ????? ???????
  • ???? ????? ???? ????? ???? ???
  • ?????? ??? ? ????? ?? ??? ? ????? ???? ?????
  • ?? ?? ?? ???? ???

7
????? ???? ????
  • ?? ??????? ?? ??? ?????? ????????? ????? ?? ????
    ?? ?? ?????? ?????? ??? ?? ?????? ???? ?? ????
  • (a b) (c d) / (e f)

8
?????? ?????
  • ???? ?????? ???????? ???? ?? ????? ???? ?????
    ????? ???? ??????? ???? ???
  • ???? ?????? ???? ?????? ?????? ?? ???? ?????
  • ????? ??????? ? ?????? ???? ???????? ??????
  • ?????? ?????? ?? ??? ?????? ?? ??????? ????
    ???????? ?????? ???
  • ????? ?????? Postfix
  • ????? ?????? ?? ????? ?????? ?? ?? ?????? ?? ????
    ??? ???
  • A o B AB o ,
  • o is the operator
  • ????? ??????? ?? ????? ????? ???? ???
  • A o B oAB ,
  • o is the operator

9
??? ????? ????? ??????
  • Infix a b c
  • Postfix

a
b
c

  • Infix a b c
  • Postfix

a
b

c
  • Infix (a b) (c d) / (e f)
  • Postfix

a
b

c
d
-

e
f

/
10
???????? ?????
  • ???? ??????? ????? ?? ?????? ???? ???? ????????
    ????? ??????? ?? ????
  • a gt a _at_
  • a b gt a _at_ b
  • - a gt a ?
  • - a-b gt a ? b -

11
Postfix Evaluation
  • ????? ?? ?? ?? ?? ???? ?? ?????? ? ?? ?? ??????
    ?? ?? ???? ?? ?? ?? ???? ??????? ?? ????
  • ?? ????? ?? ????? ?? ????? ???? ?????? ?? ???? ??
    ?? ????? ? ??? ?????? ??? ?? ????? ?? ???? ? ???
    ????? ?????? ?? ?? ???? ??????? ?? ????
  • ????? ????? ????? ?? ????? ???? ???? ????

12
Postfix Evaluation
  • (a b) (c d) / (e f)
  • a b c d - e f /
  • a b c d - e f /
  • b c d - e f /
  • c d - e f /

b
a
13
Postfix Evaluation
  • (a b) (c d) / (e f)
  • a b c d - e f /
  • a b c d - e f /

d
c
  • c d - e f /

(a b)
  • d - e f /
  • - e f /

14
Postfix Evaluation
  • (a b) (c d) / (e f)
  • a b c d - e f /
  • e f /

(c d)
(a b)
15
Postfix Evaluation
  • (a b) (c d) / (e f)
  • a b c d - e f /
  • a b c d - e f /
  • a b c d - e f /
  • a b c d - e f /

f
e
  • a b c d - e f /

(a b)(c d)
stack
16
Postfix Evaluation
  • (a b) (c d) / (e f)
  • a b c d - e f /
  • a b c d - e f /
  • a b c d - e f /
  • a b c d - e f /

(e f)
  • a b c d - e f /

(a b)(c d)
  • a b c d - e f /

stack
17
????? ?? ????? ?? ???? ??????
  • a b
  • - a

18
????? ?? ????? ?? ???? ??????
  • (a b) (c d) / (e f)

19
?????? ????? ?? ????
  • ????????? ???? ? ?? ?? ?????? ?? ???? ???? ???
  • ?? ???????? ??? ??????? ?????? ?? ???? ?????? ??
    ?????? ???

20
?????? ????
  • ?????? ???? ?????? ?????? ??
  • ?? ??? ?? ??? ????? ?? ?????
  • ????? ???? ?? ??? ??? ?????? ??? ?? ??? ?? ?????
    ?? ????
  • Preorder
  • Inorder
  • Postorder
  • Level order

21
?????? ??? ?????
  • public static void preOrder(BinaryTreeNode t)
  • if (t ! null)
  • visit(t)
  • preOrder(t.leftChild)
  • preOrder(t.rightChild)

22
Preorder Example (visit print)
a
b
c
23
Preorder Example (visit print)
a
b
d
g
h
e
i
c
f
j
24
Preorder Of Expression Tree
/


a
b
-
c
d

e
f
Gives prefix form of expression!
25
Inorder Traversal
  • public static void inOrder(BinaryTreeNode t)
  • if (t ! null)
  • inOrder(t.leftChild)
  • visit(t)
  • inOrder(t.rightChild)

26
Inorder Example (visit print)
b
a
c
27
Inorder Example (visit print)
g
d
h
b
e
i
a
f
j
c
28
?????? ???? ????? ?? ????? ????? ????
29
Inorder Of Expression Tree
?????? ???? ?????? ????? ??????? ?? ???? ??????
?? ???
30
Postorder Traversal
  • public static void postOrder(BinaryTreeNode t)
  • if (t ! null)
  • postOrder(t.leftChild)
  • postOrder(t.rightChild)
  • visit(t)

31
Postorder Example (visit print)
b
c
a
32
Postorder Example (visit print)
g
h
d
i
e
b
j
f
c
a
33
Postorder Of Expression Tree
a
b

c
d
-

e
f

/
Gives postfix form of expression!
34
Traversal Applications
  • Make a clone.
  • Determine height.
  • Determine number of nodes.

35
Level Order
  • Let t be the tree root.
  • while (t ! null)
  • visit t and put its children on a FIFO queue
  • remove a node from the FIFO queue and call it
    t
  • // remove returns null when queue is empty

36
Level-Order Example (visit print)
a
b
c
d
e
f
g
h
i
j
37
???? ???? ??????
  • ??? ???? ????? ???? ??? ?????? ?????
  • ??? ?? ????? ??? ?? ????????? ?? ???? ???? ??
    ????!?
  • ??? ?????? ???? ??? ??? ?? ?? ??? ????? ????? ???
    ???? ???? ?? ?? ???? ????? ???? ????
  • ??? ?? ????? ?? ??? ?????? ?? ???? ?? ???? ?? ??
    ???? ????? ???? ???? ?
  • ???? ???????!

38
Some Examples
  • preorder ab

inorder ab
postorder ab
level order ab
39
Preorder And Postorder
  • preorder ab

postorder ba
  • Preorder and postorder do not uniquely define a
    binary tree.
  • Nor do preorder and level order (same example).
  • Nor do postorder and level order (same example).

40
Inorder And Preorder
  • inorder g d h b e i a f j c
  • preorder a b d g h e i c f j
  • Scan the preorder left to right using the inorder
    to separate left and right subtrees.
  • a is the root of the tree gdhbei are in the left
    subtree fjc are in the right subtree.

41
Inorder And Preorder
  • preorder a b d g h e i c f j
  • b is the next root gdh are in the left subtree
    ei are in the right subtree.

42
Inorder And Preorder
  • preorder a b d g h e i c f j
  • d is the next root g is in the left subtree h
    is in the right subtree.

43
Inorder And Postorder
  • Scan postorder from right to left using inorder
    to separate left and right subtrees.
  • inorder g d h b e i a f j c
  • postorder g h d i e b j f c a
  • Tree root is a gdhbei are in left subtree fjc
    are in right subtree.

44
Inorder And Level Order
  • Scan level order from left to right using inorder
    to separate left and right subtrees.
  • inorder g d h b e i a f j c
  • level order a b c d e f g h i j
  • Tree root is a gdhbei are in left subtree fjc
    are in right subtree.

45
???? ??????? ?? ????
  • ???? ??????? ?? ???? Binary Search Tree (BST)
  • ??????? ???? ?? ???? ???? ??????? ?????? ???
    ?????? ?? ?????? ??? ?? ????? ?? ???
  • Search, Minimum, Maximum, Predecessor, Successor,
    Insert, and Delete.
  • ???? ????? Dictionary. ? Priority Queue ?? ????
    ?? ?? ??????? ???
  • ???? ????? ?????? ?????? ?????? ?? ???(??????)
    ???? ???. O(h).

46
?????BST
  • ?? ??????? ?? ?????? ?????? ????? ????? ???? ??
    ???
  • root(T) T ????? ?? ?? ???? ????
  • ?? ??? ????? ??? ??? ?? ????
  • key
  • left pointer to left child root of left
    subtree.
  • right pointer to right child root of right
    subtree.
  • p pointer to parent. prootT NIL
    (optional).

47
???? BST
  • ????? ???? (key) ???? ??? ???? ?? ????? ????
  • ? y in left subtree of x, then keyy ? keyx.
  • ? y in right subtree of x, then keyy ? keyx.

56
48
??????Inorder
?? ?????? ?? ????? BST ?? ???? ?????? ???? ?? ??
???? ????? ????? ???? ???
  • Inorder-Tree-Walk (x)
  • 1. if x ? NIL
  • 2. then Inorder-Tree-Walk(leftp)
  • 3. print keyx
  • 4. Inorder-Tree-Walk(rightp)

56
12
18
24
26
27
28
56
190
210
213
  • ????? ?????? ???? ??? ?

49
????? ?????? Inorder
  • ????? ????? ? ????? ?????
  • ?? ??????? ?? ??????? ????? ?????? ???? ??? ??
    ? ???? ?? ???? ????? ?? ???
  • ??? ???? ??? ????? ????? ? ????? ????? ????? ???
  • ??? ???? ??? ?? ?? ??? ????
  • ?????? ??? ?? - ?? ??????? ?? ??????? - ??? ??
    ???
  • ???? ??? ?? ???? ?? ???? ?? ????? BST? ????? ????
    ?? ?????? ?????? ??? ?? ?????? ??? ???????? ??
    ?????? ??? ??? ?? ???
  • ???? ??? ???? ??? ????? ??????? ?????? ??? ?? ???
    ? ?????? ?? ?? ????? ???? ????????

50
????? ?? BST
  • ???? ?????? ?????? ??? ???? ?? ?? ???? ?? ?????
    ?? ?????? ?? ??? ???? ????? ??? O(h).
  • ??? ???? ?????? ????( ??? ?????? ??? ??? ??? ????
    ????? ?? ????) h ?(lg n)
  • ?? ?????? ???? h ?(n).
  • ???? ?????? ?? ???? ?????? ?? ?? ????? ?? ?? ???

51
Tree Search
  • Tree-Search(x, k)
  • 1. if x NIL or k keyx
  • 2. then return x
  • 3. if k lt keyx
  • 4. then return Tree-Search(leftx, k)
  • 5. else return Tree-Search(rightx, k)

Running time O(h) Aside tail-recursion
52
?????? ?????? Iterative
  • Iterative-Tree-Search(x, k)
  • 1. while x ? NIL and k ? keyx
  • 2. do if k lt keyx
  • 3. then x ? leftx
  • 4. else x ? rightx
  • 5. return x

Search 24
56
?????? ????? ?? ???? ??????? ?????? ?? ???
??????? ???? ??? ????? ???? ??? ??????? ?????? ?
???? ?? ???
53
????? Min , Max
  • ??????BST ????? ?? ???
  • MIN ?? ??? ?? ???? ??? ???? ?? ????
  • MAX ?? ??? ???? ???? ??? ???? ?? ????.
  • Tree-Minimum(x) Tree-Maximum(x)
  • 1. while leftx ? NIL 1. while
    rightx ? NIL
  • 2. do x ? leftx 2. do x ?
    rightx
  • 3. return x 3. return x

????? ??? ?????? ???? ??? ?
54
Predecessor Successor
  • Successor (x) ??? ??? ?? x ???? ?? ????? ?? ??
    ????? x? ?????? ??? ?? ???? ??????? ?????? ?? x ?
    ?????? ???
  • ???????? ???? ????
  • ???? ?? ????? ?????? inorder? ???????? ??? ?? x?
    ???? ?? ???
  • Predecessor(x) ??? ??? ?? x ???? ?? ????? ?? ??
    ???? x? ?????? ??? ?? ???? ??????? ?????? ??x ?
    ?????? ???
  • ???????? ???? ?????
  • ???? ?? ????? ?????? inorder? ???????? ??? ?? x?
    ???? ?? ???

55
??? ?? Successor
  • Tree-Successor(x)
  • if rightx ? NIL
  • 2. then return Tree-Minimum(rightx)
  • 3. y ? px
  • 4. while y ? NIL and x righty
  • 5. do x ? y
  • 6. y ? py
  • 7. return y

Successor (28)
y
x
Code for predecessor is symmetric. Running time
O(h)
56
?????? ?? ??? ?? ????
  • Tree-Insert(T, z)
  • y ? NIL
  • x ? rootT
  • while x ? NIL
  • do y ? x
  • if keyz lt keyx
  • then x ? leftx
  • else x ? rightx
  • pz ? y
  • if y NIL
  • then roott ? z
  • else if keyz lt keyy
  • then lefty ? z
  • else righty ? z
  • ??? ?????? ???? ???? ????? ??? ?? ????? BST ???
    ???
  • ???? ?????? 50 ?? ???? ???

50
57
?????? ?????? ???
  • Tree-Insert(T, z)
  • y ? NIL
  • x ? rootT
  • while x ? NIL
  • do y ? x
  • if keyz lt keyx
  • then x ? leftx
  • else x ? rightx
  • pz ? y
  • if y NIL
  • then roott ? z
  • else if keyz lt keyy
  • then lefty ? z
  • else righty ? z
  • Initialization O(1)
  • While loop in lines 3-7 searches for place to
    insert z, maintaining parent y.O(h) time.
  • Lines 8-13 insert the value O(1)
  • ? TOTAL O(h) time to insert a node.

58
Tree-Delete (T, x)
  • if x has no children then remove x ?
    case 0
  • if x has one child then point px to child ?
    case 1
  • if x has two children (subtrees) then ?
    case 2
  • swap x with its successor
  • perform case 0 or case 1

Delete 26
Delete 28
56
56
26
200
28
28
18
18
27
27
59
??? ?? Successor
  • Tree-Successor(x)
  • if rightx ? NIL
  • 2. then return Tree-Minimum(rightx)
  • 3. y ? px
  • 4. while y ? NIL and x righty
  • 5. do x ? y
  • 6. y ? py
  • 7. return y

Successor (28)
y
x
Code for predecessor is symmetric. Running time
O(h)
60
??? ?? ???
  • Tree-Delete(T, z)
  • / Determine which node to splice out either z
    or zs successor. /
  • if leftz NIL or rightz NIL
  • then y ? z
  • else y ? Tree-Successorz
  • / Set x to a non-NIL child of x, or to NIL if y
    has no children. /
  • if lefty ? NIL
  • then x ? lefty
  • else x ? righty
  • / y is removed from the tree by manipulating
    pointers of py and x /
  • if x ? NIL
  • then px ? py
  • / Continued on next slide /

61
??? ?? ??? ?? ???
  • Tree-Delete(T, z) (Contd. from previous slide)
  • if py NIL
  • then rootT ? x
  • else if y leftpi
  • then leftpy ? x
  • else rightpy ? x
  • / If zs successor was spliced out, copy its
    data into z /
  • if y ? z
  • then keyz ? keyy
  • copy ys satellite data into z.
  • return y

62
????? ???????? ???
  • ?? ???? ???? ????? ???????? ?? ???? 0 ?? 1 ?????
    ?? ???? ????
  • ??? x ?? ????? ????? ????? ??? ???? ??? ????????
    ??? ?????? ??? ???? ????? ???. ??? ??? ????? ???
    ?? ?????. ??? ????? ???? ????? ????? ???? 0 ? ???
    ?????? ???? ???? 1 ????? ?? ????
  • ?? ??? ???????? ?? ???? ?? ??? ??? ??? ???????
    ???. ???? ??? ????? ??? ??? ??? ?? ?????? ??? ?
    ??? ????? ?????? ?????? ??? ?? ???? ????? ????
    ???????? ???? ????.

63
?????
  • ??? ?? ??? ???? ???? ???? ?????? BST ????? ???
    ???. ????? ????? ?? ?? ?? ??????? ????? ? ??????
    ???? ?????? ????
  • ??????? ??? ?? ?????? ??? ???????? ?? ?????? ??
    ???? ?????? ???? ???? ???? ?
  • Sort (A)
  • for i ? 1 to n
  • do tree-insert(Ai)
  • inorder-tree-walk(root)
Write a Comment
User Comments (0)
About PowerShow.com