Tree Traversals, TreeSort 20 February 2003. 2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
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.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
KNURE, Software department, Ph , N.V. Bilous Faculty of computer sciences Software department, KNURE The 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 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
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.
Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; } 
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
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.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
Tree Data Structures.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Starting at Binary Trees
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.
Discrete Structures Trees (Ch. 11)
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
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.
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.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
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.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
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. 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.
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Week 6 - Wednesday CS221.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Binary Trees, Binary Search Trees
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Introduction to Trees Chapter 6 Objectives
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Tree Traversals, TreeSort 20 February 2003

2 Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C * (E - F) + –* BA–C FE

3 Formal Definition of Tree An empty structure is an empty tree. If t 1, t 2, …, t k are disjoint trees, then the structure whose root has as its children the roots of t 1, t 2, …, t k, is also a tree. Only structures generated by rules 1 and 2 are trees.

4 Observations The recursive fashion in which a tree is defined provides a strong hint that most tree processing algorithms will also be recursive. Most operations on the tree data structure are closely linked to the hierarchical relationship among nodes for that particular tree.

5 Tree Properties The minimal tree is an empty tree and has no nodes. If not empty, a tree consists of nodes and branches. Each node must be reachable from the root through a unique sequence of branches, called a path. The number of branches in a path is the length of the path. The level of a node is the length of the path from the root to the node plus one, which is the number of nodes in the path. The height of a nonempty tree is the maximum level of a node in the tree. [The empty tree has height 0 (by definition), and a single node is a tree of height 1. This is the only case in which a node is both the root and a leaf.]

6 Linked Implementation of a Binary Tree Since each node in a binary tree has at most two children, a node in a linked implementation has two pointer members, one for each child, and one or more members for storing data. –*+–ABCEF

7 Tree Traversals Move through all the nodes in a tree visiting them one at a time. For lists we traverse nodes by visiting successors. We have more freedom in traversing trees.

8 Preorder Binary Tree Traversal In a preorder traversal, the nodes will be visited in the following order: 1. First, process the root node. 2. Then, recursively visit all nodes in the left subtree. 3. Finally, recursively visit all nodes in the right subtree. The traversal method works recursively, that is, once the root of the tree is processed, we go to the root of the left subtree, and then to the root of the left subtree of the left subtree, and so on until we can go no farther. + – A B * C – E F (prefix form)

9 Inorder Binary Tree Traversal The inorder traversal of a binary tree visits nodes in the following order. 1. First, recursively visit all nodes in the left subtree. 2. Then, process the root node. 3. Finally, recursively visit all nodes in the right subtree. A – B + C * E – F (infix form)

10 Postorder Binary Tree Traversal The postorder traversal of a binary tree visits the nodes in the following order: 1. First, recursively visit all nodes in the left subtree of the root node. 2. Then, recursively visit all nodes in the right subtree of the root node. 3. Finally, process the root. A B – C E F – * + (postfix form)

11 General Trees So far we have considered binary trees. There are many applications with hierarchical relationships in which a parent may have an unrestricted number of children. A tree that represents such structures is called a general tree. For example, consider the general tree of the next slide. – In this structure, A 1 is the first child of A, with A 2, A 3, and A 4 as A 1 ’s siblings. Similarly, A 11 is the first child of A 1, with A 12 and A 13 as A 1 ’s siblings

12 A General Tree A 413 A 411 A 412 A 41 A 32 A 31 A 13 A 12 A 11 A4A4 A3A3 A2A2 A1A1 A

13 Linked Representation We can convert any general tree (or forest) into a binary tree. We can use the linked representation of the converted binary tree to represent this general tree. Since only two pointers are associated with each node of the binary tree, we do this by defining one of the pointers to be a pointer to the leftmost child of a node in the general tree, while the other pointer is used to identify the next sibling of that node in the general tree.

14 Converting any Forest to a Tree Connect the roots of the trees via right links As we descend the levels of the trees in the forest, make the left pointer point to the first child of a node and the right pointer of the first child begins a linked list of that child’s siblings. Rotate the entire structure by 45º

15 Example

16 Example (first child/sibling links) X X X X X X

17 Example

18 Example (rotate 45º)

19 Tree Sort Algorithm: – create empty BST – insert (ordered) each element into BST – Inorder traverse BST – (destroy BST)

20 Tree Sort Advantages: – n elements, lg(n) insert  O(nlg(n)) sort – don’t need to have a fixed set of data, nodes can be inserted and deleted dynamically Disadvantages: – additional overhead of entire Tree – if data arrives in order or reverse order degenerate to O(n 2 ) behavior just like QuickSort