Lecture No.15 Data Structures Dr. Sohail Aslam

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Advertisements

Trees Types and Operations
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Computer Science C++ High School Level By Guillermo Moreno.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
BST Data Structure A BST node contains: A BST contains
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Sorted Array What is BigO for sorted list implemented as: ArrayList: – Search : – Insert(value) : – Remove(value) : LinkedList: – Search : – Insert(value)
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.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
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 Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University.
 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)
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
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.
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/14/20161 BST Operations Data Structures Ananda Gunawardena.
CSCS-200 Data Structure and Algorithms Lecture
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.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Binary Search Trees Chapter 7 Objectives
Lecture No.14 Data Structures Dr. Sohail Aslam
Lecture No.13 Data Structures Dr. Sohail Aslam
Binary search tree. Removing a node
UNIT III TREES.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Binary Search Trees.
Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals.
Data Structures Lecture 27 Sohail Aslam.
Chapter 21: Binary Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Find in a linked list? first last 7  4  3  8 NULL
Search Sorted Array: Binary Search Linked List: Linear Search
singleRightRotation 
Lecture No.16 Data Structures Dr. Sohail Aslam.
Trees CMSC 202, Version 5/02.
CMSC 202 Trees.
Operations on Binary Tree
2018, Fall Pusan National University Ki-Joune Li
Non-Linear Structures
Data Structures Lecture 28 Sohail Aslam.
Podcast Ch18a Title: Overview of Binary Search Trees
Chapter 20: Binary Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
Presentation transcript:

Lecture No.15 Data Structures Dr. Sohail Aslam

Level-order Traversal There is yet another way of traversing a binary tree that is not related to recursive traversal procedures discussed previously. In level-order traversal, we visit the nodes at each level before proceeding to the next level. At each level, we visit the nodes in a left-to-right order.

Level-order Traversal 14 4 9 7 3 5 15 18 16 20 17 End of lecture 14 Level-order: 14 4 15 3 9 18 7 16 20 5 17

Level-order Traversal How do we do level-order traversal? Surprisingly, if we use a queue instead of a stack, we can visit the nodes in level-order. Here is the code for level-order traversal:

Level-order Traversal  void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << " "; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl;

Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << " "; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; 

Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << " "; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; 

Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << " "; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; 

Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << " "; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; 

Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << " "; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; 

Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << " "; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; 

Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << " "; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; 

Level-order Traversal 14 4 15 3 9 18 7 16 20 5 17 Queue: 14 Output:

Level-order Traversal 14 4 15 3 9 18 7 16 20 5 17 Queue: 4 15 Output: 14

Level-order Traversal 14 4 15 3 9 18 7 16 20 5 17 Queue: 15 3 9 Output: 14 4

Level-order Traversal 14 4 15 3 9 18 7 16 20 5 17 Queue: 3 9 18 Output: 14 4 15

Level-order Traversal 14 4 15 3 9 18 7 16 20 5 17 Queue: 9 18 Output: 14 4 15 3

Level-order Traversal 14 4 15 3 9 18 7 16 20 5 17 Queue: 18 7 Output: 14 4 15 3 9

Level-order Traversal 14 4 15 3 9 18 7 16 20 5 17 Queue: 7 16 20 Output: 14 4 15 3 9 18

Level-order Traversal 14 4 15 3 9 18 7 16 20 5 17 Queue: 16 20 5 Output: 14 4 15 3 9 18 7

Level-order Traversal 14 4 15 3 9 18 7 16 20 5 17 Queue: 20 5 17 Output: 14 4 15 3 9 18 7 16

Level-order Traversal 14 4 15 3 9 18 7 16 20 5 17 Queue: 5 17 Output: 14 4 15 3 9 18 7 16 20

Level-order Traversal 14 4 15 3 9 18 7 16 20 5 17 Queue: 17 Output: 14 4 15 3 9 18 7 16 20 5

Level-order Traversal 14 4 15 3 9 18 7 16 20 5 17 Queue: Output: 14 4 15 3 9 18 7 16 20 5 17

Storing other Type of Data The examples of binary trees so far have been storing integer data in the tree node. This is surely not a requirement. Any type of data can be stored in a tree node. Here, for example, is the C++ code to build a tree with character strings.

Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = "babble", "fable", "jacket", "backup", "eagle","daily","gain","bandit","abandon", "abash","accuse","economy","adhere","advise","cease", "debunk","feeder","genius","fetch","chain", NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ ) insert(root, word[i] ); inorder( root ); cout << endl; } 

Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = "babble", "fable", "jacket", "backup", "eagle","daily","gain","bandit","abandon", "abash","accuse","economy","adhere","advise","cease", "debunk","feeder","genius","fetch","chain", NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ ) insert(root, word[i] ); inorder( root ); cout << endl; } 

Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = "babble", "fable", "jacket", "backup", "eagle","daily","gain","bandit","abandon", "abash","accuse","economy","adhere","advise","cease", "debunk","feeder","genius","fetch","chain", NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ ) insert(root, word[i] ); inorder( root ); cout << endl; } 

Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = "babble", "fable", "jacket", "backup", "eagle","daily","gain","bandit","abandon", "abash","accuse","economy","adhere","advise","cease", "debunk","feeder","genius","fetch","chain", NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ ) insert(root, word[i] ); inorder( root ); cout << endl; } 

Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = "babble", "fable", "jacket", "backup", "eagle","daily","gain","bandit","abandon", "abash","accuse","economy","adhere","advise","cease", "debunk","feeder","genius","fetch","chain", NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ ) insert(root, word[i] ); inorder( root ); cout << endl; } 

Binary Search Tree with Strings  void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }

Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); } 

Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); } 

Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); } 

Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); } 

Binary Search Tree with Strings  if( strcmp(info, p->getInfo()) == 0 ){ cout << "attempt to insert duplicate: " << *info << endl; delete node; } else if( strcmp(info, p->getInfo()) < 0 ) p->setLeft( node ); else p->setRight( node );

Binary Search Tree with Strings if( strcmp(info, p->getInfo()) == 0 ){ cout << "attempt to insert duplicate: " << *info << endl; delete node; } else if( strcmp(info, p->getInfo()) < 0 ) p->setLeft( node ); else p->setRight( node ); 

Binary Search Tree with Strings if( strcmp(info, p->getInfo()) == 0 ){ cout << "attempt to insert duplicate: " << *info << endl; delete node; } else if( strcmp(info, p->getInfo()) < 0 ) p->setLeft( node ); else p->setRight( node ); 

Binary Search Tree with Strings Output: abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket

Binary Search Tree with Strings Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket

Binary Search Tree with Strings Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. This should not come as a surprise if you consider how we built the BST. abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket

Binary Search Tree with Strings Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. This should not come as a surprise if you consider how we built the BST. For a given node, values less than the info in the node were all in the left subtree and values greater or equal were in the right. abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket

Binary Search Tree with Strings Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. This should not come as a surprise if you consider how we built the BST. For a given node, values less than the info in the node were all in the left subtree and values greater or equal were in the right. Inorder prints the left subtree, then the node finally the right subtree. abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket

Binary Search Tree with Strings Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. This should not come as a surprise if you consider how we built the BST. For a given node, values less than the info in the node were all in the left subtree and values greater or equal were in the right. Inorder prints the left subtree, then the node finally the right subtree. Building a BST and doing an inorder traversal leads to a sorting algorithm. abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket

Binary Search Tree with Strings Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. This should not come as a surprise if you consider how we built the BST. For a given node, values less than the info in the node were all in the left subtree and values greater or equal were in the right. Inorder prints the left subtree, then the node finally the right subtree. Building a BST and doing an inorder traversal leads to a sorting algorithm. abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket

Deleting a node in BST As is common with many data structures, the hardest operation is deletion. Once we have found the node to be deleted, we need to consider several possibilities. If the node is a leaf, it can be deleted immediately.

Deleting a node in BST If the node has one child, the node can be deleted after its parent adjusts a pointer to bypass the node and connect to inorder successor. 6 2 4 3 1 8

Deleting a node in BST The inorder traversal order has to be maintained after the delete. 6 2 4 3 1 8 6  2 8 1 4 3

Deleting a node in BST The inorder traversal order has to be maintained after the delete. 6 2 4 3 1 8 6 6   2 8 2 8 1 4 1 3 3

Deleting a node in BST The complicated case is when the node to be deleted has both left and right subtrees. The strategy is to replace the data of this node with the smallest data of the right subtree and recursively delete that node.

Deleting a node in BST Delete(2): locate inorder successor 6 2 8 1 5 3 Start lecture 16 3 4 Inorder successor

Deleting a node in BST Delete(2): locate inorder successor 6 Inorder successor will be the left-most node in the right subtree of 2. The inorder successor will not have a left child because if it did, that child would be the left-most node. 2 8 1 5 3 4 Inorder successor

Deleting a node in BST Delete(2): copy data from inorder successor  6 8 3 8 1 5 1 5 3 3 4 4

Deleting a node in BST Delete(2): remove the inorder successor   6 6 8 3 8 3 8 1 5 1 5 1 5 3 3 3 4 4 4

Deleting a node in BST Delete(2)   6 6 3 8 3 8 1 5 1 5 3 4 4 End of lecture 15 3 4 4