The.Algorithm.Design.Manual.Springer-Verlag.1998
The.Algorithm.Design.Manual.Springer-Verlag.1998 The.Algorithm.Design.Manual.Springer-Verlag.1998
Lecture 8 - binary trees Tree-insert(T,z) left[x] right[x] y = NIL x = root[T] while if y = NIL do y = x then y is maintained as the parent of x, since x eventually becomes NIL. else if key[z] < key[y] The final test establishes whether the NIL was a left or right turn from y. Insertion takes time proportional to the height of the tree, O(h). file:///E|/LEC/LECTUR17/NODE8.HTM (7 of 13) [19/1/2003 1:34:46] if key[z] < key[x] then else then x = else x =
Lecture 8 - binary trees Listen To Part 8-12 Tree Deletion Deletion is somewhat more tricky than insertion, because the node to die may not be a leaf, and thus effect other nodes. Case (a), where the node is a leaf, is simple - just NIL out the parents child pointer. Case (b), where a node has one chld, the doomed node can just be cut out. Case (c), relabel the node as its successor (which has at most one child when z has two children!) and delete the successor! This implementation of deletion assumes parent pointers to make the code nicer, but if you had to save space they could be dispensed with by keeping the pointers on the search path stored in a stack. Tree-Delete(T,z) if (left[z] = NIL) or (right[z] = NIL) if if if p[y] = NIL then else Tree-Successor(z) then else then then file:///E|/LEC/LECTUR17/NODE8.HTM (8 of 13) [19/1/2003 1:34:46] else if (y = left[p[y]])
- Page 871 and 872: Lecture 3 - recurrence relations A
- Page 873 and 874: Lecture 4 - heapsort Note iteration
- Page 875 and 876: Lecture 4 - heapsort The convex hul
- Page 877 and 878: Lecture 4 - heapsort 1. All leaves
- Page 879 and 880: Lecture 4 - heapsort left = 2i righ
- Page 881 and 882: Lecture 4 - heapsort Since this sum
- Page 883 and 884: Lecture 4 - heapsort Selection sort
- Page 885 and 886: Lecture 4 - heapsort Greedy Algorit
- Page 887 and 888: Lecture 5 - quicksort Partitioning
- Page 889 and 890: Lecture 5 - quicksort Listen To Par
- Page 891 and 892: Lecture 5 - quicksort Listen To Par
- Page 893 and 894: Lecture 5 - quicksort The worst cas
- Page 895 and 896: Lecture 5 - quicksort Mon Jun 2 09:
- Page 897 and 898: Lecture 6 - linear sorting Since 2i
- Page 899 and 900: Lecture 6 - linear sorting With uni
- Page 901 and 902: Lecture 6 - linear sorting Listen T
- Page 903 and 904: Lecture 7 - elementary data structu
- Page 905 and 906: Lecture 7 - elementary data structu
- Page 907 and 908: Lecture 7 - elementary data structu
- Page 909 and 910: Lecture 7 - elementary data structu
- Page 911 and 912: Lecture 7 - elementary data structu
- Page 913 and 914: Lecture 7 - elementary data structu
- Page 915 and 916: Lecture 7 - elementary data structu
- Page 917 and 918: Lecture 8 - binary trees - Joyce Ki
- Page 919 and 920: Lecture 8 - binary trees TREE-MINIM
- Page 921: Lecture 8 - binary trees Inorder-Tr
- Page 925 and 926: Lecture 8 - binary trees insert(b)
- Page 927 and 928: Lecture 8 - binary trees Not (1) -
- Page 929 and 930: Lecture 9 - catch up Next: Lecture
- Page 931 and 932: Lecture 10 - tree restructuring 1.
- Page 933 and 934: Lecture 10 - tree restructuring Lis
- Page 935 and 936: Lecture 10 - tree restructuring Not
- Page 937 and 938: Lecture 10 - tree restructuring Cas
- Page 939 and 940: Lecture 11 - backtracking Next: Lec
- Page 941 and 942: Lecture 11 - backtracking Only 63 s
- Page 943 and 944: Lecture 11 - backtracking Recursion
- Page 945 and 946: Lecture 11 - backtracking Specifica
- Page 947 and 948: Lecture 12 - introduction to dynami
- Page 949 and 950: Lecture 12 - introduction to dynami
- Page 951 and 952: Lecture 12 - introduction to dynami
- Page 953 and 954: Lecture 12 - introduction to dynami
- Page 955 and 956: Lecture 12 - introduction to dynami
- Page 957 and 958: Lecture 13 - dynamic programming ap
- Page 959 and 960: Lecture 13 - dynamic programming ap
- Page 961 and 962: Lecture 13 - dynamic programming ap
- Page 963 and 964: Lecture 14 - data structures for gr
- Page 965 and 966: Lecture 14 - data structures for gr
- Page 967 and 968: Lecture 14 - data structures for gr
- Page 969 and 970: Lecture 14 - data structures for gr
- Page 971 and 972: Lecture 14 - data structures for gr
Lecture 8 - binary trees<br />
Listen To Part 8-12<br />
Tree Deletion<br />
Deletion is somewhat more tricky than insertion, because the node to die may not be a leaf, and thus effect other nodes.<br />
Case (a), where the node is a leaf, is simple - just NIL out the parents child pointer.<br />
Case (b), where a node has one chld, the doomed node can just be cut out.<br />
Case (c), relabel the node as its successor (which has at most one child when z has two children!) and delete the successor!<br />
This implementation of deletion assumes parent pointers to make the code nicer, but if you had to save space they could be<br />
dispensed with by keeping the pointers on the search path stored in a stack.<br />
Tree-Delete(T,z)<br />
if (left[z] = NIL) or (right[z] = NIL)<br />
if<br />
if<br />
if p[y] = NIL<br />
then<br />
else Tree-Successor(z)<br />
then<br />
else<br />
then<br />
then<br />
file:///E|/LEC/LECTUR17/NODE8.HTM (8 of 13) [19/1/2003 1:34:46]<br />
else if (y = left[p[y]])