Trees-part1. Objectives Understand tree terminology Understand and implement tree traversals Define the binary search tree property Implement binary search.

Slides:



Advertisements
Similar presentations
Introduction to Trees Chapter 6 Objectives
Advertisements

Binary Search Trees. John Edgar  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
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.
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.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies.
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
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.
Marc Smith and Jim Ten Eyck
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
Binary Trees Chapter 6.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Chapter 11 A Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 A-2 Terminology A tree consists of vertices and edges, –An edge connects to.
COSC2007 Data Structures II
Tree.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Course: Programming II - Abstract Data Types Slide Number 1 The ADT Binary Tree Definition The ADT Binary Tree is a finite set of nodes which is either.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Trees CSCI Objectives Define trees as data structures Define the terms associated with trees Discuss the possible implementations of trees Analyze.
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.
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 A connected graph that contains no simple circuits is called a tree. Because a tree cannot have a simple circuit, a tree cannot contain multiple.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
Chapter 10-A Trees Modified
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. 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:
CS 1031 Trees A Quick Introduction to Graphs Definition of Trees Rooted Trees Binary Trees Binary Search Trees.
Data Structures TREES.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
CSE 326: Data Structures Lecture #6 From Lists to Trees Henry Kautz Winter 2002.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 16: Trees Announcements 1.Programming project.
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.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
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.
1 Lecture 14: Trees & Insertion Sort CompSci 105 SS 2006 Principles of Computer Science.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
1 CMSC 341 Introduction to Trees Textbook sections:
Trees A non-linear implementation for collection classes.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
Binary Trees.
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Csc 2720 Instructor: Zhuojun Duan
CMSC 341 Introduction to Trees.
Trees What is a Tree? Tree terminology Why trees?
Binary Trees, Binary Search Trees
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
TREES General trees Binary trees Binary search trees AVL trees
CMPT 225 Binary Search Trees.
Trees.
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Binary Trees, Binary Search Trees
Binary Trees.
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
Tree.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Binary Trees, Binary Search Trees
Presentation transcript:

Trees-part1

Objectives Understand tree terminology Understand and implement tree traversals Define the binary search tree property Implement binary search trees

Trees A set of nodes with a single starting point  called the root Each node is connected by an edge to some other node A tree is a connected graph  There is a path to every node in the tree There are no cycles in the tree.  It can be proved by MI that a tree has one less edge than the number of nodes. Usually depicted with the root at the top

Is it a Tree? yes! NO! All the nodes are not connected NO! There is a cycle and an extra edge (5 nodes and 5 edges) yes! (but not a binary tree) yes! (it’s actually the same graph as the blue one) – but usually we draw tree by its “levels”

Examples of trees directory structure family trees:  all descendants of a particular person  all ancestors born after year 1800 of a particular person evolutionary tress (also called phylogenetic trees) algebraic expressions

Examples of trees Binary trees that represent algebraic expressions

Tree Relationships If there is an edge between two nodes u and v, and u is “above” v in the tree (closer to the root), then v is said to be a child of u, and u the parent of v  A is the parent of B, C and D This relationship can be generalized (transitively)  E and F are descendants of A  D and A are ancestors of G  B, C and D are siblings A BCD GEF

C Tree Terminology Example A B C D GEF EFG leaves: C,E,F,G A leaf is a node with no children

C A B C D GEF EFGDGA path from A to D to G A path [a branch] is a sequence of nodes v 1 … v n where v i is a parent of v i+1 (1  i  n-1) [and v 1 is a root and v n is a leaf]

C A B C D GEF EFG subtree rooted at B A subtree is any node in the tree along with all of its descendants

A BC GDE left subtree of A H IJ F right subtree of C right child of A A binary tree is a tree with at most two children per node The children are referred to as left and right (i.e., children are usually ordered) We can also refer to left and right subtrees of a node

Prove that the number of edges in a tree is one less than the number of nodes. Prove by induction on the number of nodes. Base of induction is true for a tree with one node. Induction hypothesis: the theorem is true for any tree with M < N number of nodes. Induction step: prove that the theorem is true for a tree with N nodes TLTL TRTR  Let N and E be the number of nodes and edges of tree T respectively. Let N L (N R ) and E L (E R ) be the number of nodes and edges of T L (T R ) respectively.  N=N L +N R +1 and E=E L +E R +2 (there is no edge running between T R and T L )  Based on induction hypothesis: E L =N L -1 and E R =N R -1 (because T R and T L have less than N nodes).  Hence E=N L -1 + N R = N L +N R = N - 1

Measuring Trees The height of a node v is the number of nodes on the longest path from v to a leaf  The height of the tree is the height of the root, which is the number of nodes on the longest path from the root to a leaf The depth of a node v is the number of nodes on the path from the root to v  This is also referred to as the level of a node Note that there are slightly different formulations of the height of a tree  Where the height of a tree is said to be the length (the number of edges) on the longest path from node to a leaf

A BC GDE H IJ F A B height of node B is 3 height of the tree is 4 The height of a node v is the number of nodes on the longest path from v to a leaf  The height of the tree is the height of the root, which is the number of nodes on the longest path from the root to a leaf

A BC GDE H IJ F E depth of node E is 3 The depth of a node v is the number of nodes on the path from the root to v  This is also referred to as the level of a node

A BC GDE H IJ F level 2 level 3 level 4 level 1 All the nodes in the same distance from root is called to be in the same level. The root of the tree is on level 1 Note that there are slightly different formulations of the height of a tree  Where the height of a tree is said to be the length (the number of edges) on the longest path from node to a leaf

Representation of a binary tree is very much like linked list. Each node has two references one to the right child and one to the left child. item leftChildrightChild A B C D TreeNode

Representation of binary trees public class TreeNode { private Comparable item; private TreeNode leftChild; private TreeNode rightChild; public TreeNode(Comparable newItem) { // Initializes tree node with item and no children (a leaf). item = newItem; leftChild = null; rightChild = null; } // end constructor public TreeNode(T newItem, TreeNode left, TreeNode right) { // Initializes tree node with item and // the left and right children references. item = newItem; leftChild = left; rightChild = right; } // end constructor public Comprable getItem() { // Returns the item field. return item; } // end getItem

public void setItem(Comprable newItem) { // Sets the item field to the new value newItem. item = newItem; } // end setItem public TreeNode getLeft() { // Returns the reference to the left child. return leftChild; } // end getLeft public void setLeft(TreeNode left) { // Sets the left child reference to left. leftChild = left; } // end setLeft public TreeNode getRight() { // Returns the reference to the right child. return rightChild; } // end getRight public void setRight(TreeNode right) { // Sets the right child reference to right. rightChild = right; } // end setRight } // end TreeNode

Representing a tree TreeNode root=new TreeNode(new Integer(2)); root.setLeft( new TreeNode(new Integer(5), new TreeNode(new Integer(4)), new TreeNode(new Integer(1))));

Java Generics: Generic Classes ADT developed in this text relied upon the use of Object class or the Comparable interface. Problems with this approach  Items of any type could be added to same ADT instance  ADT instance returns objects Cast operations are needed: e.g Integer I = (Integer) stack.pop(); May lead to class-cast exceptions Avoid these issues by using Java generics  To specify a class in terms of a data-type parameter

Example – Nodes with a type //defining a generic class. T is the input data type public class Node { private T item; private Node next; public Node (T x, Node n){ item = x; next = n; } public T getItem() { return item; } //creating an object of a generic class. Node intNode= new Node (new Integer(1), null); Node stringNode= new Node (new String (“Jhon”), intNode);

Things you cannot do with generics When creating an object of a generic class the input data type must be a defined class (not a primitive data type) Node intNode=new Node (1, null); Java does not allow generic types to be used in array declaration. T[] items = new T[10]; The alternative is to use either the ArrayList or Vector class in Java. Vector items = new Vector (); ArrayList items = new ArrayList ();

public class Stack { private ArrayList items; private int top, capacity; public Stack(int c){ items = new ArrayList (c); top = -1; capacity = c; } public boolean isEmpty(){ return top == -1; } public T pop() throws StackException{ if(isEmpty()) throw new StackException("Stack is empty"); return items.get(top--); } public void push (T x) throws StackException{ if(top==capacity-1) throw new StackException("Stack is full"); items.add(++top, x); } Stack Implementation using Java generic class.

public static void main(String args[]){ Stack s = new Stack (100); for(int i=0; i<100; i++) s.push(new Integer(i)); for(int i=0; i<100; i++){ Integer x = s.pop();//no casting is required System.out.println(x); }