1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.

Slides:



Advertisements
Similar presentations
Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Advertisements

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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
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, Binary Trees, and Binary Search Trees COMP171.
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.
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.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
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.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
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 Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R2. Binary Search Trees.
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.
Compiled by: Dr. Mohammad Omar Alhawarat
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.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
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.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
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.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
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.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
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.
Foundation of Computing Systems Lecture 4 Trees: Part I.
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.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
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
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree.
Section 8.1 Trees.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Binary Trees, Binary Search Trees
CS223 Advanced Data Structures and Algorithms
Binary Trees, Binary Search Trees
Trees.
Chapter 20: Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

1 Trees

2 Trees Trees. Binary Trees Tree Traversal

3 Trees

4 Trees

5 Tree Terminology A non-empty tree has a root node (e.g.node a). Every other node can be reached from the root via a unique path (e.g. a-d-f). a is the parent of nodes b and c because there is an edge going from a down to each. Moreover, b and c are children of a. dagbecf root parent of g child of a

6 Tree Terminology The descendant of a is (e and c), nodes that can be reached by paths from a. The ancestor of e is (a and c), nodes found on the path from e to a. Nodes (b, d, and e) are leaf nodes (they have no children). Each of the nodes a and c has at least one child and is an internal node. caebd Siblings

7 Trees

8 Binary Trees Can be implemented using arrays, structs and pointers Used in problems dealing with: Searching Hierarchy Ancestor/descendant relationship Classification

9 Tree Terminology Each node in the tree (except leaves) may have one or more subtrees For a tree with n nodes there are n-1 edges abdecfg

10 The Binary Tree A binary tree is a tree in which a parent has at most two children. It consists of a root and two disjoint subtrees (left and right). The root is at level (L = 1) and the height h = maximum level The maximum number of nodes in level L is abdecf Level 1 Level 2 Level 3 A binary tree of height h = 3 Left Right g

11 The Full Binary Tree A binary tree is full iff the number of nodes in level L is 2 L-1 abdecfg

12 The Full Binary Tree A full binary tree of height h has n nodes, where

13 The Balanced Tree A balanced binary tree has the property that the heights of the left and right subtrees differ at most by one level. i.e. |h L – h R | ≤ 1 A Full tree is also a balanced tree hLhL hRhR

14 A binary tree is Complete iff the number of Nodes at level 1 <= L <= h-1 is 2 L-1 and leaf nodes at level h occupy the leftmost positions in the tree i.e. all levels are filled except the rightmost of the last level. Complete Binary Tree Missing Leaves

15 Complete Binary Tree

16 Complete Binary Tree A complete binary tree can be efficiently implemented as an array, where a node at index i has children at indexes 2i+1 and 2i+2 and a parent at index (i-1)/2 DE BC A ABCDE

17 Complete Binary Tree

18 A binary tree is a recursive structure e.g. it can be defined recursively as: if (not empty tree) 1. It has a root 2. It has a (Left Subtree) 3. It has a (Right Subtree) Recursive structure suggests recursive processing of trees (e.g. Traversal) Binary Tree as a Recursive Structure bc a de f

19 Binary Tree as a Recursive Structure b,d,ec,f a de f a,b,c,d,e,f bc Empty

20 2. Tree Traversal Traversal is to visit every node ( to display, process, …) exactly once It can be done recursively There are 3 different binary tree traversal orders: Pre-Order: Root is visited before its two subtrees In-Order: Root is visited in between its two subtrees Post-Order:Root is visited after its two subtrees

21 Pre-Order Traversal Algorithm: PreOrder ( tree ) { if ( not empty tree) { Visit (root); PreOrder (left subtree); PreOrder (right subtree); } The resulting visit order = {a} {b, d, e} {c, f } bc a de f

22 Pre-Order Traversal Pre-Order Traversal is also called Depth-First traversal

23 In-Order Traversal Algorithm: InOrder ( tree ) { if ( not empty tree) { InOrder (left subtree); Visit (root); InOrder (right subtree); } The resulting visit order = {d, b, e} {a} {f, c } bc a de f

24 Post-Order Traversal Algorithm: PostOrder ( tree ) { if ( not empty tree) { PostOrder (left subtree); PostOrder (right subtree); Visit (root); } The resulting visit order = {d, e, b} {f, c } {a} bc a de f

25 Example: Expression Tree The expression A – B * C + D can be represented as a tree. In-Order traversal gives: A – B * C + D This is the infix representation Pre-Order traversal gives: + - A * B C D This is the prefix representation Post-Order traversal gives: A B C * - D + This is the postfix (RPN) representation - D + A* B C

26 Exercise : Assume there is a binary tree with the following traversal Preorder traversal sequence: F, B, A, D, C, E, G, I, H Inorder traversal sequence: A, B, C, D, E, F, G, H, I Can you draw the binary tree ?

27 Binary Search Tree :

28 Binary Search Tree :

29 Binary Search Tree :

30 Binary Search Tree :

31 Inserting in Binary Search Tree Insert (tree, new_item) if (tree is empty) insert new item as root; else if (root key matches new_item) skip insertion; (duplicate key) else if (new_item is smaller than root) insert in left sub-tree; elseinsert in right sub-tree;

32 Inserting in Binary Search Tree Insert: 40,20,10,50,65,45,30

33 Deleting from a Binary Search Tree

34 Deleting from a Binary Search Tree

35 Deleting from a Binary Search Tree

36 Deleting from a Binary Search Tree

37 Deleting from a Binary Search Tree

38 Deleting from a Binary Search Tree

39 Deleting from a Binary Search Tree

40 Deleting from a Binary Search Tree

41 Deleting from a Binary Search Tree

42 Deleting from a Binary Search Tree

43 Deleting from a Binary Search Tree