About This Lecture. Trees. Outline. Recursive List Definition slide 1. Recursive Tree Definition. Recursive List Definition slide 2

Size: px
Start display at page:

Download "About This Lecture. Trees. Outline. Recursive List Definition slide 1. Recursive Tree Definition. Recursive List Definition slide 2"

Transcription

1 Revised 21-Mar-05 About This Lecture 2 Trees In this lecture we study a non-linear container called a Tree and a special kind of Tree called a Binary Tree. CMPUT Lecture 18 Department of Computing Science University of Alberta Duane Szafron 03 Some code in this lecture is based on code from the book: Java Structures by Duane A. Bailey or the companion structure package 3 4 Outline Recursive List Definition slide 1 Recursive Structure Definitions Tree Terminology Binary Tree Interface Example - Expression Trees Binary Tree Implementation Tree Traversals Tree Property-based methods We can define a list recursively. A list is either empty or it contains two parts: a head element and the sublist consisting of the rest of the elements called the tail, where the sublist must be a list. Notice that by definition (since a list can be empty), the tail could be empty, so that the complete list could be a singleton list that has exactly one element. empty list singleton list head tail 5 6 Recursive List Definition slide 2 The term node is used to refer to an element at a particular location in a list so that each node contains an element. The relation between the head of a list and the head of its tail sublist is called a link or an edge and it is drawn as a line segment. node simplified view edges head tail head general list fully recursive tail view Recursive Tree Definition A tree is either empty or it contains two parts: a root element and a (possibly empty) list of subtrees, where each subtree is a tree. The term node is used to refer to an element at a particular location in a tree so that each node "contains" an element. Since each subtree is a tree it has its own root. The relation between the root of a tree and the roots of its subtrees is called an edge and it is drawn as a line segment. simplified view edges a node that contains 11 a different node that contains 11 1

2 7 8 Recursive Binary Tree Definition slide 1 Recursive Binary Tree Definition slide 2 A binary tree is a data structure that is not linear, but it can also be defined recursively. A binary tree is either empty or it contains three parts: a root element, a left binary subtree which is a binary tree and a right binary subtree, which is a binary tree. Notice that by definition (since a binary tree can be empty), both subtrees could be empty, so that the complete binary tree could be a singleton tree that has exactly one element. left subtree root empty binary tree singleton binary tree right subtree We can generalize the definitions of node and edge (link) to binary trees. The term node is used to refer to an element at a particular location in a binary tree so that each node contains an element. Since each binary subtree is a binary tree it has its own root. The relation between the root of a tree and the root of each of its subtrees is called a link or edge and is drawn as a line segment. 9 Recursive Binary Tree Definition slide 3 Tree Architecture left subtree root general binary tree right subtree fully recursive view Two trees are disjoint if they share no nodes and no edges. A forest is a set of disjoint trees. We can generalize the notion of subtree so that a tree T 1 is a subtree of a tree T 2 if all of the nodes and edges of T 1 are also in T 2. What we called a subtree before is now called a direct subtree. simplified view edges nodes forest a direct subtree some disjoint trees some subtrees Node Relationships Trees - node relationships example If node N 2 is the root of a direct subtree of node N 1, then node N 2 is a child of node N 1 and node N 1 is the parent of node N 2. The root node of a tree has no parent node in that tree. A node N 1 is an ancestor of a node N 2, if N 1 is the parent of node N 2 or if N 1 is the ancestor of a parent of N 2 (this is a recursive definition: ancestors(n) = parent(n) + ancestors(parent(n))). A node N 2 is a descendant of a node N 1, if N 1 is an ancestor of node N 2. A node with no children is called a leaf node. A node with children is called an interior node. Two nodes are siblings if they have the same parent. The ancestors of node 21 are nodes: and. The parent of node 21 is node. The children of node are nodes: 21, 17 and 67. The descendants of node are nodes: 21, 17, 67, 15, 11, 88 and 99. The leaf nodes are:, 21, 11, 88, 99, 13 and 90. The interior nodes are:,,, 17, 67 and 15. The siblings of node 21 are nodes: 17 and

3 13 14 Measuring Trees A path is the unique shortest sequence of edges from a node to an ancestor. The length of a path is the number of nodes in it. The height of a node is the length of the longest path from a leaf to the node. The height of a tree is the height of its root. The depth (level) of a node is the length of the path from the root to the node. The degree of a node is the number of children it has. The degree (arity) of a tree is the maximum degree of its nodes. Measuring Trees - Example The path from node 88 to node is highlighted in red. The length of the path from node 88 to node is 4. The height of node is 4. The height of the tree is the height of node : 5. The depth (level) of node 67 is the length of the path from node to node 67 which is 3. The degree of node is 3 and the degree of node is 2 and the degree of node 11 is 0. The degree of the tree is Binary Trees - terminology A binary tree is a tree in which nodes can have a maximum of 2 children. A binary tree is oriented if every node with 2 children differentiates between the children by calling them the left child and right child and every node with one child designates it either as a left child or right child. A node in a binary tree is full if it has arity 2. A full binary tree of height h has leaves only on level h and each of its interior nodes is full. A complete binary tree of height h is a full binary tree of height h with 0 or more of the rightmost leaves of level h removed. Binary Tree Examples Node is full in all trees since it has arity 2 in all of them. Node is full in the left tree, but not full in the middle tree where it has arity 0 and not full in the right tree where it has arity 1. full binary tree non-full but complete binary tree non-complete binary tree Binary Tree Traversals There are four common binary tree traversals: Preorder: process root then left subtree then right subtree Inorder: process left subtree then root then right subtree Postorder: process left subtree then right subtree then root Levelorder: process nodes of level i, before processing nodes of level i + 1 (not supported in the structure package) Processing of left and right subtrees is done recursively There are examples of why each traversal is useful, but in the lectures, we will only consider one example (expression evaluation) to show why one of the traversals (postorder) is useful. Binary Tree Traversals Example Preorder: Inorder: Postorder: Levelorder:

4 19 Postorder Expression Evaluation Postorder Traversal Example Consider the expression: (2+3) * 4 / (6-1) There are Java language precedence rules that order the operations: +, *, /, -. Normally, * and / have higher precedence than + and - so if the parentheses were not included, the * and / would be done first. When a compiler parses expressions in a programming language, it uses a binary tree to store the components of the expression. The compiler must ensure that the precedence is correct. One approach is for the compiler to produce a parse tree whose postorder traversal provides input for an evaluation stack that will evaluate the expression in the correct order. The rules for an evaluation stack are: when an operand (number) is processed during traversal, it is pushed onto the stack. when a binary operator is processed during traversal, two operands are popped from the stack, the binary operator is applied to the operands and the result is pushed onto the stack Postorder Traversal Example We will check our example expression to see if a postorder traversal and stack evaluator will correctly evaluate the expression. Here is the parse tree for the expression (2+3) * 4 / (6-1): * 4 / Postorder traversal: * / Stack evaluation of the traversal input: push 2 push 3 pop 3, pop 2, apply + and push 5 push 4 pop 4, pop 5, apply * and push push 6 push 1 pop 1, pop 6, apply - and push 5 pop 5, pop, apply / and push 4 Interface for Binary Trees The BinaryTree class in these lectures is significantly different than in the structure package. The structure package s BinaryTree is cursor-based but we will not have cursors. Our BinaryTree class implements the Collection interface. We will have an Interface for BinaryTree, the structure package does not BinaryTreeInterface (slide 1) public interface BinaryTreeInterface extends Collection { /* Inherited from Store mentioned here in comments for documentation purposes. public int size() // post: return the number of elements in the tree public boolean isempty() // post: return true iff the receiver is empty public void clear() // post: makes the receiver tree and all its subtrees empty */ BinaryTreeInterface (slide 2) /* Inherited from Collection mentioned here in comments for documentation purposes. public boolean contains(object anobject) // post: returns true iff the receiver contains anobject public Object remove(object anobject) // post: if receiver contains an element equal to anobject // one such element is removed and returned public Iterator elements() // post: returns an Iterator that goes through the // receiver's elements in inorder order. Use inorder as the "default" traversal since it is most intuitive. 4

5 26 BinaryTreeInterface (slide 3) BinaryTreeInterface (slide 4) public void add(object anobject) //post: anobject is added to the receiver. // It will be added as the root or a child of the // root if possible, otherwise recursively // in the left subtree (left chosen arbitrarily). */ // methods defined by BinaryTreeInterface start here public boolean hasleft() ; //post: returns true iff the receiver s left subtree // is non-empty public boolean hasright() ; //post: returns true iff the receiver s right subtree // is non-empty public boolean hasparent() ; //post: returns true iff the receiver has a parent BinaryTreeInterface (slide 5) BinaryTreeInterface (slide 6) public Object rootelement() ; //pre: receiver is not empty //post: returns the receiver's root element public BinaryTreeInterface left() ; //pre: receiver has a non-empty left subtree //post: returns the left subtree public BinaryTreeInterface right() ; //pre: receiver has a non-empty right subtree //post: returns the right subtree public BinaryTreeInterface parent() ; //pre: receiver has a parent //post: returns the parent public Iterator preorder() ; // post: returns an Iterator that goes through the receiver's // elements in preorder order. public Iterator postorder() ; // post: returns an Iterator that goes through the receiver's // elements in postorder order. public Iterator inorder() ; // post: returns an Iterator that goes through the receiver's // elements in inorder order BinaryTreeInterface (slide 7) Detach Example public void detachleftsubtree() ; //post: detaches the receiver's left subtree - it is the // same as before but is no longer a subtree. // The receiver has an empty left subtree. t t public void detachrightsubtree() ; //post: detaches the receiver's right subtree - it is the // same as before but is no longer a subtree. // The receiver has an empty right subtree. t.detachfromparent() public void detachfromparent() ; //post: detaches the receiver from its parent. The receiver // is the same tree as before but is no longer a subtree. // The parent has an empty subtree where the receiver // used to be. 5

6 31 32 BinaryTreeInterface (slide 8) BinaryTreeInterface (slide 9) public void attachleft(binarytreeinterface newleft) ; // pre: newleft is not empty, the receiver is not empty, // newleft!= receiver // post: newleft becomes the receiver's left subtree. // If the receiver had a left subtree it is detached. // If newleft had a parent, it is detached before being // attached to the receiver. public void attachright(binarytreeinterface newright) ; // pre: newright is not empty, the receiver is not empty, // newright!= receiver // post: newright becomes the receiver's right subtree. // If the receiver had a right subtree it is detached. // If newright had a parent, it is detached before being // attached to the receiver. public void attachparent(binarytreeinterface newparent, boolean onleft) ; // pre: newparent is not empty, the receiver is not empty, // newparent!= receiver // post: newparent becomes the receiver's Parent. // If the receiver had a parent it is detached. // If onleft is true the receiver becomes newparent's left // subtree; otherwise it becomes newparent's right subtree. // If newparent had a subtree on that side it is detached. public void setroot(object anobject) ; // post: the receiver's root element is anobject Binary Tree - Expression Evaluation BinaryTree Example - main() As an example of building a Binary Tree and using it, we will write a program that uses a Binary Tree and Stack to evaluate expressions. For example, here is the parsed form of the expression: (2+3) * 4 / (6-1) / public static void main(string args[]) { /* A Binary Expression Tree. */ BinaryTree tree; int value; tree = constructtree(); value = evaluatetree(tree); System.out.println(value); * code based on Bailey pg. 192 BinaryTree Example - constructtree() 35 BinaryTree Example - evaluatetree() slide 1 36 private static BinaryTree constructtree() { // post: return the example expression tree BinaryTree leftsubtree, rightsubtree, tree ; // build the tree from the bottom up leftsubtree = new BinaryTree(); leftsubtree.add("+"); leftsubtree.add(new Integer(2)); leftsubtree.add(new Integer(3)); tree = new BinaryTree(); tree.add("*"); tree.attachleft(leftsubtree) ; tree.add(new Integer (4)) ; leftsubtree = tree ; rightsubtree = new BinaryTree(); rightsubtree.add("-"); rightsubtree.add(new Integer(6)); rightsubtree.add(new Integer(1)); tree = new BinaryTree(); tree.add("/"); tree.attachleft(leftsubtree) ; tree.attachright(rightsubtree) ; return tree; private static int evaluatetree(binarytree tree) { // pre: (1) tree is not empty (2) all leaf nodes are Integers (3) all the inner nodes are strings of the form +, -. *, or / // post: return the int value of the given Tree. code based on Bailey pg

7 37 38 BinaryTree Example - evaluatetree() slide 1 Binary Tree - implementation private static int evaluatetree(binarytree tree) { // post: return the value of the given Tree. if (!three.hasleft() &&! Three.hasRight()) return tree.intvalue(); else if (this.rootelement().equals("+")) return evaluatetree(tree.left()+evaluatetree(tree.right()); else if (this.rootelement().equals( -")) return evaluatetree(tree.left()-evaluatetree(tree.right()); else if (this.rootelement().equals( *")) return evaluatetree(tree.left()*evaluatetree(tree.right()); else return evaluatetree(tree.left()/evaluatetree(tree.right()); code based on Bailey pg. 192 The Binary Tree class could be implemented like the linked list classes using an auxiliary class called BinaryTreeNode which would contain one element and have links to the parent node, the left child node and the right child node. To illustrate an alternative, we will not have a node class, only the BinaryTree class itself. A BinaryTree instance will contain its root element and links to its parent tree and left and right subtrees. Because we do not permit an element in a tree to be null we can represent the empty tree by setting its root element to null BinaryTree - state and Constructor public class BinaryTree implements BinaryTreeInterface { protected Object rootelement ; protected BinaryTreeInterface parent ; protected BinaryTreeInterface left ; protected BinaryTreeInterface right ; parent rootelement left right public BinaryTree() { // post: initializes a BinaryTree to be empty this.rootelement = null ; // indicates an empty tree this.parent = null ; this.left = null ; this.right = null ; BinaryTree Basic tests and access public boolean isempty() { // post: return true iff the receiver is empty return (this.rootelement == null) ; public boolean hasleft() { //post: returns true iff receiver has a non-empty left subtree return (this.left!= null) ; public Object rootelement() { //pre: receiver is not empty //post: returns the receiver's root element return this.rootelement ; public BinaryTreeInterface left() { //pre: receiver has a non-empty left subtree //post: returns the left subtree return this.left ; hasright(), hasparent() are similar right(), parent() are similar BinaryTree size BinaryTree contains public int size() { // post: return the number of elements in the receiver int size = 0 ; if (!this.isempty()) { size = 1 ; // count 1 for the root if (this.hasleft()) size = size + this.left.size() ; if (this.hasright()) size = size + this.right.size() ; return size ; public boolean contains(object anobject) { // post: returns true iff the receiver contains anobject if (this.isempty()) return false ; else { return this.rootelement.equals(anobject) (this.hasleft() && left.contains(anobject)) (this.hasright() && right.contains(anobject)); 7

8 44 BinaryTree add public void add(object anobject) { //post: anobject is added to the receiver. It will be added // as the root or a child of the root if possible, // otherwise recursively in the left subtree. if (this.isempty()) this.rootelement = anobject ; else { if (this.hasleft() && this.hasright()) { this.left().add( anobject ) ; else { if (!this.hasleft()) { this.left = this.newchild(anobject, this) ; else this.right = this.newchild(anobject,this) ; BinaryTree newchild [protected] protected BinaryTree newchild(object anobject, BinaryTreeInterface parent){ //post: create a new child leaf with anobject as its // element and "parent" as its parent BinaryTree newtree = new BinaryTree() ; newtree.rootelement= anobject ; newtree.parent = parent ; return newtree ; BinaryTree create Iterators BinaryTree detachleftsubtree public Iterator elements() { return inorder() ; // post: returns an Iterator that goes through the receiver's // elements in inorder order. public Iterator inorder() { // post: returns an Iterator that goes through the receiver's // elements in inorder order. Vector vector = new Vector(this.size()); BinaryTree.inorder(this, vector) ; return vector.elements() ; protected static void inorder(binarytreeinterface t, Vector vector) { // post: the elements of t are stored in vector in inorder order. if (!t.isempty()) { if (t.hasleft()) inorder(t.left(), vector) ; vector.addelement( t.rootelement() ) ; if (t.hasright()) inorder(t.right(), vector) ; preorder, postorder are similar public void detachleftsubtree() { // post: detaches the receiver's left subtree - it is the same // tree as before but is no longer a subtree. // The receiver has an empty left subtree. detachrightsubtree is similar if (this.left!= null) { BinaryTreeInterface temp = this.left ; this.left = null ; temp.detachfromparent() ; Why call temp.detachfromparent()? Why not just have temp.parent = null ; BinaryTree detachfromparent BinaryTree attachleft attachright is similar public void detachfromparent() { // post: detaches the receiver from its parent. The receiver // is the same tree as before but is no longer a subtree. // Parent has an empty subtree where the receiver used to be. if (this.parent!=null) { BinaryTreeInterface temp = this.parent ; this.parent = null ; if ( (temp.hasleft()) && (temp.left() == this)) { temp.detachleftsubtree(); if ( (temp.hasright()) && (temp.right() == this)) { temp.detachrightsubtree(); Why doesn t the call to detachleftsubtree cause an infinite loop? public void attachleft(binarytreeinterface newleft) { // pre: newleft is not empty, the receiver is not empty, // newleft!= receiver // post: newleft becomes the receiver's left subtree. // If the receiver had a left subtree it is detached. // If newleft had a parent, it is detached before being // attached to the receiver. if ((!this.hasleft()) (this.left()!= newleft)) { if (this.hasleft()) this.detachleftsubtree() ; this.left = newleft ; newleft.attachparent(this,true) ; Why call newleft.attachparent( )? Why not just have newleft.parent = this ; 8

9 49 50 BinaryTree attachparent BinaryTree clear, setroot public void attachparent(binarytreeinterface newparent, boolean onleft) { // pre: newparent is not empty, the receiver is not empty, // newparent!= receiver // post: newparent becomes the receiver's Parent. // If the receiver had a parent it is detached. // If onleft is true the receiver becomes newparent's left subtree; // otherwise it becomes newparent's right subtree. // If newparent had a subtree on that side it is detached. if (this.parent!= newparent) { if (this.hasparent()) this.detachfromparent() ; this.parent = newparent ; if (onleft) newparent.attachleft(this) ; else newparent.attachright(this) ; public void clear() { // post: makes the receiver tree and all its subtrees empty this.detachfromparent() ; Could we just set left and right to this.rootelement = null ; null & avoid using recursion? if (this.hasleft()) this.left.clear(); // will set left to null if (this.hasright()) this.right.clear(); // will set right to null public void setroot(object anobject) { // post: the receiver's root element is anobject this.rootelement = anobject ; Strategy for Removing an Element Find the element in the tree. Example: remove 1. Find the node containing. node If the element is in a leaf, delete the leaf. If the element is in an interior node, find a leaf beneath that node, delete the leaf, and replace the element by the leaf s element. 2. Find a leaf below that node. leaf below node 3. Delete the leaf. 4. Replace by the leaf element. node Binary Tree - remove Binary Tree find [protected] public Object remove(object anobject) { // post: if the receiver contains an element equal to anobject // one such element is is removed and returned; // otherwise does nothing. Object returnvalue ; BinaryTreeInterface where = find(this,anobject) ; if (where == null) return null ; returnvalue = where.rootelement() ; if ((!where.hasleft()) && (!where.hasright()) ) { where.clear() ; else { Object leafelement = findleafandremove(where) ; where.setroot( leafelement ) ; return returnvalue ; protected static BinaryTreeInterface find(binarytreeinterface t, Object anobject) { // post: if the receiver contains an element "equal to" anobject // returns a subtree having such an element at its root; // otherwise returns null. if (t.isempty()) { return null ; if (t.rootelement().equals(anobject)) { return t ; if (t.hasleft()) { BinaryTreeInterface where = find(t.left(), anobject) ; if (where!= null) { return where ; if (t.hasright()) { return find(t.right(), anobject) ; return null ; 9

10 56 Binary Tree findleafandremove [protected] protected static Object findleafandremove(binarytreeinterface t) { // post: removes a leaf node from the given tree, t, and returns // the leaf node s element in it. // Returns null if the given tree is empty. if (t.isempty()) return null ; if ((!t.hasleft()) && (!t.hasright()) ) { Object returnvalue = t.rootelement() ; t.clear() ; return returnvalue ; else { if (t.hasleft()) { return( findleafandremove(t.left()) ) ; else { return( findleafandremove(t.right()) ) ; External methods The following examples show how methods can be written outside the BinaryTree class. They are static, with a BinaryTreeInterface argument Binary Tree - depth public static int depth(binarytreeinterface t) { //pre: t is not empty //post: returns the depth of t's root node BinaryTreeInterface ancestor = t ; int depth = 1 ; while (ancestor.hasparent()) { ancestor = ancestor.parent() ; depth++ ; return depth ; Binary Tree - height public static int height(binarytreeinterface t) { //post: returns the height of the given tree (t). if (t.isempty()) return 0; else int lheight = 0 ; if (t.hasleft()) { lheight = height(t.left()); int rheight = 0 ; if (t.hasright()) { rheight = height(t.right()) ; return 1 + Math.max(lheight,rheight); time complexity: O(n) n = #nodes in the tree

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree Binary Search Trees CMPUT 115 - Lecture Department of Computing Science University of Alberta Revised 21-Mar-05 In this lecture we study an important data structure: Binary Search Tree (BST) Motivation

More information

Topic 14. The BinaryTree ADT

Topic 14. The BinaryTree ADT Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary tree implementation Examine a binary tree

More information

tree nonlinear Examples

tree nonlinear Examples The Tree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary tree implementation Examine a binary tree example 10-2

More information

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology Trees : Part Section. () (2) Preorder, Postorder and Levelorder Traversals Definition: A tree is a connected graph with no cycles Consequences: Between any two vertices, there is exactly one unique path

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco 06-0: Ordered List ADT Operations: Insert an element in the list

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 11: Binary Search Trees MOUNA KACEM mouna@cs.wisc.edu Fall 2018 General Overview of Data Structures 2 Introduction to trees 3 Tree: Important non-linear data structure

More information

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop. Tree A tree consists of a set of nodes and a set of edges connecting pairs of nodes. A tree has the property that there is exactly one path (no more, no less) between any pair of nodes. A path is a connected

More information

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures. Trees Q: Why study trees? : Many advance DTs are implemented using tree-based data structures. Recursive Definition of (Rooted) Tree: Let T be a set with n 0 elements. (i) If n = 0, T is an empty tree,

More information

Binary Trees, Binary Search Trees

Binary Trees, Binary Search Trees Binary Trees, Binary Search Trees Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search, insert, delete)

More information

CS200: Trees. Rosen Ch & 11.3 Prichard Ch. 11. CS200 - Trees

CS200: Trees. Rosen Ch & 11.3 Prichard Ch. 11. CS200 - Trees CS200: Trees Rosen Ch. 11.1 & 11.3 Prichard Ch. 11 1 Trees A A node has only one parent! Except the root: zero parents B C D E F Tree grows top to bottom! 2 Tree Terminology Node interior node path root

More information

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 Chapter 11!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 2015-12-01 09:30:53 1/54 Chapter-11.pdf (#13) Terminology Definition of a general tree! A general tree T is a set of one or

More information

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 Chapter 11!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 2015-03-25 21:47:41 1/53 Chapter-11.pdf (#4) Terminology Definition of a general tree! A general tree T is a set of one or more

More information

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees Computer Science 0 Data Structures Siena College Fall 08 Topic Notes: Trees We ve spent a lot of time looking at a variety of structures where there is a natural linear ordering of the elements in arrays,

More information

Binary Trees

Binary Trees Binary Trees 4-7-2005 Opening Discussion What did we talk about last class? Do you have any code to show? Do you have any questions about the assignment? What is a Tree? You are all familiar with what

More information

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo The tree data structure 1 Trees COL 106 Amit Kumar Shweta Agrawal Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo 1 Trees The tree data structure 3 A rooted tree data structure stores

More information

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology Chapter 11 Trees Definition of a general tree A general tree T is a set of one or more nodes such that T is partitioned into disjoint subsets: A single node r, the root Sets that are general trees, called

More information

Trees. (Trees) Data Structures and Programming Spring / 28

Trees. (Trees) Data Structures and Programming Spring / 28 Trees (Trees) Data Structures and Programming Spring 2018 1 / 28 Trees A tree is a collection of nodes, which can be empty (recursive definition) If not empty, a tree consists of a distinguished node r

More information

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals Binary Trees 1 Binary Tree Node Relationships 2 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the

More information

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management Binary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from

More information

TREES. Trees - Introduction

TREES. Trees - Introduction TREES Chapter 6 Trees - Introduction All previous data organizations we've studied are linear each element can have only one predecessor and successor Accessing all elements in a linear sequence is O(n)

More information

Tree traversals and binary trees

Tree traversals and binary trees Tree traversals and binary trees Comp Sci 1575 Data Structures Valgrind Execute valgrind followed by any flags you might want, and then your typical way to launch at the command line in Linux. Assuming

More information

a graph is a data structure made up of nodes in graph theory the links are normally called edges

a graph is a data structure made up of nodes in graph theory the links are normally called edges 1 Trees Graphs a graph is a data structure made up of nodes each node stores data each node has links to zero or more nodes in graph theory the links are normally called edges graphs occur frequently in

More information

Recursive Data Structures and Grammars

Recursive Data Structures and Grammars Recursive Data Structures and Grammars Themes Recursive Description of Data Structures Grammars and Parsing Recursive Definitions of Properties of Data Structures Recursive Algorithms for Manipulating

More information

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR STUDENT IDENTIFICATION NO MULTIMEDIA COLLEGE JALAN GURNEY KIRI 54100 KUALA LUMPUR FIFTH SEMESTER FINAL EXAMINATION, 2014/2015 SESSION PSD2023 ALGORITHM & DATA STRUCTURE DSEW-E-F-2/13 25 MAY 2015 9.00 AM

More information

Chapter 20: Binary Trees

Chapter 20: Binary Trees Chapter 20: Binary Trees 20.1 Definition and Application of Binary Trees Definition and Application of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two other

More information

Tree Structures. A hierarchical data structure whose point of entry is the root node

Tree Structures. A hierarchical data structure whose point of entry is the root node Binary Trees 1 Tree Structures A tree is A hierarchical data structure whose point of entry is the root node This structure can be partitioned into disjoint subsets These subsets are themselves trees and

More information

Binary Trees Fall 2018 Margaret Reid-Miller

Binary Trees Fall 2018 Margaret Reid-Miller Binary Trees 15-121 Fall 2018 Margaret Reid-Miller Trees Fall 2018 15-121 (Reid-Miller) 2 Binary Trees A binary tree is either empty or it contains a root node and left- and right-subtrees that are also

More information

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false. Name: Class: Date: CSCI-401 Examlet #5 True/False Indicate whether the sentence or statement is true or false. 1. The root node of the standard binary tree can be drawn anywhere in the tree diagram. 2.

More information

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Trees Kostas Alexis Trees List, stacks, and queues are linear in their organization of data. Items are one after another In this section, we organize data in a

More information

Tree Terminology. root. Edge. interior node. parent. path. subtree. child. leaf

Tree Terminology. root. Edge. interior node. parent. path. subtree. child. leaf Tree Terminology Binary Trees CS00: Trees interior node Node path root parent Edge subtree Degree? Depth/Level? A binary tree is a set T of nodes such that either T is empty, or T is partitioned into three

More information

BBM 201 Data structures

BBM 201 Data structures BBM 201 Data structures Lecture 11: Trees 2018-2019 Fall Content Terminology The Binary Tree The Binary Search Tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

More information

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 10 / 10 / 2016 Instructor: Michael Eckmann Today s Topics Questions? Comments? A few comments about Doubly Linked Lists w/ dummy head/tail Trees Binary trees

More information

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking Review for Test 2 (Chapter 6-10) Chapter 6: Template functions & classes 1) What is the primary purpose of template functions? A. To allow a single function to be used with varying types of arguments B.

More information

CE 221 Data Structures and Algorithms

CE 221 Data Structures and Algorithms CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, 4.1 4.2 Izmir University of Economics 1 Preliminaries - I (Recursive) Definition: A tree is a collection of nodes. The

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College November 13, 2017 Outline Outline 1 C++ Supplement.1: Trees Outline C++ Supplement.1: Trees 1 C++ Supplement.1: Trees Uses

More information

Lecture Topics. Object Structures. Hierarchical Organization. Tree Concepts. Hierarchical Organization. Hierarchical Organization

Lecture Topics. Object Structures. Hierarchical Organization. Tree Concepts. Hierarchical Organization. Hierarchical Organization Object Structures Trees CISH 4020 Tom Blough blought@rh.edu www.rh.edu/~blought 860.618.4148 Lecture Topics Tree Concepts Traversals of a Tree Java Interfaces for Trees Examples of Binary Trees Examples

More information

March 20/2003 Jayakanth Srinivasan,

March 20/2003 Jayakanth Srinivasan, Definition : A simple graph G = (V, E) consists of V, a nonempty set of vertices, and E, a set of unordered pairs of distinct elements of V called edges. Definition : In a multigraph G = (V, E) two or

More information

CSC148 Week 6. Larry Zhang

CSC148 Week 6. Larry Zhang CSC148 Week 6 Larry Zhang 1 Announcements Test 1 coverage: trees (topic of today and Wednesday) are not covered Assignment 1 slides posted on the course website. 2 Data Structures 3 Data Structures A data

More information

Algorithms and Data Structures

Algorithms and Data Structures Lesson 3: trees and visits Luciano Bononi http://www.cs.unibo.it/~bononi/ (slide credits: these slides are a revised version of slides created by Dr. Gabriele D Angelo) International

More information

Terminology. The ADT Binary Tree. The ADT Binary Search Tree

Terminology. The ADT Binary Tree. The ADT Binary Search Tree Terminology The ADT Binary Tree The ADT Binary Search Tree 1 Terminology 3 A general tree A general tree T is a set of one or more nodes such that T is partitioned into disjoint subsets: o A single node

More information

from inheritance onwards but no GUI programming expect to see an inheritance question, recursion questions, data structure questions

from inheritance onwards but no GUI programming expect to see an inheritance question, recursion questions, data structure questions Exam information in lab Tue, 18 Apr 2017, 9:00-noon programming part from inheritance onwards but no GUI programming expect to see an inheritance question, recursion questions, data structure questions

More information

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Spring 2018 Profs Bill & Jon

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Spring 2018 Profs Bill & Jon CSCI 136 Data Structures & Advanced Programming Lecture 22 Spring 2018 Profs Bill & Jon Administrative Details CS Colloquium?!?! Meets (almost) every Friday at 2:30pm Guest speaker presents their research

More information

Chapter Contents. Trees. Tree Concepts. Hierarchical Organization. Hierarchical Organization. Hierarchical Organization.

Chapter Contents. Trees. Tree Concepts. Hierarchical Organization. Hierarchical Organization. Hierarchical Organization. Chapter Contents Chapter 18 Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals of a General Tree Java Interfaces for Interfaces for All

More information

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University U Kang (2016) 1 In This Lecture The concept of binary tree, its terms, and its operations Full binary tree theorem Idea

More information

TREES Lecture 10 CS2110 Spring2014

TREES Lecture 10 CS2110 Spring2014 TREES Lecture 10 CS2110 Spring2014 Readings and Homework 2 Textbook, Chapter 23, 24 Homework: A thought problem (draw pictures!) Suppose you use trees to represent student schedules. For each student there

More information

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents Section 5.5 Binary Tree A binary tree is a rooted tree in which each vertex has at most two children and each child is designated as being a left child or a right child. Thus, in a binary tree, each vertex

More information

CS24 Week 8 Lecture 1

CS24 Week 8 Lecture 1 CS24 Week 8 Lecture 1 Kyle Dewey Overview Tree terminology Tree traversals Implementation (if time) Terminology Node The most basic component of a tree - the squares Edge The connections between nodes

More information

Why Do We Need Trees?

Why Do We Need Trees? CSE 373 Lecture 6: Trees Today s agenda: Trees: Definition and terminology Traversing trees Binary search trees Inserting into and deleting from trees Covered in Chapter 4 of the text Why Do We Need Trees?

More information

Binary trees. Binary trees. Binary trees

Binary trees. Binary trees. Binary trees Binary trees March 23, 2018 1 Binary trees A binary tree is a tree in which each internal node has at most two children. In a proper binary tree, each internal node has exactly two children. Children are

More information

Trees Chapter 19, 20. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Trees Chapter 19, 20. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Trees Chapter 19, 20 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Trees: Trees as data structures Tree terminology Tree implementations Analyzing tree efficiency Tree traversals

More information

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux CS 231 Data Structures and Algorithms Fall 2018 Binary Search Trees Lecture 23 October 29, 2018 Prof. Zadia Codabux 1 Agenda Ternary Operator Binary Search Tree Node based implementation Complexity 2 Administrative

More information

UNIT IV -NON-LINEAR DATA STRUCTURES 4.1 Trees TREE: A tree is a finite set of one or more nodes such that there is a specially designated node called the Root, and zero or more non empty sub trees T1,

More information

Building Java Programs

Building Java Programs Building Java Programs Binary Trees reading: 17.1 17.3 2 Trees in computer science TreeMap and TreeSet implementations folders/files on a computer family genealogy; organizational charts AI: decision trees

More information

CS 151. Binary Trees. Friday, October 5, 12

CS 151. Binary Trees. Friday, October 5, 12 CS 151 Binary Trees 1 Binary Tree Examples Without telling you what a binary tree is, here are some examples (that I will draw on the board): The dots/circles are called nodes, or vertices (singular: one

More information

Binary Trees. Reading: Lewis & Chase 12.1, 12.3 Eck Programming Course CL I

Binary Trees. Reading: Lewis & Chase 12.1, 12.3 Eck Programming Course CL I inary Trees Reading: Lewis & hase 12.1, 12.3 Eck 9.4.1 Objectives Tree basics Learn the terminology used when talking about trees iscuss methods for traversing trees iscuss a possible implementation of

More information

Trees. Truong Tuan Anh CSE-HCMUT

Trees. Truong Tuan Anh CSE-HCMUT Trees Truong Tuan Anh CSE-HCMUT Outline Basic concepts Trees Trees A tree consists of a finite set of elements, called nodes, and a finite set of directed lines, called branches, that connect the nodes

More information

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values) 9// References and Homework Text: Chapters, and ABSTRACT DATA TYPES; LISTS & TREES Homework: Learn these List methods, from http://docs.oracle.com/javase/7/docs/api/java/util/list.html add, addall, contains,

More information

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson Reading: Carrano, Chapter 15 Introduction to trees The data structures we have seen so far to implement

More information

Name CPTR246 Spring '17 (100 total points) Exam 3

Name CPTR246 Spring '17 (100 total points) Exam 3 Name CPTR246 Spring '17 (100 total points) Exam 3 1. Linked Lists Consider the following linked list of integers (sorted from lowest to highest) and the changes described. Make the necessary changes in

More information

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it. Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 49 Module 09 Other applications: expression tree

More information

Lecture 26. Introduction to Trees. Trees

Lecture 26. Introduction to Trees. Trees Lecture 26 Introduction to Trees Trees Trees are the name given to a versatile group of data structures. They can be used to implement a number of abstract interfaces including the List, but those applications

More information

INF2220: algorithms and data structures Series 1

INF2220: algorithms and data structures Series 1 Universitetet i Oslo Institutt for Informatikk A. Maus, R.K. Runde, I. Yu INF2220: algorithms and data structures Series 1 Topic Trees & estimation of running time (Exercises with hints for solution) Issued:

More information

Introduction to Binary Trees

Introduction to Binary Trees Introduction to inary Trees 1 ackground ll data structures examined so far are linear data structures. Each element in a linear data structure has a clear predecessor and a clear successor. Precessors

More information

Garbage Collection: recycling unused memory

Garbage Collection: recycling unused memory Outline backtracking garbage collection trees binary search trees tree traversal binary search tree algorithms: add, remove, traverse binary node class 1 Backtracking finding a path through a maze is an

More information

CSE 230 Intermediate Programming in C and C++ Binary Tree

CSE 230 Intermediate Programming in C and C++ Binary Tree CSE 230 Intermediate Programming in C and C++ Binary Tree Fall 2017 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu Introduction to Tree Tree is a non-linear data structure

More information

Introduction to Computers and Programming. Concept Question

Introduction to Computers and Programming. Concept Question Introduction to Computers and Programming Prof. I. K. Lundqvist Lecture 7 April 2 2004 Concept Question G1(V1,E1) A graph G(V, where E) is V1 a finite = {}, nonempty E1 = {} set of G2(V2,E2) vertices and

More information

CSE 143 Lecture 19. Binary Trees. read slides created by Marty Stepp

CSE 143 Lecture 19. Binary Trees. read slides created by Marty Stepp CSE 143 Lecture 19 Binary Trees read 17.1-17.2 slides created by Marty Stepp http://www.cs.washington.edu/143/ Trees tree: A directed, acyclic structure of linked nodes. directed : Has one-way links between

More information

Trees. CSE 373 Data Structures

Trees. CSE 373 Data Structures Trees CSE 373 Data Structures Readings Reading Chapter 7 Trees 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical relationships File directories

More information

[ DATA STRUCTURES ] Fig. (1) : A Tree

[ DATA STRUCTURES ] Fig. (1) : A Tree [ DATA STRUCTURES ] Chapter - 07 : Trees A Tree is a non-linear data structure in which items are arranged in a sorted sequence. It is used to represent hierarchical relationship existing amongst several

More information

Computer Science 136 Exam 2

Computer Science 136 Exam 2 Computer Science 136 Exam 2 Sample exam Show all work. No credit will be given if necessary steps are not shown or for illegible answers. Partial credit for partial answers. Be clear and concise. Write

More information

Stacks, Queues and Hierarchical Collections

Stacks, Queues and Hierarchical Collections Programming III Stacks, Queues and Hierarchical Collections 2501ICT Nathan Contents Linked Data Structures Revisited Stacks Queues Trees Binary Trees Generic Trees Implementations 2 Copyright 2002- by

More information

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr mahmoud.sakr@cis.asu.edu.eg Cairo, Egypt, October 2012 Binary Search Trees (BST) 1. Hierarchical data structure with a single reference to root

More information

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers? Review for Final (Chapter 6 13, 15) 6. Template functions & classes 1) What is the primary purpose of template functions? A. To allow a single function to be used with varying types of arguments B. To

More information

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text) Lec 17 April 8 Topics: binary Trees expression trees Binary Search Trees (Chapter 5 of text) Trees Linear access time of linked lists is prohibitive Heap can t support search in O(log N) time. (takes O(N)

More information

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge Trees & Heaps Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Fall 2018 Jill Seaman!1 Tree: non-recursive definition! Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every

More information

IX. Binary Trees (Chapter 10)

IX. Binary Trees (Chapter 10) IX. Binary Trees (Chapter 10) -1- A. Introduction: Searching a linked list. 1. Linear Search /* To linear search a list for a particular Item */ 1. Set Loc = 0; 2. Repeat the following: a. If Loc >= length

More information

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 07 / 15 / 2016 Instructor: Michael Eckmann Today s Topics Questions? Comments? Binary trees implementation Binary search trees Michael Eckmann - Skidmore College

More information

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees Outline General Trees Terminology, Representation, Properties Binary Trees Representations, Properties, Traversals Recursive Algorithms

More information

Definitions A A tree is an abstract t data type. Topic 17. "A tree may grow a. its leaves will return to its roots." Properties of Trees

Definitions A A tree is an abstract t data type. Topic 17. A tree may grow a. its leaves will return to its roots. Properties of Trees Topic 17 Introduction ti to Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb Definitions A A tree is an abstract t data t d internal type nodes one

More information

Principles of Computer Science

Principles of Computer Science Principles of Computer Science Binary Trees 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 1 Today s Topics Extending LinkedList with Fast Search Sorted Binary Trees Tree Concepts Traversals of a Binary

More information

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Advanced Java Concepts Unit 5: Trees. Notes and Exercises Advanced Java Concepts Unit 5: Trees. Notes and Exercises A Tree is a data structure like the figure shown below. We don t usually care about unordered trees but that s where we ll start. Later we will

More information

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management Binary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from

More information

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage: Discussion 2C Notes (Week 8, February 25) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Trees Definitions Yet another data structure -- trees. Just like a linked

More information

An Introduction to Trees

An Introduction to Trees An Introduction to Trees Alice E. Fischer Spring 2017 Alice E. Fischer An Introduction to Trees... 1/34 Spring 2017 1 / 34 Outline 1 Trees the Abstraction Definitions 2 Expression Trees 3 Binary Search

More information

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms... First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms.... Q1) What are some of the applications for the tree data structure? Q2) There are 8, 15, 13, and

More information

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree. Unit 6: Binary Trees Part 1 Engineering 4892: Data Structures Faculty of Engineering & Applied Science Memorial University of Newfoundland July 11, 2011 1 Binary trees 1 Binary search trees Analysis of

More information

A set of nodes (or vertices) with a single starting point

A set of nodes (or vertices) with a single starting point Binary Search Trees Understand tree terminology Understand and implement tree traversals Define the binary search tree property Implement binary search trees Implement the TreeSort algorithm 2 A set of

More information

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1 CS61B Lecture #20: Trees Last modified: Mon Oct 8 21:21:22 2018 CS61B: Lecture #20 1 A Recursive Structure Trees naturally represent recursively defined, hierarchical objects with more than one recursive

More information

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Stacks, Queues and Hierarchical Collections. 2501ICT Logan Stacks, Queues and Hierarchical Collections 2501ICT Logan Contents Linked Data Structures Revisited Stacks Queues Trees Binary Trees Generic Trees Implementations 2 Queues and Stacks Queues and Stacks

More information

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am Announcements Midterm exam 2, Thursday, May 18 Closed book/notes but one sheet of paper allowed Covers up to stacks and queues Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps

More information

Data Structure - Binary Tree 1 -

Data Structure - Binary Tree 1 - Data Structure - Binary Tree 1 - Hanyang University Jong-Il Park Basic Tree Concepts Logical structures Chap. 2~4 Chap. 5 Chap. 6 Linear list Tree Graph Linear structures Non-linear structures Linear Lists

More information

COMP : Trees. COMP20012 Trees 219

COMP : Trees. COMP20012 Trees 219 COMP20012 3: Trees COMP20012 Trees 219 Trees Seen lots of examples. Parse Trees Decision Trees Search Trees Family Trees Hierarchical Structures Management Directories COMP20012 Trees 220 Trees have natural

More information

Trees, Binary Trees, and Binary Search Trees

Trees, Binary Trees, and Binary Search Trees COMP171 Trees, Binary Trees, and Binary Search Trees 2 Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

More information

Lecture 37 Section 9.4. Wed, Apr 22, 2009

Lecture 37 Section 9.4. Wed, Apr 22, 2009 Preorder Inorder Postorder Lecture 37 Section 9.4 Hampden-Sydney College Wed, Apr 22, 2009 Outline Preorder Inorder Postorder 1 2 3 Preorder Inorder Postorder 4 Preorder Inorder Postorder Definition (Traverse)

More information

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example. Trees, Binary Search Trees, and Heaps CS 5301 Fall 2013 Jill Seaman Tree: non-recursive definition Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every node (except

More information

Trees. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme Goodrich, Tamassia. Trees 1

Trees. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme Goodrich, Tamassia. Trees 1 Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery Trees 1 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child

More information

TREES Lecture 12 CS2110 Fall 2016

TREES Lecture 12 CS2110 Fall 2016 TREES Lecture 12 CS2110 Fall 2016 Prelim 1 tonight! 2 5:30 prelim is very crowded. You HAVE to follow these directions: 1. Students taking the normal 5:30 prelim (not the quiet room) and whose last names

More information

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1 Trees Introduction & Terminology Cinda Heeren / Geoffrey Tien 1 Review: linked lists Linked lists are constructed out of nodes, consisting of a data element a pointer to another node Lists are constructed

More information

Introduction to Trees. D. Thiebaut CSC212 Fall 2014

Introduction to Trees. D. Thiebaut CSC212 Fall 2014 Introduction to Trees D. Thiebaut CSC212 Fall 2014 A bit of History & Data Visualization: The Book of Trees. (Link) We Concentrate on Binary-Trees, Specifically, Binary-Search Trees (BST) How Will Java

More information

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology Introduction Chapter 4 Trees for large input, even linear access time may be prohibitive we need data structures that exhibit average running times closer to O(log N) binary search tree 2 Terminology recursive

More information