Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.

Slides:



Advertisements
Similar presentations
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Trees, Binary Trees, and Binary Search Trees COMP171.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Trees, Binary Trees, and Binary Search Trees COMP171.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
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.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Binary Search Trees … From
Trees Namiq Sultan. Trees Trees are very flexible, versatile and powerful non-liner data structure that can be used to represent data items possessing.
CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM.
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Lecture 9 Binary Trees Trees General Definition Terminology
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Binary Search Trees Chapter 7 Objectives
Trees Chapter 15.
Trees ---- Soujanya.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Lecture 22 Binary Search Trees Chapter 10 of textbook
Section 8.1 Trees.
Data Structures & Algorithm Design
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 7 TREES.
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Introduction to Trees IT12112 Lecture 05.
Chapter 21: Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Trees.
Binary Search Tree (BST)
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

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.