当前位置:
文档之家› 浙大数据结构课件-update_hqm_DS05_Ch04b
浙大数据结构课件-update_hqm_DS05_Ch04b
if ( T->Left == NULL ) return T; /* found left most */
else return FindMin( T->Left ); /* keep moving to left */
}
FindMax
Position FindMax( SearchTree T ) {
}
7/13
Insert
§3 Binary Search Trees
Sketch of the idea:
30
Insert 80
check if 80 is already in the tree
5
40
80 > 40, so it must be the right child
2 25 35 80 of 40
return T; /* Do not forget this line!! */
}
9/13
Delete
§3 Binary Search Trees
❖ Delete a leaf nodeNo: tRe:eTshetesites kpianrdesntolfinnkodtoesNULL. ❖ Delete a degree 1 nohdavee: dReegprelaecaettmheosnto1d.e by its single child.
{ Position TmpCell;
if ( T == NULL ) Error( "Element not found" );
else if ( X < T->Element ) /* Go left */
T->Left = Delete( X, T->Left );
else if ( X > T->Element ) /* Go right */
the key in the root of the subtree. (4) The left and right subtrees are also binary search trees.
30
60
20
5
40
70
15 25
2
65 80
12 10
22
3/13
2. ADT
§3 Binary Search Trees
TmpCell = FindMin( T->Right );
T->Element = TmpCell->Element;
if ( X < T->Element ) T = T->Left ; /*move down along left path */
else T = T-> Right ; /* move down along right path */
} /* end while-loop */ return NULL ; /* not found */ }
【Definition】A binary search tree is a binary tree. It may be
empty. If it is not empty, it satisfies the following properties: (1) Every node has a key which is an integer, and the keys are
distinct. (2) The keys in a nonempty left subtree must be smaller than
the key in the root of the subtree. (3) The keys in a nonempty right subtree must be larger than
TH(aNnd)le=dOup( ldic)ated Keys?
if ( X < T->Element )
T->Left = Insert( X, T->Left );
else
if ( X > T->Element )
T->Right = Insert( X, T->Right );
/* Else X is in the tree already; we'll do nothing */
Proof: Let n1 be the number of nodes of degree 1, and n the
total number of nodes. Then
n = n0 n1 n2 1
Let B be the number of branches. Then nn=~ BB?+ 1. 2
B
B
Skewed Binary Trees
A
A
B
B
C
C
D
D
Skewed to the left Skewed to the right
1/13
Complete Binary Tree A
B
C
D EF G
HI
All the leaf nodes are on two adjacent levels
Properties of Binary Trees
Since all e out of nodes of degree 1 or 2, we have B
~ n1 & n2 ? B = n1 + 2 n2. 3
n0 = n2 + 1
2/13
§3 The Search Tree ADT -- Binary Search Trees
1. Definition
〖Example〗 Delete 60
40
Solution 1: reset left subtree. Solution 2: reset right subtree.
20
6505
10 30 50 70
45 552
52
10/13
§3 Binary Search Trees
SearchTree Delete( ElementType X, SearchTree T )
Insert 35 chTechkisifis3t5hieslaalsrteandoydien the tree when35se<a4rw0c,hesfoeonritctomhuuensktteebryenthuemlebfetrc.hild of 40
Insert 25
chIetcwkiilfl b25e itshealpreaardeynitn the tree of the new node.
Objects: A finite ordered list with zero or more elements.
Operations:
SearchTree MakeEmpty( SearchTree T ); Position Find( ElementType X, SearchTree T ); Position FindMin( SearchTree T ); Position FindMax( SearchTree T ); SearchTree Insert( ElementType X, SearchTree T ); SearchTree Delete( ElementType X, SearchTree T ); ElementType Retrieve( Position P );
not ) /*
iffosumndallienratnhaenmrpotoytt*r/eteaiT*l /rheecsuerasiroens.
return Find( X, T->Left ); /* search left subtree */
else
if ( X > T->Element ) /* if larger than root */
❖ Delete a degree 2 node :
Replace the node by the largest one in its left subtree or
the smallest one in its right subtree.
Delete the replacing node from the subtree.
§2 Binary Trees
The maximum number of nodes on level i is 2 i1, i 1.
The maximum number of nodes in a binary tree of depth k is
2 k 1, k 1.
For any nonempty binary tree, n0 = n2 + 1 where n0 is the number of leaf nodes and n2 the number of nodes of degree 2.
return Find( X, T->Right ); /* search right subtree */
else /* if X == root */
return T; /* found */
} T( N ) = S ( N ) = O( d ) where d is the depth of X
4/13
3. Implementations
Find
§3 Binary Search Trees
Must this test be performed first?
Position Find( ElementType X, SearchTreeT == NULL ) return NULL; /* ( X < T->Element