Operations on Binary Tree

Slides:



Advertisements
Similar presentations
Section 5 Lists again. Double linked lists – insertion, deletion. Trees.
Advertisements

DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
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.
Binary Tree A data structure whose nodes contain two pointer fields. One or both pointers can have the value NULL. Each node in a binary tree can have.
trees1 Binary Trees trees2 Basic terminology nodesFinite set of nodes (may be empty -- 0 nodes), which contain data rootFirst node in tree is called.
Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
INTRODUCTION TO AVL TREES P. 839 – 854. INTRO  Review of Binary Trees: –Binary Trees are useful for quick retrieval of items stored in the tree –order.
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort.
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.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
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.
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.
Introduction to C Programming CE Lecture 24 Insertion and Deletion with Binary Search Trees.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 6. Dictionaries(3): Binary Search Trees.
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.
1 Lecture 14: Trees & Insertion Sort CompSci 105 SS 2006 Principles of Computer Science.
1 Nell Dale Chapter 8 Binary Search Trees Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Binary Search Trees. 2 Overview Recursive linked list ops Binary Trees.
CSCE 210 Data Structures and Algorithms
Lecture No.14 Data Structures Dr. Sohail Aslam
Tree Insert Animation.
Lecture No.13 Data Structures Dr. Sohail Aslam
Data Structures Lecture 22 Sohail Aslam.
Binary search tree. Removing a node
Lecture No.15 Data Structures Dr. Sohail Aslam
Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas.
Trees Lecture 12 CS2110 – Fall 2017.
CSCS-200 Data Structures and Algorithms
Binary Search Trees -Monty Python and The Holy Grail
Topic 18 Binary Search Trees
Chapter 20: Binary Trees.
Topic 19 Binary Search Trees
Trees Lecture 12 CS2110 – Spring 2018.
Chapter 21: Binary Trees.
Programming Assignment #4 Binary Trees in C++
Binary Search Tree AVL Tree
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Search Sorted Array: Binary Search Linked List: Linear Search
singleRightRotation 
Lecture No.16 Data Structures Dr. Sohail Aslam.
Lecture No.11 Data Structures Dr. Sohail Aslam
Tree Data Structures There are a number of applications where linear data structures are not appropriate. Consider a genealogy tree of a family. Mohammad.
Iterators Professor Hugh C. Lauer CS-2303, System Programming Concepts
Non-Linear Structures
Pointers & Dynamic Data Structures
Data Structures Lecture 28 Sohail Aslam.
BINARY TREE CSC248 – Data Structure.
Search Sorted Array: Binary Search Linked List: Linear Search
Trees A Quick Introduction to Graphs Definition of Trees Rooted Trees
Presentation transcript:

Operations on Binary Tree There are a number of operations that can be defined for a binary tree. If p is pointing to a node in an existing tree then left(p) returns pointer to the left subtree right(p) returns pointer to right subtree parent(p) returns the father of p brother(p) returns brother of p. info(p) returns content of the node. End of lecture 11

Operations on Binary Tree In order to construct a binary tree, the following can be useful: setLeft(p,x) creates the left child node of p. The child node contains the info ‘x’. setRight(p,x) creates the right child node of p. The child node contains the info ‘x’.

Applications of Binary Trees A binary tree is a useful data structure when two-way decisions must be made at each point in a process. For example, suppose we wanted to find all duplicates in a list of numbers: 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

Applications of Binary Trees One way of finding duplicates is to compare each number with all those that precede it. 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

Searching for Duplicates If the list of numbers is large and is growing, this procedure involves a large number of comparisons. A linked list could handle the growth but the comparisons would still be large. The number of comparisons can be drastically reduced by using a binary tree. The tree grows dynamically like the linked list.

Searching for Duplicates The binary tree is built in a special way. The first number in the list is placed in a node that is designated as the root of a binary tree. Initially, both left and right subtrees of the root are empty. We take the next number and compare it with the number placed in the root. If it is the same then we have a duplicate.

Searching for Duplicates Otherwise, we create a new tree node and put the new number in it. The new node is made the left child of the root node if the second number is less than the one in the root. The new node is made the right child if the number is greater than the one in the root.

Searching for Duplicates 14 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

Searching for Duplicates 15 14 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

Searching for Duplicates 14 15 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

Searching for Duplicates 14 4 15 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

Searching for Duplicates 14 4 15 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

Searching for Duplicates 14 9 4 15 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

Searching for Duplicates 14 4 15 9 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

Searching for Duplicates 7 14 4 15 9 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

Searching for Duplicates 14 4 15 9 7 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

Searching for Duplicates 18 14 4 15 9 7 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

Searching for Duplicates 14 4 15 9 18 7 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

Searching for Duplicates 3 14 4 15 9 18 7 3, 5, 16, 4, 20, 17, 9, 14, 5

Searching for Duplicates 14 4 15 3 9 18 7 3, 5, 16, 4, 20, 17, 9, 14, 5

Searching for Duplicates 5 14 4 15 3 9 18 7 5, 16, 4, 20, 17, 9, 14, 5

Searching for Duplicates 14 4 15 3 9 18 7 5 5, 16, 4, 20, 17, 9, 14, 5

Searching for Duplicates 16 14 4 15 3 9 18 7 5 16, 4, 20, 17, 9, 14, 5

Searching for Duplicates 14 4 15 3 9 18 7 16 5 16, 4, 20, 17, 9, 14, 5

Searching for Duplicates 4 14 4 15 3 9 18 7 16 5 4, 20, 17, 9, 14, 5

Searching for Duplicates 20 14 4 15 3 9 18 7 16 5 20, 17, 9, 14, 5

Searching for Duplicates 14 4 15 3 9 18 7 16 20 5 20, 17, 9, 14, 5

Searching for Duplicates 17 14 4 15 3 9 18 7 16 20 5 17, 9, 14, 5

Searching for Duplicates 14 4 15 3 9 18 7 16 20 5 17 17, 9, 14, 5

Searching for Duplicates 14 4 15 3 9 18 7 16 20 5 17 9, 14, 5

C++ Implementation #include <stdlib.h> template <class Object> class TreeNode { public: // constructors TreeNode() { this->object = NULL; this->left = this->right = NULL; }; TreeNode( Object* object ) this->object = object;

C++ Implementation Object* getInfo() { return this->object; }; void setInfo(Object* object) this->object = object; TreeNode* getLeft() return left; void setLeft(TreeNode *left) this->left = left;

C++ Implementation TreeNode *getRight() { return right; }; void setRight(TreeNode *right) this->right = right; int isLeaf( ) if( this->left == NULL && this->right == NULL ) return 1; return 0;

C++ Implementation private: Object* object; TreeNode* left; TreeNode* right; }; // end class TreeNode

C++ Implementation #include <iostream> #include <stdlib.h> #include "TreeNode.cpp" int main(int argc, char *argv[]) { int x[] = { 14, 15, 4, 9, 7, 18, 3, 5, 16,4, 20, 17, 9, 14,5, -1}; TreeNode<int>* root = new TreeNode<int>(); root->setInfo( &x[0] ); for(int i=1; x[i] > 0; i++ ) insert(root, &x[i] ); }

C++ Implementation void insert(TreeNode<int>* root, int* info) { TreeNode<int>* node = new TreeNode<int>(info); TreeNode<int> *p, *q; p = q = root; while( *info != *(p->getInfo()) && q != NULL ) p = q; if( *info < *(p->getInfo()) ) q = p->getLeft(); else q = p->getRight(); }

C++ Implementation if( *info == *(p->getInfo()) ){ cout << "attempt to insert duplicate: " << *info << endl; delete node; } else if( *info < *(p->getInfo()) ) p->setLeft( node ); else p->setRight( node ); } // end of insert

Trace of insert p 17 q 14 4 15 3 9 18 7 16 20 5 17, 9, 14, 5

Trace of insert p 17 14 4 15 q 3 9 18 7 16 20 5 17, 9, 14, 5

Trace of insert 17 14 p 4 15 q 3 9 18 7 16 20 5 17, 9, 14, 5

Trace of insert 17 14 p 4 15 3 9 q 18 7 16 20 5 17, 9, 14, 5

Trace of insert 17 14 4 15 p 3 9 18 q 7 16 20 5 17, 9, 14, 5

Trace of insert 17 14 4 15 p 3 9 18 7 q 16 20 5 17, 9, 14, 5

Trace of insert 17 14 4 15 3 9 18 p 7 16 20 q 5 17, 9, 14, 5

Trace of insert 17 14 4 15 3 9 18 p 7 16 20 q 5 17, 9, 14, 5

p->setRight( node ); Trace of insert 14 4 15 3 9 18 7 p 16 20 End of lecture 12 5 17 node 17, 9, 14, 5 p->setRight( node );