But in A Binary Tree, Left Child and Right Child Are Different
But in A Binary Tree, Left Child and Right Child Are Different
Note:
Note:InInaatree,
tree,the
theorder
orderof ofchildren
childrendoes
doesnot
notmatter.
matter.
But
Butininaabinary
binarytree,
tree,left
leftchild
childand
andright
rightchild
childare
are
different.
different.
A A
and are two different binary trees.
B B
B B B C
C C D E F G
D D H I
Skewed to the left Skewed to the right All the leaf nodes are on two
adjacent levels
17/18
§2 Binary Trees
Properties of 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.
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 =~ B
B?+ 1. 2
Since all branches come out of nodes of degree 1 or
2, we have B ~= n11 &
+ 2nn 2.
2 ?
3
n0 = n2 + 1
18/18
§3 The Search Tree ADT -- Binary Search Trees
1. Definition
【 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
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
1/11
§3 Binary Search Trees
2. ADT
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 );
2/11
§3 Binary Search Trees
3. Implementations
Must this test
be performed first?
Find
Position Find( ElementType X, SearchTree T )
{
if ( T == NULL )
These are
return NULL; /* not found in an empty tree */
tail recursions.
if ( X < T->Element ) /* if smaller than root */
return Find( X, T->Left ); /* search left subtree */
else
if ( X > T->Element ) /* if larger than root */
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
3/11
§3 Binary Search Trees
4/11
§3 Binary Search Trees
FindMin
Position FindMin( SearchTree T )
{ T( N ) = O ( d )
if ( T == NULL )
return NULL; /* not found in an empty tree */
else
if ( T->Left == NULL ) return T; /* found left most */
else return FindMin( T->Left ); /* keep moving to left */
}
FindMax
Position FindMax( SearchTree T )
{ T( N ) = O ( d )
if ( T != NULL )
while ( T->Right != NULL )
T = T->Right; /* keep moving to find right most */
return T; /* return NULL or the right most */
}
5/11
Quiz
Insert 80
30
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
Insert 35 Thisifis35
check theislast nodein the tree
already
we encounter
35search
when < 40, so it must be the left child of 40
for the key number.
Insert 25 It will be the parent
check if 25 is already in the tree
of the new node.
25 > 5, so it must be the right child of 5
6/11
§3 Binary Search Trees
7/11
§3 Binary Search Trees
Delete
Delete a leaf node : Reset
Note: its kinds
These parentoflink to NULL.
nodes
have: degree
Delete a degree 1 node at the
Replace most 1. by its single child.
node
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.
〖 Example 〗 Delete 60 40
52
8/11
§3 Binary Search Trees
9/11
§3 Binary Search Trees
Note:
Note:
IfIfthere
thereare
arenot
notmany
manydeletions,
deletions,then
thenlazy
lazydeletion
deletion
may
maybe beemployed:
employed:add addaaflag
flagfield
fieldto
toeach
eachnode,
node,to
tomark
mark
ififaanode
nodeisisactive
activeororisisdeleted.
deleted. Therefore
Thereforewewecan
candelete
delete
aanode
nodewithout
withoutactually
actuallyfreeing
freeingthethespace
spaceof
ofthat
thatnode.
node.
IfIfaadeleted
deletedkeykeyisisreinserted,
reinserted,wewewon’t
won’thave
haveto
tocall
call
malloc
mallocagain.
again.
10/11
§3 Binary Search Trees
4. Average-Case Analysis
Question: Place n elements in a binary search tree. How
high can this tree be?
Answer: The height depends on the order of insertion.
〖 Example 〗 Given elements 1, 2, 3, 4, 5, 6, 7. Insert
them into a binary search tree in the orders:
4, 2, 1, 3, 6, 5, 7 and 1, 2, 3, 4, 5, 6, 7
4 1
2
2 6 3 h=6
1 3 5 7 4
5
h=2
6
7
11/11