CS 1031 Trees A Quick Introduction to Graphs Definition of Trees Rooted Trees Binary Trees Binary Search Trees.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II Chapter 10 Trees I. 2 Topics Terminology.
Advertisements

Chapter 7. Binary Search Trees
CS 1031 Graphs Definition of Graphs and Related Concepts Representation of Graphs The Graph Class Graph Traversal Graph Applications.
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.
Trees Types and Operations
Computer Science C++ High School Level By Guillermo Moreno.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Trees-part1. Objectives Understand tree terminology Understand and implement tree traversals Define the binary search tree property Implement binary search.
1 Trees What is a Tree? Tree terminology Why trees? General Trees and their implementation N-ary Trees N-ary Trees implementation Implementing trees Binary.
Important Problem Types and Fundamental Data Structures
Binary Trees Chapter 6.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
COSC2007 Data Structures II
Tree.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical.
CS 1031 Tree Traversal Techniques; Heaps Tree Traversal Concept Tree Traversal Techniques: Preorder, Inorder, Postorder Full Trees Almost Complete Trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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.
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 ),
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
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.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Tree ADTs Tree concepts. Applications of Trees. A Tree ADT – requirements, contract. Linked implementation of Trees. Binary Tree ADTs. Binary Search.
Discrete Structures Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Trees, Binary Trees, and Binary Search Trees COMP171.
1 Trees, Trees, and More Trees. 2 By looking at forests of terms, awesome animations, and complete examples, we hope to get at the root of trees. Hopefully,
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
1 Trees A Quick Introduction to Graphs Definition of Trees Rooted Trees Binary Trees Binary Search Trees.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
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.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
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.
Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 Lecture 14: Trees & Insertion Sort CompSci 105 SS 2006 Principles of Computer Science.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Discrete Structures Li Tak Sing( 李德成 ) Lectures
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Graphs Definition of Graphs and Related Concepts
Definition of Graphs A graph is a finite set of nodes with edges between nodes Formally, a graph G is a structure (V,E) consisting of a finite set V called.
CMSC 341 Introduction to Trees.
Chapter 20: Binary Trees.
Introduction to Trees IT12112 Lecture 05.
Chapter 21: Binary Trees.
Trees.
Graphs Definition of Graphs and Related Concepts
COMPS263F Unit 2 Discrete Structures Li Tak Sing( 李德成 ) Room A
Binary Trees, Binary Search Trees
Non-Linear Structures
Tree.
Search Sorted Array: Binary Search Linked List: Linear Search
Trees A Quick Introduction to Graphs Definition of Trees Rooted Trees
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:

CS 1031 Trees A Quick Introduction to Graphs Definition of Trees Rooted Trees Binary Trees Binary Search Trees

CS 1032 Introduction to Graphs A graph is a finite set of nodes with edges between nodes Formally, a graph G is a structure (V,E) consisting of –a finite set V called the set of nodes, and –a set E that is a subset of VxV. That is, E is a set of pairs of the form (x,y) where x and y are nodes in V

CS 1033 Examples of Graphs V={1,2,3,4,5} E={(1,2), (2,3), (2,4), (4,2), (3,3), (5,4)} When (x,y) is an edge, we say that x is adjacent to y. 1 is adjacent to 2. 2 is not adjacent to 1. 4 is not adjacent to 3.

CS 1034 A “Real-life” Example of a Graph V=set of 6 people: John, Mary, Joe, Helen, Tom, and Paul, of ages 12, 15, 12, 15, 13, and 13, respectively. E ={(x,y) | if x is younger than y} John Joe MaryHelen TomPaul

CS 1035 Intuition Behind Graphs The nodes represent entities (such as people, cities, computers, words, etc.) Edges (x,y) represent relationships between entities x and y, such as: –“x loves y” –“x hates y” –“x is as smart as y” –“x is a sibling of y” –“x is bigger than y” –‘x is faster than y”, …

CS 1036 Directed vs. Undirected Graphs If the directions of the edges matter, then we show the edge directions, and the graph is called a directed graph (or a digraph) The previous two examples are digraphs If the relationships represented by the edges are symmetric (such as (x,y) is edge if and only if x is a sibling of y), then we don’t show the directions of the edges, and the graph is called an undirected graph.

CS 1037 Examples of Undirected Graphs V=set of 6 people: John, Mary, Joe, Helen, Tom, and Paul, where the first 4 are siblings, and the last two are siblings E ={(x,y) | x and y are siblings} John Joe MaryHelen TomPaul

CS 1038 Definition of Some Graph Related Concepts (Paths) A path in a graph G is a sequence of nodes x 1, x 2, …,x k, such that there is an edge from each node the next one in the sequence For example, in the first example graph, the sequence 4, 1, 2, 3 is a path, but the sequence 1, 4, 5 is not a path because (1,4) is not an edge In the “sibling-of” graph, the sequence John, Mary, Joe, Helen is a path, but the sequence Helen, Tom, Paul is not a path

CS 1039 Definition of Some Graph Related Concepts (Cycles) A cycle in a graph G is a path where the last node is the same as the first node. In the “sibling-of” graph, the sequence John, Mary, Joe, Helen, John is a cycle, but the sequence Helen, Tom, Paul, Helen is not a cycle

CS Graph Connectivity An undirected graph is said to be connected if there is a path between every pair of nodes. Otherwise, the graph is disconnected Informally, an undirected graph is connected if it hangs in one piece Disconnected Connected

CS Graph Cyclicity An undirected graph is cyclic if it has at least one cycle. Otherwise, it is acyclic Disconnected and acyclic Connected and acyclic Disconnected and cyclic Connected and cyclic

CS Trees A tree is a connected acyclic undirected graph. The following are three trees:

CS Rooted Trees A rooted tree is a tree where one of the nodes is designated as the root node. (Only one root in a tree) A rooted tree has a hierarchical structure: the root on top, followed by the nodes adjacent to it right below, followed by the nodes adjacent to those next, and so on.

CS Example of a Rooted Tree Unrooted tree Tree rooted with root 1

CS Tree-Related Concepts The nodes adjacent to x and below x are called the children of x, and x is called their parents A node that has no children is called a leaf The descendents of a node are: itself, its children, their children, all the way down The ancestors of a node are: itself, its parent, its grandparent, all the way to the root

CS Tree-Related Concepts (Contd.) The depth of a node is the number of edges from the root to that node. The depth (or height) of a rooted tree is the depth of the lowest leaf Depth of node 10: 3 Depth of this tree:

CS Binary Trees A tree is a binary tree if every node has at most two children Nonbinary treeBinary tree

CS Binary-Tree Related Definitions The children of any node in a binary tree are ordered into a left child and a right child A node can have a left and a right child, a left child only, a right child only, or no children The tree made up of a left child (of a node x) and all its descendents is called the left subtree of x Right subtrees are defined similarly

CS Graphical View Binary-tree Nodes data leftright In practice, a TreeNode will be shown as a circle where the data is put inside, and the node label (if any) is put outside. Graphically, a TreeNode is: 5.82 datalabel A binary-tree node consists of 3 parts: -Data -Pointer to left child -Pointer to right child

CS A Binary-tree Node Class class TreeNode { public: typedef int datatype; TreeNode(datatype x=0, TreeNode *left=NULL, TreeNode *right=NULL){ data=x; this->left=left; this->right=right; }; datatype getData( ) {return data;}; TreeNode *getLeft( ){return left;}; TreeNode *getRight( ){return right;}; void setData(datatype x) {data=x;}; void setLeft(TreeNode *ptr) {left=ptr;}; void setRight(TreeNode *ptr) {right=ptr;}; private: datatype data; // different data type for other apps TreeNode *left; // the pointer to left child TreeNode *right; // the pointer to right child };

CS Binary Tree Class class Tree { public: typedef int datatype; Tree(TreeNode *rootPtr=NULL){this->rootPtr=rootPtr;}; TreeNode *search(datatype x); bool insert(datatype x); TreeNode * remove(datatype x); TreeNode *getRoot(){return rootPtr;}; Tree *getLeftSubtree(); Tree *getRightSubtree(); bool isEmpty(){return rootPtr == NULL;}; private: TreeNode *rootPtr; };

CS Binary Search Trees A binary search tree (BST) is a binary tree where –Every node holds a data value (called key) –For any node x, all the keys in the left subtree of x are ≤ the key of x –For any node x, all the keys in the right subtree of x are > the key of x

CS Example of a BST

CS Searching in a BST To search for a number b: 1.Compare b with the root; –If b=root, return –If b<root, go left –If b>root, go right 2.Repeat step 1, comparing b with the new node we are at. 3.Repeat until either the node is found or we reach a non-existing node Try it with b=12, and also with b=

CS Code for Search in BST // returns a pointer to the TreeNode that contains x, // if one is found. Otherwise, it returns NULL TreeNode * Tree::search(datatype x){ if (isEmpty()){return NULL;} TreeNode *p=rootPtr; while (p != NULL){ datatype a = p->getData(); if (a == x) return p; else if (x getLeft(); else p=p->getRight(); } return NULL; };

CS Insertion into a BST Insert(datatype b, Tree T): 1.Search for the position of b as if it were in the tree. The position is the left or right child of some node x. 2.Create a new node, and assign its address to the appropriate pointer field in x 3.Assign b to the data field of the new node

CS Illustration of Insert Before inserting After inserting 25

CS Code for Insert in BST bool Tree::insert(datatype x){ if (isEmpty()) {rootPtr = new TreeNode(x);return true; } TreeNode *p=rootPtr; while (p != NULL){ datatype a = p->getData(); if (a == x) return false; // data is already there else if (x<a){ if (p->getLeft() == NULL){ // place to insert TreeNode *newNodePtr= new TreeNode(x); p->setLeft(newNodePtr); return true;} elsep=p->getLeft(); }else { // a>a if (p->getRight() == NULL){ // place to insert TreeNode *newNodePtr= new TreeNode(x); p->setRight(newNodePtr); return true;} elsep=p->getRight();} } };

CS Deletion from a BST Illustration in class

CS Deletion from a BST (pseudocode) Delete(datatype b, Tree T) 1.Search for b in tree T. If not found, return. 2.Call x the first node found to contain b 3.If x is a leaf, remove x and set the appropriate pointer in the parent of x to NULL 4.If x has only one child y, remove x, and the parent of x become a direct parent of y (More on the next slide)

CS Deletion (contd.) 5. If x has two children, go to the left subtree, and find there in largest node, and call it y. The node y can be found by tracing the rightmost path until the end. Note that y is either a leaf or has no right child 6. Copy the data field of y onto the data field of x 7. Now delete node y in a manner similar to step 4.

CS Code for Delete in BST(4 slides) // finds x in the tree, removes it, and returns a pointer to the containing // TreeNode. If x is not found, the function returns NULL. TreeNode * Tree::remove(datatype x){ if (isEmpty()) return NULL; TreeNode *p=rootPtr; TreeNode *parent = NULL; // parent of p char whatChild; // 'L' if p is a left child, 'R' O.W. while (p != NULL){ datatype a = p->getData(); if (a == x) break;// x found else if(x getLeft();} else {parent = p; whatChild = 'R'; p=p->getRight();} } if (p==NULL) return NULL; // x was not found

CS // Handle the case where p is a leaf. // Turn the appropriate pointer in its parent to NULL if (p->getLeft() == NULL && p->getRight() == NULL){ if (parent != NULL) // x is not at the root if (whatChild == 'L') parent->setLeft(NULL); else parent->setRight(NULL); else // x is at the root rootPtr=NULL; return p; }

CS else if (p->getLeft() == NULL){ // p has only one a child -- a right child. Let the parent of p // become an immediate parent of the right child of p. if (parent != NULL) // p is not the root if (whatChild == 'L') parent->setLeft(p->getRight()); else parent->setRight(p->getRight()); else rootPtr=p->getRight(); // p is the root return p; } else if (p->getRight() == NULL){ // p has only one a child -- a left child. Let the parent of p // become an immediate parent of the left child of p. if (parent != NULL) // p is not the root if (whatChild == 'L')parent->setLeft(p->getLeft()); elseparent->setRight(p->getLeft()); else rootPtr=p->getLeft(); // p is the root return p; }

CS else { // p has two children TreeNode *returnNode= new TreeNode(*p); // replicates p TreeNode * leftChild = p->getLeft(); if (leftChild->getRight() == NULL){// leftChild has no right child p->setData(leftChild->getData()); p->setLeft(leftChild->getLeft()); delete leftChild; return returnNode; } TreeNode * maxLeft = leftChild->getRight(); TreeNode * parent2 = leftChild; while (maxLeft != NULL){parent2 = maxLeft; maxLeft = maxLeft ->getRight();} // now maxLeft is the node to swap with p. p->setData(maxLeft->getData()); if (maxLeft->getLeft()==NULL) parent2->setRight(NULL); // maxLeft a leaf else parent2->setRight(maxLeft->getLeft()); //maxLeft not a leaf delete maxLeft; return returnNode; } };

CS Additional Things for YOU to Do Add a method to the Tree class for returning the maximum value in the BST Add a method to the Tree class for returning the minimum value in the BST Write a function that takes as input an array of type datatype, and an integer n representing the length of the array, and returns a BST Tree Object containing the elements of the input array