Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
What is a binary tree? Recall Unit 12 and 13, in which we have discussed about Binary Tree while explaining Heap Sort. A Binary Tree in which each element may has 0-child, 1-child or maximum of 2-children. A Binary Tree is defined as finite set of elements, called nodes, such that: Tree contains a distinguished node Root, called the root of Tree, and the remaining nodes of Tree form an ordered pair of disjoint binary trees Tree 1 and Tree 2. If Tree does contain a root, then the two trees Tree 1 and Tree 2 are called, respectively, the left sub tree and right sub tree of Root. 2 CSC 244 Concepts of Algorithms
What is a binary tree? Property : each node can have up to two successor nodes. 3 CSC 244 Concepts of Algorithms
Some terminology The successor nodes of a node are called its children The predecessor node of a node is called its parent The "beginning" node is called the root (has no parent) A node without children is called a leaf 4 CSC 244 Concepts of Algorithms
Traversing of Binary Tree : A traversal of a tree is a systematic way of accessing or visiting all the node. There are three standard ways of traversing a binary tree with root. These are : Preorder (N L R): Process the node/root. (A B D F I C G H J L K) Traverse the Left sub tree. Traverse the Right sub tree. Inorder (L N R): ( D B I F A G C L J H K ) Traverse the Left sub tree. Process the node/root. Traverse the Right sub tree. Postorder (L R N): ( D I F B G L J K H C A ) Traverse the Left sub tree. Traverse the Right sub tree. Process the node/root. Descending order (R N L): ( K H J L C G A F I B D ) Traverse the Right sub tree. Process the node/root. Traverse the Left sub tree. Traversing Binary Trees 5 CSC 244 Concepts of Algorithms
Tree Traversals: another example 6 CSC 244 Concepts of Algorithms
Example: Expression Trees Leaves are operands (constants or variables) The other nodes (internal nodes) contain operators 7 CSC 244 Concepts of Algorithms
Example: Expression Trees 8 CSC 244 Concepts of Algorithms
Tree node structure struct TreeNode { int Info; struct TreeNode * left; struct TreeNode * right; }; 9 CSC 244 Concepts of Algorithms
Binary Tree Representation 10 CSC 244 Concepts of Algorithms
Pre-Order Traversal Algorithm 11 CSC 244 Concepts of Algorithms
In-Order Traversal Algorithm 12 CSC 244 Concepts of Algorithms
Post-Order Traversal Algorithm 13 CSC 244 Concepts of Algorithms
Search Binary Search Trees (BSTs) Property 1: Binary Search Tree The value stored at a node is greater than the value stored at its left child and less than the value stored at its right child 14 CSC 244 Concepts of Algorithms
Property 2: In a BST, the value stored at the root of a sub-tree is greater than any value in its left sub-tree and less than any value in its right sub-tree! Binary Search Trees (BSTs) 15 CSC 244 Concepts of Algorithms
Where is the smallest element? Ans: leftmost element A Where is the largest element? Ans: rightmost element J Binary Search Trees (BSTs) 16 CSC 244 Concepts of Algorithms
How to search a binary search tree? (1) Start at the root (2) Compare the value of the item you are searching for with the value stored at the root (3) If the values are equal, then item found; otherwise, if it is a leaf node, then not found 17 CSC 244 Concepts of Algorithms
How to search a binary search tree? (4) If it is less than the value stored at the root, then search the left subtree (5) If it is greater than the value stored at the root, then search the right subtree (6) Repeat steps 2-6 for the root of the subtree chosen in the previous step 4 or 5 18 CSC 244 Concepts of Algorithms
Some terminology (cont’d) Nodes are organized in levels (indexed from 0). Level (or depth) of a node: number of edges in the path from the root to that node. Height of a tree h: #levels = L (Warning: some books define h as #levels-1). 19 CSC 244 Concepts of Algorithms
Function Retrieve Item 20 CSC 244 Concepts of Algorithms
Binary Search Tree The values of at N (node) is greater than every value in the left sub tree of N and is less than every value in the right sub tree of N. Binary Search Tree using these values: (50, 30, 55, 25, 10, 35, 31, 20, 53, 60, 62) CSC 244 Concepts of Algorithms
Another Example Binary Search Tree Following figure shows a binary search tree. Notice that this tree is obtained by inserting the values 13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18 in that order, starting from an empty tree. 22 CSC 244 Concepts of Algorithms
How to delete a Node from Binary Search Tree Next Slide……… 23 CSC 244 Concepts of Algorithms
24 CSC 244 Concepts of Algorithms
C-Language Code (BST) // Program by Shahid Lone // for Binary Search Tree (BST) #include struct NODE { intinfo; struct NODE *Left; struct NODE *Right; }; // initially Root is NULL struct NODE *Root = NULL; void AttachNode( struct NODE *pRoot, struct NODE *pNew ) { if( Root == NULL ) // to attach first node with tree { Root = pNew; return; } // attaches node on first root if (pNew->info info ) { // traverse to left sub-tree and find null at left if( pRoot->Left != NULL) AttachNode( pRoot->Left, pNew ); else pRoot->Left = pNew; // attaches node on left } else { // traverse to right sub-tree and find null at right if( pRoot->Right != NULL) AttachNode( pRoot->Right, pNew ); else pRoot->Right = pNew; // attaches node on right } 25 CSC 244 Concepts of Algorithms
C-Language Code (BST) Cont….. void Insert(int x) { struct NODE *NewNode= new NODE; NewNode->Left = NULL; NewNode->Right= NULL; NewNode->info = x; AttachNode( Root, NewNode ); } void Pre_Order( NODE *pRoot) { if (pRoot) { cout info << “\t” ; Pre_Order(pRoot->Left); Pre_Order(pRoot->Right); } 26 CSC 244 Concepts of Algorithms
C-Language Code (BST) Cont….. Void Post_Order(struct NODE *pRoot) { if (pRoot) { Post_Order(pRoot->Left); Post_Order(pRoot->Right); cout info << “\t” ; } void In_Order( NODE *pRoot) { if (pRoot) { In_Order(pRoot->Left); cout info << “\t” ; In_Order(pRoot->Right); } 27 CSC 244 Concepts of Algorithms
C-Language Code (BST) Cont….. Void DisplayDescending( NODE *pRoot) { if( pRoot ) { DisplayDescending(pRoot->Right); cout info << “\t” ; DisplayDescending(pRoot->Left); } void DeleteTree( struct NODE *pRoot) // This function deletes all nodes in the tree // and make Tree empty { if( pRoot ) { if(pRoot->Right) { DeleteTree(pRoot->Right); } if(pRoot->Left) { DeleteTree(pRoot->Left); } delete ( pRoot ); } 28 CSC 244 Concepts of Algorithms
C-Language Code (BST) Cont….. int main( void ) { int ch, item; while( 1 ) { cout<<"\n\n\n Binary Search Tree Functions\n\n"; cout<<"\n1. Insert a New Node"; cout<<"\n2. Remove Existing Node"; cout<<"\n3. In-Order Traverse (Ascending Order)"; cout<<"\n4. Pre-Order Traverse "; cout<<"\n5. Post-Order Traverse "; cout<<"\n6. Display in Descending Order (Reverse)"; cout<<"\n7. Exit"; cout<<"\n Enter you choice: "; cin >> ch; 29 CSC 244 Concepts of Algorithms
C-Language Code (BST) Cont….. switch(ch) { case 1: cout > item; Insert(item); break; case 2: // Remove(); // This function is not defined. break; // Students shall write this function as home work. case 3: cout << "\n\n\n In-Order Traverse \n"; In_Order(Root); // produce Ascending Order sorted list cout << "\n\n"; break; case 4: cout << "\n\n\n Pre-Order Traverse \n"; Pre_Order(Root); cout << "\n\n"; break; 30 CSC 244 Concepts of Algorithms
C-Language Code (BST) Cont….. case 5: cout << " \n\n\n Post-Order Traverse \n"; Post_Order(Root); cout <<"\n\n"; break; case 6: cout << "\n\n\nDESCENDING ORDER (Reverse )\n"; DisplayDescending(Root); cout <<"\n\n"; break; case 7: DeleteTree(Root); exit(0); // it stops the execution of the program default: cout << "\n\nInvalid Input"; } // end of switch } // end of while loop } // end of main( ) function 31 CSC 244 Concepts of Algorithms
Thank You.