









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










void foo(Tree root) { //Do something } int main() { Tree T; T = buildATree(); //Build a tree and //let T point to the root of the tree foo(T); //Pass value of T (address of node) to root ... }
void foo(Tree root) { root->label = ‘X’; ... } int main() { Tree T; T = buildATree(); //Build a tree and //let T point to the root of the tree foo(T); ... } A B C D E F
X B C D E F
void foo( *Tree pT ) { //Do something } int main() { Tree T; T = buildATree(); //Build a tree and //let T point to the root of the tree foo( &T ); //Pass address of T to pT … }
A
A C D E F
void makeNull( Tree pT ) { (pT) = NULL; } int main() { Tree T; T = buildATree(); //Build a tree and //let T point to the root of the tree makeNull( &T ); //Pass address of T to root … }
A
A C D E F
void insert(LabelType x, *Tree pT ) { Case 1: If pT is a null tree, if (pT == NULL) { //1a. create a node, Node root = (Node)malloc(sizeof(Node); //1b. place x and root->label = x; //1c. make pT point to the new node (pT) = root; return; } Case 2: //2a. Find a right position to insert //2b. Insert a new node }
Node *node = *pT;
while (node != NULL) { parent = node if x == node->label => break if x < node->label node = node->left else node = node->right }
Node child = (Node)malloc(sizeof(Node)); child->label = x; if (x < parent) parent->left = child; else parent->right = child; 1 0 5 1 5 2 0 4 8 1 4 1 8
void insert(LabelType x, Tree pT ) { Case 1: If pT is a null tree, if (pT == NULL) { //1a. create a node, Node root = (Node)malloc(sizeof(Node); //1b. place x and root->label = x; //1c. make pT point to the new node (pT) = root; return; } Case 2: Compare x to (pT)->label //2a. x == (pT)->label if (x == (pT)->label) return; // 2 b. x < (pT)->label => recursively insert to left subtree if (x == (pT)->label) insert(x, &((pT)->left)); // 2 c. x > (pT)->label => recursively insert to right subtree else insert(x, &((*pT)->right)); }
void delete(LabelType x, *Tree pT ) { Step 1 : find the node content x Step 2: delete the node and adjust the tree }
Node *node = *pT;
Node *parent = NULL; while (node != NULL) { if (x == node->label) break; parent = node; if (x < node->label) node = node->left; else node = node->right; }
void delete(LabelType x, Tree pT ) { if ((pT) == NULL) return; Step 1 : find the node containing x Node *node = *pT, parent; while (node != NULL) { if (x == node->label) break; parent = node; //keep parent before moving node if (x < node->label) node = node->left; else node = node->right; } if (node == NUL) return; //x does not appear in tree Step 2: delete the node and adjust the tree Case 1: node has no child if (node->left == NULL && node->right == NULL) { if (parent == NULL) (pT) = NULL; else if (node == parent->left) parent->left = NULL; else parent->right = NULL; return; } Case 2 : node has two children if (node->left != NULL && node->right != NULL) { node->label = deleteMin(&(node->right)); return; } Case 3 : node has only one child Node child; if (node->left == NULL) child = node->right; else child = node->left; if (parent == NULL) (pT) = child; else if node = parent->left) parent->left = child; else parent->right = child; }
LabelType deleteMin( *Tree pT ) { Step 1: find the smallest node Step 2 : delete it }
Node *node = *pT; Node *parent = NULL; while (node->left != NULL) { parent = node; node = node->left; }
LabelType deleteMin( *Tree pT ) { Step 1: find the smallest node Node *node = *pT; Node parent = NULL; while (node->left != NULL) { parent = node; node = node->left; } Step 2 : delete it, and return its label if (parent == NULL) //node to be delete is the root (pT) = node->right; else if (node == parent->left) parent->left = node->right; else parent->right = node->right; return node->label; }