Download presentation
Presentation is loading. Please wait.
Published byDorcas Phelps Modified over 8 years ago
1
Binary Search Trees
2
2 Overview Recursive linked list ops Binary Trees
3
3 Recursion w/ LL Recursion can be used to traverse a linked list Any recursive solution has an iterative counterpart –recursive is sometime more elegant though 2 functions : –countNodes(ListNode*); –showReverse(ListNode*);
4
4 Counting Nodes in a List int NumberList::numNodes() { return countNodes(head); } int NumberList::countNodes(ListNode* nodePtr) { if(nodePtr != NULL) return 1 + countNodes(nodePtr->next); else return 0; } Must provide public member function. Recursive function must start at head, but head can’t be passed in from outside the class.
5
5 Counting Nodes in a List int NumberList::countNodes(ListNode* nodePtr) { if(nodePtr != NULL) return 1 + countNodes(nodePtr->next); else return 0; } 534 Head Null 300030083016
6
6 Counting Nodes in a List int NumberList::countNodes(3000) { if(nodePtr != NULL) return 1 + countNodes(3008); else return 0; } 534 Head Null 300030083016 stack
7
7 Counting Nodes in a List int NumberList::countNodes(3000) { if(nodePtr != NULL) return 1 + countNodes(3008); else return 0; } 534 Head Null 300030083016 int NumberList::countNodes(3008) { if(nodePtr != NULL) return 1 + countNodes(3016); else return 0; } stack
8
8 Counting Nodes in a List int NumberList::countNodes(3000) { if(nodePtr != NULL) return 1 + countNodes(3008); else return 0; } 534 Head Null 300030083016 int NumberList::countNodes(3008) { if(nodePtr != NULL) return 1 + countNodes(3016); else return 0; } int NumberList::countNodes(3016) { if(nodePtr != NULL) return 1 + countNodes(NULL); else return 0; } stack
9
9 Counting Nodes in a List int NumberList::countNodes(3000) { if(nodePtr != NULL) return 1 + countNodes(3008); else return 0; } 534 Head Null 300030083016 int NumberList::countNodes(3008) { if(nodePtr != NULL) return 1 + countNodes(3016); else return 0; } int NumberList::countNodes(3016) { if(nodePtr != NULL) return 1 + countNodes(NULL); else return 0; } int NumberList::countNodes(NULL) { if(nodePtr != NULL) return 1 + countNodes(nodePtr->next); else return 0; } stack
10
10 Counting Nodes in a List int NumberList::countNodes(3000) { if(nodePtr != NULL) return 1 + countNodes(3008); else return 0; } 534 Head Null 300030083016 int NumberList::countNodes(3008) { if(nodePtr != NULL) return 1 + countNodes(3016); else return 0; } int NumberList::countNodes(3016) { if(nodePtr != NULL) return 1 + 0; else return 0; } stack
11
11 Counting Nodes in a List int NumberList::countNodes(3000) { if(nodePtr != NULL) return 1 + countNodes(3008); else return 0; } 534 Head Null 300030083016 int NumberList::countNodes(3008) { if(nodePtr != NULL) return 1 + 1; else return 0; } stack
12
12 Counting Nodes in a List int NumberList::countNodes(3000) { if(nodePtr != NULL) return 1 + 2; else return 0; } 534 Head Null 300030083016 stack finally returns 3 to numNodes
13
13 Displaying Nodes in Reverse Order int NumberList::displayBackwards() { return showReverse(head); } int NumberList::showReverse(ListNode* nodePtr) { if(nodePtr != NULL) { showReverse(nodePtr->next); cout value << “ “; } Must provide public member function. Recursive function must start at head, but head can’t be passed in from outside the class.
14
14 Counting Nodes in a List void NumberList::showReverse(ListNode* nodePtr) { if(nodePtr != NULL) { showReverse(nodePtr->next); cout value << “ “; } 534 Head Null 300030083016
15
15 Counting Nodes in a List void NumberList::showReverse(3000) { if(nodePtr != NULL) { showReverse(3008); cout value << “ “; } 534 Head Null 300030083016
16
16 Counting Nodes in a List void NumberList::showReverse(3000) { if(nodePtr != NULL) { showReverse(3008); cout value << “ “; } 534 Head Null 300030083016 void NumberList::showReverse(3008) { if(nodePtr != NULL) { showReverse(3016); cout value << “ “; }
17
17 Counting Nodes in a List void NumberList::showReverse(3000) { if(nodePtr != NULL) { showReverse(3008); cout value << “ “; } 534 Head Null 300030083016 void NumberList::showReverse(3008) { if(nodePtr != NULL) { showReverse(3016); cout value << “ “; } void NumberList::showReverse(3016) { if(nodePtr != NULL) { showReverse(NULL); cout value << “ “; }
18
18 Counting Nodes in a List void NumberList::showReverse(3000) { if(nodePtr != NULL) { showReverse(3008); cout value << “ “; } 534 Head Null 300030083016 void NumberList::showReverse(3008) { if(nodePtr != NULL) { showReverse(3016); cout value << “ “; } void NumberList::showReverse(3016) { if(nodePtr != NULL) { showReverse(NULL); cout value << “ “; } void NumberList::showReverse(NULL) { if(nodePtr != NULL) { showReverse(NULL); cout value << “ “; }
19
19 Counting Nodes in a List void NumberList::showReverse(3000) { if(nodePtr != NULL) { showReverse(3008); cout value << “ “; } 534 Head Null 300030083016 void NumberList::showReverse(3008) { if(nodePtr != NULL) { showReverse(3016); cout value << “ “; } void NumberList::showReverse(3016) { if(nodePtr != NULL) { showReverse(NULL); cout value << “ “; }
20
20 Counting Nodes in a List void NumberList::showReverse(3000) { if(nodePtr != NULL) { showReverse(3008); cout value << “ “; } 534 Head Null 300030083016 void NumberList::showReverse(3008) { if(nodePtr != NULL) { showReverse(3016); cout value << “ “; }
21
21 Counting Nodes in a List void NumberList::showReverse(3000) { if(nodePtr != NULL) { showReverse(3008); cout value << “ “; } 534 Head Null 300030083016
22
22 Binary Trees
23
23 Binary Tree –non linear linked structure –each node may point to 2 other nodes –every node has exactly 1 predecessor tree pointer
24
24 Binary Tree tree pointer p f a t z null
25
25 Binary Tree Terminology tree pointer p f a t z null Left Subtree Leaf Node Root Node
26
Binary Search Tree class interface #ifndef INTBINARYTREE_H #define INTBINARYTREE_H class IntBinaryTree { private: struct TreeNode {int value; TreeNode *left; TreeNode *right; }; TreeNode *root; void insert(TreeNode *&, TreeNode *&); void destroySubTree(TreeNode *); void deleteNode(int, TreeNode *&); void makeDeletion(TreeNode *&); void displayInOrder(TreeNode *); void displayPreOrder(TreeNode *); void displayPostOrder(TreeNode *);
27
Binary Search Tree class interface public: IntBinaryTree()// Constructor { root = NULL; } ~IntBinaryTree()// Destructor { destroySubTree(root); } void insertNode(int); bool searchNode(int); void remove(int); void showNodesInOrder() {displayInOrder(root); } void showNodesPreOrder() {displayPreOrder(root); } void showNodesPostOrder() {displayPostOrder(root); } }; #endif
28
Inorder Traversal of a Binary Search Tree void IntBinaryTree::displayInOrder(TreeNode *nodePtr) { if (nodePtr) { displayInOrder(nodePtr->left); cout value << endl; displayInOrder(nodePtr->right); }
29
Search for a specific value within a Binary Search Tree bool IntBinaryTree::searchNode(int num) {TreeNode *nodePtr = root; while (nodePtr) { if (nodePtr->value == num) return true; else if (num value) nodePtr = nodePtr->left; else nodePtr = nodePtr->right; } return false; }
30
Insert implementation //********************************************************** // insertNode creates a new node to hold num as its value, // and passes it to the insert function. //********************************************************** void IntBinaryTree::insertNode(int num) { TreeNode *newNode;// Pointer to a new node. // Create a new node and store num in it. newNode = new TreeNode; newNode->value = num; newNode->left = newNode->right = NULL; // Insert the node. insert(root, newNode); }
31
Insert implementation void IntBinaryTree::insert(TreeNode *&nodePtr, TreeNode *&newNode) { if (nodePtr == NULL) nodePtr = newNode; // Insert the node. else if (newNode->value value) insert(nodePtr->left, newNode); // Search the left branch else insert(nodePtr->right, newNode); // Search the right branch }
32
Delete implementation void IntBinaryTree::remove(int num) {deleteNode(num, root); } //******************************************** // deleteNode deletes the node whose value // member is the same as num. void IntBinaryTree::deleteNode(int num, TreeNode *&nodePtr) { if (num value) deleteNode(num, nodePtr->left); else if (num > nodePtr->value) deleteNode(num, nodePtr->right); else makeDeletion(nodePtr); }
33
Delete implementation void IntBinaryTree::makeDeletion(TreeNode *&nodePtr) { TreeNode *tempNodePtr; // Temporary pointer, used in reattaching the // left subtree. if (nodePtr == NULL) cout << "Cannot delete empty node.\n"; else if (nodePtr->right == NULL) { tempNodePtr = nodePtr; nodePtr = nodePtr->left; // Reattach the left child delete tempNodePtr; } else
34
Delete implementation if (nodePtr->left == NULL) { tempNodePtr = nodePtr; nodePtr = nodePtr->right;// Reattach the right child delete tempNodePtr; }
35
Delete implementation else // If the node has two children. { // Move one node the right. tempNodePtr = nodePtr->right; // Go to the end left node. while (tempNodePtr->left) tempNodePtr = tempNodePtr->left; // Reattach the left subtree. tempNodePtr->left = nodePtr->left; tempNodePtr = nodePtr; // Reattach the right subtree. nodePtr = nodePtr->right; delete tempNodePtr; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.