Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.

Similar presentations


Presentation on theme: "Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only."— Presentation transcript:

1 Binary Search Tree

2 Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only one predecessor (parent), except the root node, which has none. ΩΩΩΩΩΩ ΩΩΩΩΩΩ Tree Data Structure Ω

3 Terminology Node  Contains data and pointers to other nodes (called child nodes) Parent Node  Each node has at most one node.  Root node has no parent Leaf  Node with no children ΩΩΩΩΩΩΩ ΩΩΩΩΩΩ ΩΩ Root Node Leaf Node

4 Terminology Height of Tree  Longest path to a leaf Subtree  A node and all its children Edge (or link)  A path from a node to its child Null pointer  Points to no node (in C++, 0) ΩΩΩΩΩΩΩ ΩΩΩΩΩΩ ΩΩ Root Node (Ω)(Ω)

5 Binary Tree  is a nonlinear linked structure in which each node may point to two other nodes, and every node but the root node has a single predecessor. Root Node Left SubtreeRight Subtree

6 Binary Search Tree  Binary tree in which a data item is inserted in such a way that  all items less than the item in a node are in the left subtree  All items greater than the item in a node are in the right subtree 30 2550 15 60 55205 Root Node Left SubtreeRight Subtree

7 Traversals Any process for visiting the nodes in some order is called a traversal.  E.g., printing, searching.

8 Traversals Preorder traversal:  Visit the root, then visit its left and right subtrees (children) Postorder traversal:  Visit the left and right subtrees (children), then visit the root Inorder traversal:  Visit the left subtree, then the root, then the right subtree.

9 Preorder Traversal 1 23

10 Postorder Traversal 1 2 3

11 Inorder Traversal 1 2 3

12 Representing Node struct TreeNode { elemType value; // value in node TreeNode *left; // pointer to left child TreeNode *right; // pointer to right child }; TreeNode *root; // pointer to root node Note the recursive form of the declaration. 30

13 BST Operations (Public) typedef int elemType; class BinTree { public: BinTree(); void insertNode(elemType item); bool search(elemType item); void remove(elemType item); void displayInOrder(); void displayPreOrder(); void displayPostOrder(); private:... Private declarations };

14 Private Methods class BinTree { public:... Public methods private: struct TreeNode { elemType value; // value in node TreeNode *left; // pointer to left child node TreeNode *right; // pointer to right child node }; TreeNode *root; // pointer to the root node void insert(TreeNode *&nodePtr, TreeNode *&newNode); void removeNode(elemType item, TreeNode *&nodePtr); void makeRemoval(TreeNode *&nodePtr); void displayInOrder(TreeNode *nodePtr); void displayPreOrder(TreeNode *nodePtr); void displayPostOrder(TreeNode *nodePtr); };

15 BST Constructor #define NULL 0 BinTree::BinTree() { root = NULL; // NULL pointer }

16 BST InsertNode Operation To DisplayInOrder

17 BST InsertNode Operation void BinTree::insertNode(elemType item) { TreeNode *newNode; // pointer to new node // Create new node and store item newNode = new TreeNode; newNode->value = item; newNode->left = newNode->right = NULL; // Insert the node insert(root, newNode); }

18 BST Insert Method (Private) void BinTree::insert(TreeNode *&nodePtr, TreeNode *&newNode) { if (nodePtr == NULL) // Insert the node nodePtr = newNode; else if (newNode->value value) // Attach on left subtree insert(nodePtr->left, newNode); else // Attach on right subtree insert(nodePtr->right, newNode); } TreeNode *&NodePtr — nodePtr is reference to actual argument

19 Order of Insertion Affects Tree Height 37, 24, 32, 42, 40, 42, 7, 120, 2 120, 42, 7, 42, 32, 2, 37, 40, 24

20 Which Structure Allows More Efficient Search? 37 24 42 7 3240 120 2 2 7 24 32 37 40 42 120 42 Key Value:

21 BST DisplayInOrder Operation void BinTree::displayInOrder() { displayInOrder(root); }

22 BST DisplayInOrder Method (Private) void BinTree::displayInOrder( TreeNode *nodePtr) { if (nodePtr) { displayInOrder(nodePtr->left); cout value << endl; displayInOrder(nodePtr->right); } To BST diagram

23 BST Remove Operation When removing a node from a tree, there are 4 cases to consider 1. The node is a leaf 2. The node has no right child 3. The node has no left child 4. The node has both children

24 BST Remove Operation (Node is a leaf) 37 24 42 7 55 root Ω Ω Ω Ω Ω Ω This node to be deleted 37 42 24 55 Ω Ω Ω Ω 7 Ω Ω BeforeAfter

25 BST Remove Operation (Node has no right child) 37 24 42 7 55 root Ω Ω Ω Ω Ω Ω This node to be deleted 37 24 42 7 55 Ω Ω Ω Ω Ω Ω Before After

26 BST Remove Operation (Node has no left child) 37 24 42 7 55 root Ω Ω Ω Ω Ω Ω This node to be deleted 37 42 24 7 55 Ω ΩΩ Ω Ω Ω After Before

27 BST Remove Operation (Node has both children) This node to be deleted 37 2442 7 55 root Ω Ω Ω Ω Ω 40 Ω Ω 37 2442 7 55 Ω ΩΩ Ω Ω 40 Ω Ω Before 37 24 55 7 Ω Ω Ω 40 Ω Ω After

28 BST remove Operation void BinTree::remove(elemType item) { removeNode(item, root); }

29 BST removeNode Method (Private) void BinTree::removeNode(elemType item, TreeNode *&nodePtr) { if (item == nodePtr->value) makeRemoval(nodePtr); else if (item value) removeNode(item, nodePtr->left); else // item > nodePtr->value removeNode(item, nodePtr->right); }

30 BST makeRemoval Method (Private) void BinTree::makeRemoval(TreeNode *&nodePtr) { TreeNode *tempPtr; if (nodePtr == NULL) cout << "Cannot remove empty node.\n"; else if (nodePtr->right == NULL) { // no right child tempPtr = nodePtr; nodePtr = nodePtr->left; // reattach left child delete tempPtr; } else if (nodePtr->left == NULL) { // no left child tempPtr = nodePtr; nodePtr = nodePtr->right; // reattach right child delete tempPtr; }... This is where *&nodePtr becomes important.

31 BST makeRemoval Method (cont.) else { // node has right and left children // check the right subtree tempPtr = nodePtr->right; // got to end of left subtree while (tempPtr->left) tempPtr = tempPtr->left; // reattach left subtree tempPtr->left = nodePtr->left; tempPtr = nodePtr; // reattach right subtree nodePtr = nodePtr->right; delete tempPtr; }


Download ppt "Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only."

Similar presentations


Ads by Google