Title: Binary Search Trees
1Binary 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??? ????? ????? ??????
a
b
c
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 -
11Postfix Evaluation
- ????? ?? ?? ?? ?? ???? ?? ?????? ? ?? ?? ??????
?? ?? ???? ?? ?? ?? ???? ??????? ?? ???? - ?? ????? ?? ????? ?? ????? ???? ?????? ?? ???? ??
?? ????? ? ??? ?????? ??? ?? ????? ?? ???? ? ???
????? ?????? ?? ?? ???? ??????? ?? ???? - ????? ????? ????? ?? ????? ???? ???? ????
12Postfix Evaluation
- (a b) (c d) / (e f)
- a b c d - e f /
- a b c d - e f /
b
a
13Postfix Evaluation
- (a b) (c d) / (e f)
- a b c d - e f /
- a b c d - e f /
d
c
(a b)
14Postfix Evaluation
- (a b) (c d) / (e f)
- a b c d - e f /
(c d)
(a b)
15Postfix Evaluation
- (a b) (c d) / (e f)
- a b c d - e f /
f
e
(a b)(c d)
stack
16Postfix Evaluation
- (a b) (c d) / (e f)
- a b c d - e f /
(e f)
(a b)(c d)
stack
17????? ?? ????? ?? ???? ??????
18????? ?? ????? ?? ???? ??????
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)
-
-
22Preorder Example (visit print)
a
b
c
23Preorder Example (visit print)
a
b
d
g
h
e
i
c
f
j
24Preorder Of Expression Tree
/
a
b
-
c
d
e
f
Gives prefix form of expression!
25Inorder Traversal
- public static void inOrder(BinaryTreeNode t)
-
- if (t ! null)
-
- inOrder(t.leftChild)
- visit(t)
- inOrder(t.rightChild)
-
-
26Inorder Example (visit print)
b
a
c
27Inorder Example (visit print)
g
d
h
b
e
i
a
f
j
c
28?????? ???? ????? ?? ????? ????? ????
29Inorder Of Expression Tree
?????? ???? ?????? ????? ??????? ?? ???? ??????
?? ???
30Postorder Traversal
- public static void postOrder(BinaryTreeNode t)
-
- if (t ! null)
-
- postOrder(t.leftChild)
- postOrder(t.rightChild)
- visit(t)
-
-
31Postorder Example (visit print)
b
c
a
32Postorder Example (visit print)
g
h
d
i
e
b
j
f
c
a
33Postorder Of Expression Tree
a
b
c
d
-
e
f
/
Gives postfix form of expression!
34Traversal Applications
- Make a clone.
- Determine height.
- Determine number of nodes.
35Level 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
36Level-Order Example (visit print)
a
b
c
d
e
f
g
h
i
j
37???? ???? ??????
- ??? ???? ????? ???? ??? ?????? ?????
- ??? ?? ????? ??? ?? ????????? ?? ???? ???? ??
????!? - ??? ?????? ???? ??? ??? ?? ?? ??? ????? ????? ???
???? ???? ?? ?? ???? ????? ???? ???? - ??? ?? ????? ?? ??? ?????? ?? ???? ?? ???? ?? ??
???? ????? ???? ???? ? - ???? ???????!
38Some Examples
inorder ab
postorder ab
level order ab
39Preorder And Postorder
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).
40Inorder 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.
41Inorder 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.
42Inorder 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.
43Inorder 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.
44Inorder 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).
- ???? ?????? ?? ???? ?????? ?? ?? ????? ?? ?? ???
51Tree 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
????? ??? ?????? ???? ??? ?
54Predecessor 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.
58Tree-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)