Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

Slides:



Advertisements
Similar presentations
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
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.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees Nature Lover’s View Of A Tree root branches leaves.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Binary Tree Traversal Methods In a traversal of a binary tree, each element of the binary tree is visited exactly once. During the visit of an element,
Fundamentals of Python: From First Programs Through Data Structures
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.
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Trees Nature Lover’s View Of A Tree root branches leaves.
© University of Auckland Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
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.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
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.
Tree Data Structures. Introductory Examples Willliam Willliam BillMary Curt Marjorie Richard Anne Data organization such that items of information are.
درختها Trees ساختمان داده ها والگوريتمها مقايسه ليست خطي و درخت Linear lists are useful for serially ordered data. – (e 0, e 1, e 2, …, e n-1 ) – Days.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Trees, Binary Trees, and Binary Search Trees COMP171.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
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.
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
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.
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
Lab 4 Due date: March 29. Linked Representation Each binary tree node is represented as an object whose data type is binaryTreeNode. The space required.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
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 CMSC 341 Introduction to Trees Textbook sections:
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Trees Another Abstract Data Type (ADT)
Csc 2720 Instructor: Zhuojun Duan
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
Binary Trees, Binary Search Trees
Binary Tree Traversal Methods
Trees Another Abstract Data Type (ADT)
Binary Tree Traversal Methods
Trees Another Abstract Data Type (ADT)
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Binary Tree Traversal Methods
Binary Trees, Binary Search Trees
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Binary Tree Properties & Representation
Binary Tree Traversal Methods
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
Binary Trees, Binary Search Trees
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Binary and Other Trees CSE, POSTECH

2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week – Months in a year – Students in a class Trees are useful for hierarchically ordered data – Joe’s descendants – Corporate structure – Government Subdivisions – Software structure Read Examples

3 3 Joe’s Descendants What are other examples of hierarchically ordered data?

4 4 Definition of Tree A tree t is a finite nonempty set of elements One of these elements is called the root The remaining elements, if any, are partitioned into trees, which are called the subtrees of t.

5 5 Subtrees

6 6 Tree Terminology The element at the top of the hierarchy is the root. Elements next in the hierarchy are the children of the root. Elements next in the hierarchy are the grandchildren of the root, and so on. Elements at the lowest level of the hierarchy are the leaves.

7 7 Other Definitions Leaves, Parent, Grandparent, Siblings, Ancestors, Descendents Leaves = {Mike,AI,Sue,Chris} Parent(Mary) = Joe Grandparent(Sue) = Mary Siblings(Mary) = {Ann,John} Ancestors(Mike) = {Ann,Joe} Descendents(Mary)={Mark,Sue}

8 8 Levels and Height Root is at level 1 and its children are at level 2. Height = depth = number of levels level 1level 2level 3level 4

9 9 Node Degree Node degree is the number of children it has

10 Tree Degree Tree degree is the maximum of node degrees tree degree = 3 Do Exercises 3

11 Binary Tree A finite (possibly empty) collection of elements A nonempty binary tree has a root element and the remaining elements (if any) are partitioned into two binary trees They are called the left and right subtrees of the binary tree

12 Difference Between a Tree & a Binary Tree A binary tree may be empty; a tree cannot be empty. No node in a binary tree may have a degree more than 2, whereas there is no limit on the degree of a node in a tree. The subtrees of a binary tree are ordered; those of a tree are not ordered. a bc a cb - different when viewed as a binary tree - same when viewed as a tree

13 Binary Tree for Expressions Figure 11.5 Expression Trees

14 Binary Tree Properties 1.The drawing of every binary tree with n elements, n > 0, has exactly n-1 edges. (see its proof on pg. 426) 2.A binary tree of height h, h >= 0, has at least h and at most 2 h -1 elements in it. (see its proof on pg. 427)

15 Binary Tree Properties 3. The height of a binary tree that contains n elements, n >= 0, is at least  (log 2 (n+1))  and at most n. (see its proof on pg. 427) minimum number of elements maximum number of elements

16 Full Binary Tree A full binary tree of height h has exactly 2 h -1 nodes. Numbering the nodes in a full binary tree –Number the nodes 1 through 2 h -1 –Number by levels from top to bottom –Within a level, number from left to right (see Fig. 11.6)

17 Node Number Property of Full Binary Tree Parent of node i is node  (i/2) , unless i = 1 Node 1 is the root and has no parent

18 Left child of node i is node 2i, unless 2i > n, where n is the total number of nodes. If 2i > n, node i has no left child. Node Number Property of Full Binary Tree

19 Right child of node i is node 2i+1, unless 2i+1 > n, where n is the total number of nodes. If 2i+1 > n, node i has no right child. Node Number Property of Full Binary Tree

20 Complete Binary Tree with N Nodes Start with a full binary tree that has at least n nodes Number the nodes as described earlier. The binary tree defined by the nodes numbered 1 through n is the n-node complete binary tree. A full binary tree is a special case of a complete binary tree See Figure 11.7 for complete binary tree examples See Figure 11.8 for incomplete binary tree examples

21 Example of Complete Binary Tree Complete binary tree with 10 nodes. Same node number properties (as in full binary tree) also hold here.

22 Binary Tree Representation Array representation Linked representation

23 Array Representation of Binary Tree The binary tree is represented in an array by storing each element at the array position corresponding to the number assigned to it.

24 Incomplete Binary Trees Complete binary tree with some missing elements Fig Incomplete binary trees

25 Right-Skewed Binary Tree An n node binary tree needs an array whose length is between n+1 and 2 n. Right-skewed binary tree wastes the most space What about left-skewed binary tree?

26 Linked Representation of Binary Tree The most popular way to present a binary tree Each element is represented by a node that has two link fields (leftChild and rightChild) plus an element field (see Figure 11.10) Each binary tree node is represented as an object whose data type is binaryTreeNode (see Program 11.1) The space required by an n node binary tree is n * sizeof(binaryTreeNode)

27 Linked Representation of Binary Tree

28 Node Class For Linked Binary Tree template class binaryTreeNode { private: T element; binaryTreeNode *leftChild, *rightChild; public:// 3 constructors binaryTreeNode() { leftChild = rightChild = NULL; } // no params binaryTreeNode(const T& theElement) {// element param only element = theElement; leftChild = rightChild = NULL; } binaryTreeNode(const T& theElement, binaryTreeNode *l, binaryTreeNode *r) {// element + links params element = theElement; leftChild = l; rightChild = r; } }

29 Common Binary Tree Operations Determine the height Determine the number of nodes Make a copy Determine if two binary trees are identical Display the binary tree Delete a tree If it is an expression tree, evaluate the expression If it is an expression tree, obtain the parenthesized form of the expression

30 Binary Tree Traversal Many binary tree operations are done by performing a traversal of the binary tree In a traversal, each element of the binary tree is visited exactly once During the visit of an element, all actions (make a copy, display, evaluate the operator, etc.) with respect to this element are taken

31 Binary Tree Traversal Methods Preorder  The root of the subtree is processed first before going into the left then right subtree (root, left, right). Inorder  After the complete processing of the left subtree the root is processed followed by the processing of the complete right subtree (left, root, right). Postorder  The root is processed only after the complete processing of the left and right subtree (left, right, root). Level order  The tree is processed by levels. So first all nodes on level i are processed from left to right before the first node of level i+1 is visited

32 Preorder Traversal template // Program 11.2 void preOrder(binaryTreeNode *t) { if (t != NULL) { visit(t);// visit tree root preOrder(t->leftChild);// do left subtree preOrder(t->rightChild);// do right subtree }

33 Preorder Example (visit = print) a b d g h e i c f j

34 Preorder of Expression Tree / * + a b - c d + e f Gives prefix form of expression.

35 Inorder Traversal template // Program 11.3 void inOrder(binaryTreeNode *t) { if (t != NULL) { inOrder(t->leftChild);// do left subtree visit(t);// visit tree root inOrder(t->rightChild);// do right subtree }

36 Inorder Example (visit = print) g d h b e i a f j c

37 Inorder by Projection (Squishing)

38 Inorder of Expression Tree Gives infix form of expression, which is how we normally write math expressions. What about parentheses? See Program 11.6 for parenthesized infix form Fully parenthesized output of the above tree?

39 Postorder Traversal template // Program 11.4 void postOrder(binaryTreeNode *t) { if (t != NULL) { postOrder(t->leftChild);// do left subtree postOrder(t->rightChild);// do right subtree visit(t);// visit tree root }

40 Postorder Example (visit = print) g h d i e b j f c a

41 Postorder of Expression Tree a b + c d - * e f + / Gives postfix form of expression.

42 Level Order Traversal template // Program 11.7 void levelOrder(binaryTreeNode *t) {// Level-order traversal of *t. linkedQueue *> Q; while (t != NULL) { visit(t); // visit t if (t->leftChild) q.push(t->leftChild); // put t's children if (t->rightChild) q.push(t->rightChild); // on queue try {t = q.front();} // get next node to visit catch (queueEmpty) {return;} q.pop() }

43 Level Order Example (visit = print) Add and delete nodes from a queue Output: a b c d e f g h i j

44 Space and Time Complexity The space complexity of each of the four traversal algorithm is O(n) The time complexity of each of the four traversal algorithm is  (n)

45 Binary Tree ADT, Class, Extensions See ADT 11.1 for Binary Tree ADT definition See Program 11.8 for Binary Tree abstract class See Programs for operation implementations Read Sections