Data Structures and Algorithms for Information Processing

Slides:



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

Data Structures and Algorithms1 Trees The definitions for this presentation are from from: Corman, et. al., Introduction to Algorithms (MIT Press), Chapter.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Binary Tree Terminology Linear versus hierarchical data Tree – connected graph with no cycles Child Parent Descendant Sibling Ancestor Leaf vs. internal.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Data Structures.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
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.
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
Saturday, 04 Apr 2010 University of Palestine Computer Science II Trees.
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 (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.
Compiled by: Dr. Mohammad Omar Alhawarat
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. 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.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.
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.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
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 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 CMSC 341 Introduction to Trees Textbook sections:
(c) University of Washington20-1 CSC 143 Java Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Binary Trees.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Data Structures and Design in Java © Rick Mercer
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Binary Trees.
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Recursive Objects (Part 4)
Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos
University of Palestine
Data Structures and Algorithms
Week 6 - Wednesday CS221.
Trees.
Tree.
Csc 2720 Instructor: Zhuojun Duan
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
CSC 172– Data Structures and Algorithms
Binary Trees, Binary Search Trees
CS223 Advanced Data Structures and Algorithms
COSC2100: Data Structures and Algorithms
Binary Trees.
General Trees & Binary Trees
Find in a linked list? first last 7  4  3  8 NULL
Binary Trees.
Trees.
Binary Trees.
Trees Lecture 9 CS2110 – Fall 2009.
Lecture 36 Section 12.2 Mon, Apr 23, 2007
General Trees & Binary Trees
Binary Trees, Binary Search Trees
Trees.
Binary Trees.
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
CSC 143 Java Trees.
CS210- Lecture 9 June 20, 2005 Announcements
Chapter 20: Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Data Structures and Algorithms for Information Processing Lecture 5: Trees Lecture 5: Trees

Binary Trees A binary tree has nodes, similar to nodes in a linked list structure. Data of one sort or another may be stored at each node. Each node is either a leaf, having no children, or an internal node, with one or two children In many ways, a tree is like the other structures you have seen: A tree consists of nodes, and each node can contain data of one sort or another. Lecture 5: Trees

Binary Trees Lecture 5: Trees

Some Terminology Root is the unique node with no parent Leaves or terminals are nodes with no children A subtree is a node together with all its descendents Level of a node is number of nodes on path from the node to root Lecture 5: Trees

Arithmetic Expressions A*(((B+C)*(D*E))+F) Example use of Trees Arithmetic Expressions A*(((B+C)*(D*E))+F) Each internal node labeled by an operator Each leaf labeled by a variable or numeric value Tree determines a unique value Lecture 5: Trees

Example Use of Trees Representing Chinese Characters Characters composed hierarchically into boxes Boxes can be oriented left-right, top-down, or outside-inside Each leaf labeled by one of 300-400 radicals Lecture 5: Trees

Binary vs. Binary-Unary Important distinction : Sometimes binary trees are defined to allow internal nodes with one or two children. There is a big difference... Lecture 5: Trees

Counting Trees The number of binary trees with internal nodes is There is no such formula if we allow unary internal nodes! Lecture 5: Trees

Exercise Write efficient Java functions int numBTrees(int n) int numBUTrees(int n) that return the number of binary trees and binary/unary trees, respectively. Lecture 5: Trees

Properties of Trees There is exactly one path connecting any two nodes Any two nodes have a least common ancestor A tree with N internal nodes has N+1 external nodes (easy to prove by induction) Lecture 5: Trees

The Best Trees A binary tree is full if internal nodes fill every level, except possibly the last. The height of a full binary tree with internal nodes is Lecture 5: Trees

Complete Binary Trees A complete tree is one having levels and leaves Complete trees have a simple implementation using arrays (How?) Lecture 5: Trees

Class for Binary Nodes public class BTNode { private Object data; private BTNode left; private BTNode right; ... Lecture 5: Trees

Class for Binary Nodes public BTNode(Object obj, BTNode l, BTNode r) { data = obj; left = l; right= r; } public boolean isLeaf() return (left == null) && (right == null); ... Lecture 5: Trees

Copying Trees public static BTNode treeCopy(BTNode t) { if (t == null) return null; else BTNode leftCopy = treeCopy(t.left); BTNode rightCopy = treeCopy(t.right); return new BTNode(t.data, leftCopy, rightCopy); } Lecture 5: Trees

Tree Traversals Preorder traversal P M L E S R A A T E E Lecture 5: Trees

Tree Traversals Inorder traversal P M L E S R A A T E E Lecture 5: Trees

Tree Traversals Postorder traversal P M L E S R A A T E E Lecture 5: Trees

Tree Traversals Level order traversal P M L E S R A A T E E Lecture 5: Trees

Preorder Traversal Process the root Process the nodes in the left subtree Process the nodes in the right subtree Lecture 5: Trees

Preorder Print public void preorderPrint() { System.out.println(data); if (left != null) left.preorderPrint(); if (right != null) right.preorderPrint(); } Lecture 5: Trees

Inorder Traversal Profess the nodes in the left subtree Process the root Process the nodes in the right subtree Lecture 5: Trees

Inorder Print public void preorderPrint() { if (left != null) left.preorderPrint(); System.out.println(data); if (right != null) right.preorderPrint(); } Lecture 5: Trees