Professional Documents
Culture Documents
Properties of BST
Tree traversal
1. Inorder:
INORDER-TREE-WALK(x)
1. if x=!Nil
2. then inorder –tree-walk(left[x])
3. print key[x]
4. INORDER-TREE-
WALK(right [x])
2. PREORDER-TREE-WALK(x)
1. if x=!nil
2. print key [x]
3. PREORDER-TREE-
WALK(left (x))
4. PREORDER-TREE-
WALK(right(x))
3. POSTORDER-TREE-WALK(x)
1. if x=!nil
2. POSTORDER-TREE-
WALK(left[x])
3. POSTORDER-TREE-
WALK(right[x])
4. print[x]
Searching in BST
1. if x=nil or k=key[x]
2. then return x
3. if k<key[x]
4. then return TREE-
SEARCH(left[x],k)
5. else return TREE-
SEARCH(right[x],k)
TREE-MINIMUM(x)
1 while left(x)!=nil
2 do x<- left(x)
3 return x
MAX
TREE-MAXIMUM(x)
1 while right (x)!=nil
2 do x<- right(x)
3 return x
Insertion in BST
TREE-INSERT(T,z)
1. y<-nil
2. x<-root(T)
3. while x!=nil
4. do y<-x
5. if key[z]<key[x]
6. then x<- left[x]
7. else x<- right[x]
8. P[z]<- y
9. if y=nil
Successor in BST
TREE-SUCCESSOR(x)
1. if right [x]!=nil
2. then return TREE-
MINIMUM(right[x])
3. y<- P[x]
4. while y!=nil &x=right[y]
5. do x<- y
6. y<- P[y]
7. return y
Deletion Binary search Tree
ALGO
TREE-DELETE-(T,z)
1 if left[z]=nil or right[z]=nil
2 then y<- z
3 else y<- TREE-SuccessoR(z)
4 if left[y]!= nil
5 then x<- left[y]
6 else x<- right[y]
7 if x!=nil
8then p[x]<- p[y]
9 if p[y]=nil
10 then root[T]<- x
11 else if y=left[p[y]]
12 then left [p[y]]<- x
13 else right[p[y]]<- x
14 if y!=z
15 then key[z]<- key[y]
16 if y has either field ,copy then
17 return y
Red Black Tree
n+1<2 h/2
log(n+1)<h/2log2
log(n+1)<h/2
2log(n+1)<=h
Rotation of RB tree
Insertion in a RB tree
We insert a node in a tree & a color
that node to red.we when fire the
violated RB tree properties:
Case: if x’s uncle is red
a) change x’s grandparent to
red.
b) Change x’s uncle & parent
to black.
c) Change x to x’s
grandparent.
Case2 : if x’s uncle is black ,x is the
right child of its parent
a) change x to x’s parent.
b) Rotate x’s parent to left.
c) Case 2 is now case 3.
Insertion in RB-tree
1 Tree-Insert(T,x)
2 color(x) =Red
3 while x!= root(T) & color (parent
(x))=Red
4 if parent(x)=left(parent(parent(x)))
5 then uncle=right(parent(parent(x)))
6 if color(uncle)=Red
7 then color(parent(x))=Black
8 color(uncle)=Black
9 color(parent(parent(x))=Red
10 x= parent(parent(x))
11 else if x=right(parent(x))
12 then x= parent(x)
13 left rotate(T,x)
14 color(parent(x))=Black
15 color(parent(x))=red
16 right-rotate(T,parent(parent(x)))
17 else
(same as above with”right”& “left”
swapped)
18 color(root(x))=black
Deletion in RBT
1. if left[z] = nil[T] or
right[z]=nil[T]
2. then y<-z
3. else y<- tree successor (z)
4. if left[y]!= nil[T]
5. then x<- left[y]
6. else x<- right[y]
7. p[x]<- p[y]
8. if p[y]<- nil[T]
9. then root[T]<- x
10. else if y=left[p(y)]
11. then left[p(y)]<- x
12. else right [p(y)]<- x
13. if y!=z
14. then key[z]<- key[y]
15. copy’s data into z
16. if color[y]=black
17. then RB-delete fixup(T,x)
18. return y
Bucket sort
Radix sort
ALGO
radix sort(A,d)
for i=1 to d
use a stable sort on array A on digit i.
Qoick sort
Algo:
Quick sort(A,p,r)
1) if p<r
2) then q<- partitioned(A,p,r)
3) quick sort(A,p,q-1)
4) quick sort(A,q+1,r)
partition(A,p,r)
1) x<- A[p]
2) i<- p-1
3) j<- r+1
4) while true
5) repeat
6) j=j-1
7) until A[j]<=x
8) repeat
9) i=i+1
10)until A[i]=> x
11)if i<j
12)exchange (A[i]<->A[j])
13)else return j
Order of growth:
We have used some simplify
instruction to ease our analysis of
insertion sort.we had ignore the
actule cost of each statement &
letter on we had also ignore the
abstract cost.
We make one more simplification
that abstraction which is the rate
of growth or order of growth of
the running time.
a) we consider only the leading
term of a formula since the
lower order terms are
relatively insignificant for
large n.
b) we also ignore the leading
term contt. Coefficient since
they are also less significant.
Hence the order of growth of a
running time function is the
fastest growing term discarding
contt factor.
Merge sort
Merge sort(A,p,q,r)
1. n1 q-p+1
2. n2 r-q
3. create arrays L[1…n1+1]
&R[1…n2+1]
4. for i1 to n1
5. do L[i] A[p+i-1]
6. for j 1 to n2
7. do R[j] A[q+j]
8. L[n1+1]∞
9. R[n2+1] ∞
10. i1
11. j1
12. for k p to r
13. do if L[i]<=R[j]
14. then A[k] L[i]
15. ii+1
16. else A[k]R[j]
17. jj+1
Insertion sort
1 for j2 to length [A]
2 key A[j]
3 //insert A[j] into the sorted seqn.A[1,
…j-1]
4 i=j-1
5 while i>0 & A[i]>key
6 do A[i+1]A[i]
7 i=i-1
8 A[i+1]=key
Cost iteration
C1 n