0% found this document useful (0 votes)
139 views

But in A Binary Tree, Left Child and Right Child Are Different

This document discusses binary search trees, including their properties, operations, and implementations. Binary search trees have the property that all left descendants of a node are less than the node's value and all right descendants are greater. Common operations on binary search trees like search, insertion, and deletion are implemented recursively by comparing the search key to the root node and traversing left or right.

Uploaded by

Leming Shen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views

But in A Binary Tree, Left Child and Right Child Are Different

This document discusses binary search trees, including their properties, operations, and implementations. Binary search trees have the property that all left descendants of a node are less than the node's value and all right descendants are greater. Common operations on binary search trees like search, insertion, and deletion are implemented recursively by comparing the search key to the root node and traversing left or right.

Uploaded by

Leming Shen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

§2 Binary Trees

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

Skewed Binary Trees Complete Binary Tree


A A A

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 i1, 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

Position Iter_Find( ElementType X, SearchTree T )


{
/* iterative version of Find */
while ( T ) {
if ( X == T->Element )
return T ; /* found */
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 */
}

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 

To merge two singly linked ascending lists, both


with N nodes, into one singly linked ascending list,
what’s the minimum possible number of comparisons?

Push 5 characters ooops onto a stack. In how many


different ways that we can pop these characters and still
obtain ooops?
§3 Binary Search Trees
 Insert
Sketch of the idea:

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

SearchTree Insert( ElementType X, SearchTree T )


{
if ( T == NULL ) { /* Create and return a one-node tree */
T = malloc( sizeof( struct TreeNode ) );
if ( T == NULL )
FatalError( "Out of space!!!" );
else {
T->Element = X;
T->Left = T->Right = NULL; } How would you
} /* End creating a one-node tree */ Handle
T( N ) = duplicated
O(d)
else /* If there is a tree */ 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 */
return T; /* Do not forget this line!! */
}

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

Solution 1: reset left subtree. 20 60


55
10 30 50 70
Solution 2: reset right subtree.
45 55
52

52

8/11
§3 Binary Search Trees

SearchTree Delete( ElementType X, SearchTree T )


{ 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 */
T->Right = Delete( X, T->Right );
else /* Found element to be deleted */
if ( T->Left && T->Right ) { /* Two children */
/* Replace with smallest in right subtree */
TmpCell = FindMin( T->Right );
T->Element = TmpCell->Element;
T->Right = Delete( TmpCell->Element, T->Right ); } /* End if */
else { /* One or zero child */
TmpCell = T;
if ( T->Left == NULL ) /* Also handles 0 child */
T = T->Right;
else if ( T->Right == NULL ) T = T->Left;
free( TmpCell ); } /* End else 1 or 0 child */
return T;
}

T( N ) = O ( h ) where h is the height of the tree

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.

While the number of deleted nodes


is the same as the number of active nodes
in the tree, will it seriously affect
the efficiency of the operations?

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

You might also like