trees1 Binary Trees trees2 Basic terminology nodesFinite set of nodes (may be empty -- 0 nodes), which contain data rootFirst node in tree is called.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 206 Introduction to Computer Science II 09 / 24 / 2008 Instructor: Michael Eckmann.
They’re not just binary anymore!
CSC212 Data Structure - Section FG Lecture 15 Trees and Tree Traversals Instructor: Prof. Zhigang Zhu Department of Computer Science City College of New.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
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.
Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
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.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Binary Trees Chapter 6.
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
CHAPTER 71 TREE. Binary Tree A binary tree T is a finite set of one or more nodes such that: (a) T is empty or (b) There is a specially designated node.
TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
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.
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 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 ),
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
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,
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.
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
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.
Trees, Binary Trees, and Binary Search Trees COMP171.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
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.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.
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.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
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.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Binary Tree.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
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.
CS 240Chapter 10 – TreesPage Chapter 10 Trees The tree abstract data type provides a hierarchical to the representation of certain types of relationships.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 CMSC 341 Introduction to Trees Textbook sections:
Binary Trees.
Binary Trees.
Binary Search Tree (BST)
Chapter 20: Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 21: Binary Trees.
Binary Trees.
slides created by Alyssa Harding
General Trees & Binary Trees
Find in a linked list? first last 7  4  3  8 NULL
Trees.
Binary Trees.
CMSC 202 Trees.
Non-Linear Structures
Binary Trees.
Chapter 20: Binary Trees.
Binary Trees.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

trees1 Binary Trees

trees2 Basic terminology nodesFinite set of nodes (may be empty -- 0 nodes), which contain data rootFirst node in tree is called the root

trees3 Basic terminology child nodes childrenEach node may be linked to 0, 1 or 2 child nodes (or children) parent leafA node with children is a parent; a node with no children is a leaf Nodes d and e are children of b Node c is parent of f and g

trees4 More botany & sociology ancestorThe root node has no parent; it is the ancestor of all other nodes descendantsAll nodes in the tree are descendants of the root node siblingsNodes with the same parent are siblings subtreeAny node with descendants (any parent node) is the root node of a subtree

trees5 But wait, there’s more! depthTree depth is the maximum number of steps through ancestor nodes from the deepest leaf back to root –tree with single node (root) has depth of 0 –empty tree has depth of -1 Depth of entire tree is 2; depth of b’s subtree is 1; depth of f’s subtree is 0

trees6 Tree shape terminology FullFull binary tree –every parent has 2 children, –every leaf has equal depth

trees7 Tree shape terminology CompleteComplete binary tree –every level is full except possibly the deepest level –if the deepest level isn’t full, leaf nodes are as far to the left as possible

trees8 Some examples Complete binary tree Full and complete Neither full nor complete Complete Neither Both

trees9 Array representation of a binary tree Complete binary tree is easily represented as an array (either static or dynamic) Root data is stored at index 0 Root’s children are stored at index 1 and 2 A B C A BC

trees10 Array representation of a binary tree In general: –root is tree[0] –for any node at tree[n], the parent node will be found at index (n-1)/2 –for any node at tree[n], the children of n (if any) will be found at tree[2n + 1] and tree[2n + 2]

trees11 Array representation of a binary tree Since these formulas apply to any complete tree represented as an array, can create a tree class with 2 private members: –an array that stores the data –a counter to keep track of the number of nodes in the tree Relationships between nodes can always be determined from the given formulas

trees12 Binary tree with array representation template class binTree {... private: Item* root; int used; int capacity; } Private member root represents container - root is a pointer to a dynamic array of Items Private member used represents number of values currently stored in tree; capacity is current maximum number of values that can be stored

trees13 Binary tree with array representation Choice of member functions for binary tree would depend on application Later on, we’ll look at the heap data structure, which is a binary tree based on an array representation

trees14 Array representation of a binary tree What if tree isn’t complete? –Can still use array, but problem is more complicated -- have to keep track of which children actually exist –One possibility would be to place a known dummy value in empty slots –Another is to add another private member to the class -- an array of bools paired to the data array -- index value is true if data exists, false if not

trees15 Linked representation of binary tree Again, as with linked list, entire tree can be represented with a single pointer -- in this case, a pointer to the root node Nodes are instances of class BTnode, described in the next several slides The class contains functions that operate on single nodes; we will also look at non- member functions that perform tree operations

trees16 template class BTnode { … private: Item data; BTnode *left; BTnode *right; }; Linked representation of binary tree Private member data holds the information in the node; members left and right are pointers to the node’s left and right children

trees17 Functions of BTnode class Constructor: creates node with specified data and left and right children Observer functions –data() returns value of data member –get_left() returns pointer to left child –get_right() returns pointer to right child –is_leaf() returns true if node is a leaf

trees18 Functions of BTnode class Modifiers –set_data(): changes data portion of node –set_left(): assigns new left child to node –set_right(): assigns new right child to node –modifier versions of get_data(), get_left() and get_right()

trees19 Non-member functions Observers (entire tree) –tree_size() returns number of nodes in tree –traversal functions (preorder(), inorder() and postorder()) provide different methods for moving through and processing all nodes of a tree or subtree –print() function uses traversal to print values of data portions of all nodes in a tree shape

trees20 Non-member functions Modifiers (entire tree): –tree_clear(): return memory occupied by an entire tree or subtree to system heap –tree_copy(): returns pointer to a node that is the root of an exact copy of the tree passed to the function as a parameter

trees21 BTnode class {public: BTnode (const Item& entry = Item(), BTnode* L = NULL, BTnode* R = NULL); const Item& data( ) const {return data;} const BTnode* get_left( )const {return left;} const BTnode* get_right( )const {return right;} bool is_leaf( ) const {return (left == NULL) && (right == NULL);} …

trees22 Default constructor template BTnode::BTnode(const Item& entry, BTnode* L,BTnode* R) { data = entry; left = L; right = R; }

trees23 BTnode class definition continued … Item& data( ) { return data; } Btnode* get_left( ) { return left; } Btnode* get_right( ) { return right; } void set_data(const Item& it) { data = it; } void set_left( BTnode* L) { left = L; } void set_right( BTnode* R) { right = R; } …

trees24 Non-member functions … void tree_clear ( Btnode& root); Btnode* tree_copy (const Btnode & root); …

trees25 Function tree_clear( ) Good example of a function in which a recursive solution is the simplest and most elegant method Base case: root is NULL (function does no work) Problem reduction: –clear left subtree (recursive call) –clear right subtree (recursive call) To finish: –return root’s memory to heap –set root to NULL

trees26 Function tree_clear( ) template void tree_clear(BTnode *& root) { if (root != NULL) { tree_clear(root->left); tree_clear(root->right); delete root; root = NULL; } }

trees27 Tree_clear in action Original tree:

trees28 Tree_clear in action Since root is not NULL, proceed recursively down left subtree until a NULL pointer is encountered

trees29 Tree_clear in action This node’s left pointer is NULL; so recursion stops here, and most recent call to function returns with call to tree_clear (root->right) Since this node’s right pointer is also NULL, this recursive call returns, and we proceed to next line: delete root; followed by: root = NULL;

trees30 Tree_clear in action The function then returns, and the same process is repeated at each subtree in root’s left subtree

trees31 Tree_clear in action The same process takes place in the right subtree

trees32 Tree_clear in action Finally, only root is left:

trees33 Function tree_copy( ) Copying a tree is also a recursive function Goal is to return a pointer to a tree that is an exact copy of the tree referenced by the source root pointer Base case: source root is NULL (return NULL) Recursive calls: call copy function on source root’s left & right subtrees Final step: use constructor to create new root pointer

trees34 template BTnode * tree_copy(const BTtnode * root) { BTnode *lp; BTnode *rp; if (root == NULL) return NULL; else { lp = tree_copy(root->left); rp = tree_copy(root->right); return new BTnode (root->data, lp, rp); }

trees35 Binary Trees - ends -