Download presentation
Presentation is loading. Please wait.
Published byDomenic Robbins Modified over 9 years ago
1
Node Declaration and Example typedef struct nodeT { int key; struct nodeT *left, *right; } nodeT, *treeT; 1 10 2 6 15 7 12 17
2
Free Memory used by Tree 2 void FreeTree(nodeT **tptr) { } 10 2 6 15 7 12 17
3
Free Memory used by Tree 3 void FreeTree(nodeT **tptr) { nodeT *tmp; tmp = *tptr; if (tmp == NULL) return; FreeTree(&tmp->left); FreeTree(&tmp->right); free (tmp); } 10 2 6 15 7 12 17
4
Count Number of Leaf Nodes 4 int CountLeaf(nodeT *t) { } 10 2 6 15 7 12 17 Leaf Nodes: 2, 7, 12, 17
5
Count Number of Leaf Nodes 5 int CountLeaf(nodeT *t) { if (t == NULL) return 0; if (t->left ==NULL && t->right == NULL) return 1; else return(Count_Leaf(t->left)+Count_Leaf(t>right)); } 10 2 6 15 7 12 17 Leaf Nodes: 2, 7, 12, 17
6
Make a Copy of a Tree 6 nodeT* CopyTree(nodeT *t) { } 10 2 6 15 7 12 17 10 2 6 15 7 12 17
7
Make a Copy of a Tree 7 nodeT* CopyTree(nodeT *t) { nodeT *tmp; if (t==NULL) return(NULL); tmp = (nodeT *) malloc(sizeof(struct nodeT)); tmp->key = t->key; tmp->left = CopyTree(t->left); tmp->right = CopyTree(t->right); return(tmp); } 10 2 6 15 7 12 17 10 2 6 15 7 12 17
8
Reverse a Tree 8 void Reverse(nodeT *t) { } 10 2 6 15 7 12 17 10 17 15 6 12 7 2
9
Reverse a Tree 9 void Reverse(nodeT *t) { if (t == NULL) return; else { Reverse(t->left); Reverse(t->right); nodeT *ptr = t->left; t->left = t->right; t->right = ptr; return; } 10 2 6 15 7 12 17 10 17 15 6 12 7 2
10
Root-Leaf Path with Sum 10 int PathWithSum(nodeT *t, int sum) { } 10 2 6 15 7 12 17 18 = 10 + 6 + 2 21 = 10 + 6 + 7 42 = 10 + 15 + 17
11
Root-Leaf Path with Sum 11 int PathWithSum(nodeT *t, int sum) { if (t==NULL) if (sum == 0) return 1; else return 0; if (t->left == NULL && t->right == NULL) if (sum == t->key) return 1; else return 0; if (t->left != NULL && PathWithSum(t->left,sum - t->key)) return 1; if (t->right != NULL && PathWithSum(t->right, sum - t->key)) return 1; return 0; } 10 2 6 15 7 12 17 18 = 10 + 6 + 2 21 = 10 + 6 + 7 42 = 10 + 15 + 17
12
Print Nodes with Distance k from Root 12 int kDistanceNodes(nodeT *t, int k) { } 10 2 6 15 7 12 17 k = 1: 6, 15 k = 2 : 2, 7, 12, 17
13
Print Nodes with Distance k from Root 13 int kDistanceNodes(nodeT *t, int k) { if (t==NULL || k<0) return; if (k==0) printf("%d ",t->key); else { kDistanceNodes(t->left,k-1); kDistanceNodes(t->right,k-1); } 10 2 6 15 7 12 17 k = 1: 6, 15 k = 2 : 2, 7, 12, 17
14
Print Nodes with value [k1,k2] 14 void PrintBST(nodeT *t, int k1, int k2) { } 10 2 6 15 7 12 17 [10,15] = 10, 12, 15 [8, 13] = 10, 12
15
Print Nodes with value [k1,k2] 15 void PrintBST(nodeT *t, int k1, int k2) { if (t==NULL) return; if ((t->key >= k1) && (t->key <=k2)) printf("%d ",t->key); PrintBST(t->left, k1, k2); PrintBST(t->right, k1, k2); } 10 2 6 15 7 12 17 [10,15] = 10, 12, 15 [8, 13] = 10, 12
16
Print Nodes with value [k1,k2] 16 void PrintBST2(nodeT *t, int k1, int k2) { if (t==NULL) return; if ((t->key >= k1) && (t->key <=k2)) printf("%d ",t->key); if (t->key > k1) PrintBST2(t->left, k1, k2); if (t->key < k2) PrintBST2(t->right, k1, k2); } 10 2 6 15 7 12 17 [10,15] = 10, 12, 15 [8, 13] = 10, 12
17
Find Minimum Value in BST 17 int Minimum(nodeT *t) { } 10 2 6 15 7 12 17 Minimum is 2
18
Find Minimum Value in BST 18 int Minimum(nodeT *t) { while (t->left != NULL) t = t->left; return(t->key); } 10 2 6 15 7 12 17 Minimum is 2
19
Recursively Find Minimum Value in BST 19 int Minimum2(nodeT *t) { } 10 2 6 15 7 12 17 Minimum is 2
20
Recursively Find Minimum Value in BST 20 int Minimum2(nodeT *t) { if (t->left == NULL) return(t->key); else return(Minimum2(t->left)); } 10 2 6 15 7 12 17 Minimum is 2
21
Find Maximum Value in BST 21 int Maximum(nodeT *t) { } 10 2 6 15 7 12 17 Maximum is 17
22
Find Maximum Value in BST 22 int Maximum(nodeT *t) { while (t->right != NULL) t = t->right; return(t->key); } 10 2 6 15 7 12 17 Maximum is 17
23
Recursively Find Maximum Value in BST 23 int Maximum2(nodeT *t) { } 10 2 6 15 7 12 17 Maximum is 17
24
Recursively Find Maximum Value in BST 24 int Maximum2(nodeT *t) { if (t->right == NULL) return(t->key); else return(Maximum2(t->right)); } 10 2 6 15 7 12 17 Maximum is 17
25
Nodes with Parent Pointer typedef struct nodeT2 { int key; struct nodeT2 *left, *right, *parent; } nodeT2, *treeT2; 25 10 2 6 15 7 12 17
26
Make a Copy of a Tree with Parent Pointers 26 nodeT2* CopyTree2(nodeT *t) { } 10 2 6 15 7 12 17 10 2 6 15 7 12 17
27
Make a Copy of a Tree with Parent Pointers 27 nodeT2* CopyTree2(nodeT *t) { nodeT2 *tmp; if (t==NULL) return(NULL); tmp = (nodeT2 *) malloc(sizeof(struct nodeT2)); tmp->key = t->key; tmp->parent = NULL; tmp->left = CopyTree2(t->left); if (tmp->left != NULL) tmp->left->parent = tmp; tmp->right = CopyTree2(t->right); if (tmp->right != NULL) tmp->right->parent = tmp; return(tmp); } 10 2 6 15 7 12 17 10 2 6 15 7 12 17
28
Find Successor of a Node 28 nodeT2 *Successor(nodeT2 *t, nodeT2 *n) { } 10 2 6 15 7 12 17 Successor of 2 is 6 Successor of 6 is 7 Successor of 7 is 10
29
Find Successor of a Node 29 nodeT2 *Successor(nodeT2 *t, nodeT2 *n) { if (n->right != NULL) { nodeT2* ptr=n->right; while (ptr->left != NULL) ptr = ptr->left; return(ptr); } nodeT2* p = n->parent; while (p != NULL && n == p->right){ n = p; p = p->parent; } return (p); } 10 2 6 15 7 12 17 Successor of 2 is 6 Successor of 6 is 7 Successor of 7 is 10
30
Insert a Node 30 nodeT2* InsertNode2(nodeT2 *t, int key) { } 10 2 6 15 7 12 17 8 Insert 8
31
Insert a Node 31 nodeT2* InsertNode2(nodeT2 *t, int key) { if (t == NULL) { t=(nodeT2 *)malloc(sizeof(nodeT2)); t->key = key; t->left=t->right= t->parent=NULL; return(t); } if (key key) { t->left = InsertNode2(t->left, key); t->left ->parent = t; return(t); } else if (key> t->key ){ t->right = InsertNode2(t->right, key); t->right ->parent = t; return(t); } 10 2 6 15 7 12 17 8 Insert 8
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.