Вы находитесь на странице: 1из 45

BINARY SEARCH TREE

BINARY SEARCH TREE


A binary tree T is called a binary search tree, if
each node N of T has the following property.
I. The value at N is greater than every value
in the left sub tree of N
II. The value at N is less than every value in
the right sub tree of N
Example:
15

14 18

10

7 12
1. Algorithm search(K,r)
2. // K is a root node r is a node pointer
3. {
4. if(r=nil) then
5. return (false)// no node in the tree
6. else if (k=r.key) then
7. return (true) // node found in the tree
8. else if (k<r.key) then
9. search(K,r.left) // k may be left subtree, call
the algorithm with left pointer as root
10. else if (K>r.key) then
11. search(K,r.right)// k may be in right
subtree, call the algorithm with night pointer
as root.
12. }
Algorithm to find a node in binary search
tree
To find a node with key K in the binary search tree
rooted at R base i. The following steps are taken
1. if the tree is empty the search terminates
unsuccessfully
2. if K=K base i the search terminates successfully.
3. if K < K base i the left sub tree of R base i is
searched.
4. if K > K base i the right sub tree of R base i is
searched.
Algorithm to insert a node in a binary search
tree
To insert a new node with key K in the binary search
tree rooted at R base i. the following steps are taken.
1. algorithm insert (K, r)
2. // K is a key node to be inserted
3. // r is a node pointer
4.{
5. if (r=nil) then
6. {
7. new(r)
8. r.key=K
9. r. leftchild= nil
10. r. rightchild=nil
11.}
12.else if(K<r.key) then
13. insert(K, r, leftchild)
14. else if (K>r.key) then
15. insert(K, r, rightchild)
16. else if(K=r.key) then
17. print(node already present)
18.}
Deleting node from binary search tree
To delete node N with key K from a binary search
tree T there are three cases
1. when N has no children.
2. when N has exactly one child.
3. when N has two children.
1. when N has no children.
the process to delete a node N when it has no
children is simply delete the node N from T and
replace the location of P(N) with nil
2. when N has exactly one child.
delete N from T by replacing P(N) with the location
of only child of N. consider the binary search tree T
40

30 60

15
After deleting the node 30 the tree T look like
this
40

15 60
3. when N has two children
find the smallest element in the descendent
of rightchild of N; S(N)
Delete node N from T by replacing location of
S(N) in P(N).
Consider the binary tree T

40

30 60

50 70
50

30 60

70
Algorithm to delete a node from binary
search tree
1. Algorithm delete (K,r)
2.// K is a node of key to be deleted
3. {
4. if(r!=nil) then
5. if(K<r.key) then
6. delete (K,r.leftchild)
7.else if (K,r.rightchild) then
8. delete (K,r.rightchild)
9.Else if(r.rightchild=nil) and (r.leftchild=nil) then
10. r=nil
11.Else if(r.leftchild=nil) then
12. r=r.rightchild
13. Else if (r.rightchild=nil) then
14. r=r.leftchild
15. Else // both children are present
16. r.key=deletemin(r.rightchild)
17.}
The algorithm deletemin delete the minimum element in the
right child of N and return the number deleted.
Representing binary tree in memory
Let T be a binary tree. There are two ways of representing T
in memory
1. linked representation
2. Sequential representation.
1. linked representation
Suppose the node N(T) correspond to a location K such that
i) INFO(K) contains the data at the node N
ii) LEFT(K) contains the location of the leftchild of Node N
iii) RIGHT(K) contains the location of the rightchild of Node
N
Consider the binary T

B C

D E G

H
LINKED REPRESENTATION OF IN MEMORY
INFO LEFT RIGHT
1 C 8 0
ROOT
4 2 E 6 0
3 10
4 A 7 1
AVAIL
5 5 3
6 H 0 0
7 B 9 2
8 G 0 0
9 D 0 0
10 0

Here ROOT will contain the location of the Root R of T.


2. Sequential representation
Sequential representation used a single linear
array A as follows
1. the root of T is stored in A[1]
2. if a node N occupies A[K], then its leftchild is
stored in A[2*K] and its rightchild is stored in
A[2*K+1].
If A[1] = NULL indicates that the tree is empty.
sequential of representation of in memory
Array A
A
1
B
2
C
3
D
4
5 E

6 G
-
7
-
8
-
9
H
10
PROBLEM
Show the resulting binary search tree if the
elements are added into in the following order
G, T, E, M, H, P, A, F
Solution
a) item=G b) item = T c) item=E
G G G

T E T
d) item=M e)item=H f)item=p

G G G

ET E T E T

M M M

H H P
g)item=A h)item=F
G G

E T E T

AMA F M

H P H P
GRAPHS
Definition: A graph, G consists of two sets V and
E, Vis a finite non empty set of vertices, these
pair are called edges. V(G)and E(G) will
represent the set of vertices and edges of graph
G. we can write G= (V, E) to represent a graph.
The following figure shows the two graph G1
and G2
The graph G1 is undirected and G2 is directed
graph.
GRAPH G1 GRAPH G2
1 2 1 2

3
4 3
V(G1)= {1,2,3}
E(G1)= {(1,2),(1,3),(2,1), (2,3),(3,1),(3,2)}

V(G2)={1,2,3,4}
E(G2)={(1,2),(2,2),(2,3),(3,4),(4,1),(4,2),(4,3)}
Directed graph
A graph is directed when its edges have an
orientation and this provide a one-way
connection an indicated by an arrow. The
graph G2 is directed graph.
Weighted graph: When each edge of a graph is
associated with a real no, called its weight, the
graph is said to be weighted. The weighted
graph may directed or undirected.
Degree of vertex: the degree of a vertex in an
undirected graph is the number of edges
incident on it
The degree of a vertex in a directed graph is its
in-degree plus its out-degree
Example: the graph G1 has a degree 3 and the
graph G2 has the degree 5
In-degree and out-degree of graph
For directed graph the in-degree and out-
degree of a vertex are the number of incoming
and outgoing edges respectively.
Example: the following figure has vertex 2 has
in-degree 3, out-degree 2.
1 2

4 3
Complete graph: A simple graph in which
there exists an edge between every pair of
vertices is called a complete graph. Complete
graph of four vertices is shown in the figure. A
complete graph is sometimes also referred to
as a universal graph.
Example
Degree of a graph: The maximum degree of
any vertex in a graph is called degree of a
graph.
Example: the degree of graph is 2 for the
following figure

1 2

3
1) Representation of graph:
1.1 Adjacency matrix: Let G = (V,E) be a graph
with n= |V| m= |E| and V = {v1,v2,,vn}
G can be represented by an n*n matrix.
A=(aij) called the adjacency matrix for G.
A is defined by
aij={1 vi vj E
0, otherwise for 1<=i,j<=h
if G is a weighted graph
aij={W(vi vj) if vi vj E
C otherwise
C is a constant
1.2:Adjacency list: an alternative to the
adjacency matrix representation is an array
indexed by vector number containing linked
list is called adjacency lists.
For each vertex vi, the ith array contains a list
with information on all edges of G that leave vi
An undirected graph a)
1 2

5 6

3 4
Adjacency matrix of (a)
0 1 1 0 0 0
1 0 0 1 1 0
1 0 0 1 0 0
0 1 10 1 0
0 1 0 1 0 1
0 0 0 0 1 0
Adjacency list structure of (a)
1 2 3 nil

2 1 4 5 nil

3
1 4 nil
4
2 3 5 nil
5
2 4 6 nil
6
5 nil

Adjacency matrix and adjacency list of an undirected graph without edge weight
Two representation for a weighted directed
graph
a) A Weighted graph

1 2

5 6

3 4
b) adjacency matrix of (a)
0 25 0 0 0 0
0 0 0 14 16 0
5 0 0 6 0 0
0 0 18 0 0 0
0 0 0 32 0 11
0 0 0 0 0 0
c)Adjacency list structure of a)
1 2 25 nil

2 4 14 5 16 nil

3 1 5 4 6 nil

4
3 18 nil

5
4 32 6 11 nil
6

Two representation for a weighted directed graph


Directed acyclic graphs: A directed acyclic
graph, or Dag is a directed graph with no
cycles. The following figure is dag

1 2

4 3
Topological sort: It is a process of assigning a linear
ordering to the vertices of a dag so that if there is an
edge from vertex i to vertex j, then I appears b4 j in the
linear ordering.
Algorithm start with the node N, with zero indegree
ie., without any predecessors.
The algorithm will repeat the following steps until the
graph G is empty.
1. Finding a node N with zero in degree
2. Deleting N and its edge from the graph G.
Algorithm for topological sort:
1. Find the indegree of each node N ofG
2. Put in a queue all the nodes with zero indegree.
3. Remove the front node N of the queue.
4.Repeat the following foe each w adjacent to N
a) Reduce the indegree of w
b) if indegree of w is zero then add w to the rear of
thequeue.
5.goto step 4 if queue is not empty.
6. end.

Вам также может понравиться