CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.

Slides:



Advertisements
Similar presentations
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Advertisements

Binary Trees, Binary Search Trees COMP171 Fall 2006.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
CS 171: Introduction to Computer Science II
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
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.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1 Chapter 7 Trees. 2 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.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Marc Smith and Jim Ten Eyck
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
Binary Trees Chapter 6.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
ICOM 4035 – Data Structures Lecture 13 – Trees ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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,
Chapter 10-A Trees Modified
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Compiled by: Dr. Mohammad Omar Alhawarat
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
Discrete Mathematics Chapter 5 Trees.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
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.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Section 8.1 Trees.
Data Structures & Algorithm Design
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Chapter 21: Binary Trees.
Chapter 7. Trees & Binary Trees
Trees.
Trees Definitions Implementation Traversals K-ary Trees
Binary Trees, Binary Search Trees
Trees.
Chapter 20: Binary Trees.
Binary Trees.
General Tree Concepts Binary Trees
Binary Trees, Binary Search Trees
Presentation transcript:

CHAPTER 12 Trees

2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs. A tree with N nodes has N -1 links Parents and children: The first node in the ordered pair is called a parent of the second node, called a child Each child may have only one parent – this is the defining characteristics of a tree. Root: One node is designated to be the root of the tree – it does not have a parent Leaf: Nodes that don’t have children are called leaves. Internal node: A node that is not the root and has at least one child is called an internal node

3 Tree terminology

4 A root Leaves or terminal nodes Child (of root) Parent (of P, U, T, E) M C O L N D R P UTE

5 More Definitions Path: A sequence of nodes such that each next node is a child of the previous, Any node below another node and on a path from that node is called a descendant of that node Any node above another node on a connecting path from the root to that node is called an ancestor of that node All children of the same node are called siblings A tree that limits each node to no more than n children is called an n-ary tree

6 More Definitions Path length: the number of the links in the path Level (depth) of a node : The length of the path from the root to that node The root is considered to be level 0, the children of the root are at level 1, the grandchildren of the root are at level 2, and so on Height of a node: The length of the longest path from that node to a leaf Height or order of a tree is the length of the longest path from the root to a leaf

7 Path length and level

8 Balanced trees Subtree: a tree rooted at some internal node Balanced tree: For each node the height of its subtrees differs by not more than 1 link A tree is considered to be balanced if all of the leaves of the tree are at roughly the same depth

9 Balanced and unbalanced trees

10 Complete and perfect trees Complete tree: The heights of all leaves differ by at most 1 link. All nodes have same number of children except the leaves (0 children) and possibly the rightmost internal node with maximum depth. Perfect tree: All nodes have same number of children All leaves have same height

11 Some complete trees

12 Tree Implementations Tree nodes with references to: The object stored in the node Each child The parent (optional) Arrays Two dimensional, N x N (N – number of nodes) Two dimensional, M x 2 (M – number of links) One dimensional (complete trees), N One dimensional – any tree, N

13 Binary Tree nodes public class BinaryTreeNode { protected T element; protected BinaryTreeNode left, right; BinaryTreeNode (T object) // constructor { element = object; left = null; right = null; } …… }

14 Binary Tree: Linked Implementation public class LinkedBinaryTree { protected int count; protected BinaryTreeNode root; public LinkedBinaryTree() // empty tree { count = 0; root = null; } public LinkedBinaryTree( T element) // root specified { count = 1; root = new BinaryTreeNode (element); }

15 Tree implementations with arrays The nodes of the tree have to be numbered A.Two dimensional N x N Array[i][j] = 1 if j is a child of i = 0 otherwise B.Two dimensional M x 2, M – number of links Each row contains a pair representing a link

16 Example – two dimensional arrays Let the tree be given by the following pairs ( 6 nodes, 5 links): (1,2),(1,3),(1,4),(2,5),(2,6) A:B: more efficient in terms of space very inefficient implementation In terms of space

17 Tree implementations with arrays One dimensional arrays: A.Complete binary trees Array[0] contains the root Array[2*n + 1] contains the left child of node Array[n] Array[2*n+2] contains the right child of node Array[n] B.Any tree: the indexes in the array correspond to the nodes Array[n] contains the parent of node n Array[0] contains -1, i.e. no parent

18 Example – one dimensional arrays A: Complete binary tree Let the tree be given by the following pairs ( 7 nodes): (a,b),(a,c),(b,e),(b,f),(c,d), (c,m), (e,k) Array:a b c e f d m the left child of c: array[2] is in Indexes array[2*2 + 1], this is d Quick access to children and parents, but the tree has to be complete. What can be done if it is not complete? B: Any tree. Nodes are numbers from 0 Let the tree be: (0,1),(0,2),(0,3),(2,4),(2,5),(3,6) Array: indexes Quick access from any node to the root. Used in game programming

19 Traversal of Binary Trees Preorder Inorder Postorder Levelorder – nodes are visited from left to right starting with level 0

20 Preorder traversal Visit the root Visit the left subtree Visit the right subtree preorder(tree) { if (tree == null) return else visit (tree) preorder (tree.left) preorder (tree.right) }

21 Inorder traversal Visit the left subtree Visit the root Visit the right subtree inorder(tree) { if (tree == null) return else inorder (tree.left) visit (tree) inorder (tree.right) }

22 Postorder traversal Visit the left subtree Visit the right subtree Visit the root postorder(tree) { if (tree == null) return else postorder (tree.left) postorder (tree.right) visit (tree) }

23 C R L E T N O U M P A D C O M P U T E R L A N D P M U O C T E L R N A D P U M O L N D A R E T C