Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
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.
Computer Science C++ High School Level By Guillermo Moreno.
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
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.
Binary Tree B G E D I H F c A Binary tree
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
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.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
BINARY TREES && TREE TRAVERSALS. DEFINITION : Binary Tree A binary tree is made of nodes Each node contains –a "left" pointer -- left child –a "right"
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
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.
Trees CS212 & CS-240 D.J. Foreman. What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
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 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.
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.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Data Structures & Algorithm Analysis Muhammad Hussain Mughal Trees. Binary Trees. Reading: Chap.4 ( ) Weiss.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Binary Search Trees Data Structures Ananda Gunawardena
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
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.
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)
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Lecture 9 Binary Trees Trees General Definition Terminology
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
1 CMSC 341 Introduction to Trees Textbook sections:
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
Recursive Objects (Part 4)
Binary Search Tree (BST)
Tree.
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Data Structures & Algorithm Design
Binary Tree and General Tree
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Trees.
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Binary Trees, Binary Search Trees
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: 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:

Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of Computer Science California State University, Fresno Fall 2006

Introduction to Data Structure, Fall 2006 Slide- 2 California State University, Fresno Tree Structures

Introduction to Data Structure, Fall 2006 Slide- 3 California State University, Fresno Tree Structures

Introduction to Data Structure, Fall 2006 Slide- 4 California State University, Fresno Tree Structures

Introduction to Data Structure, Fall 2006 Slide- 5 California State University, Fresno Tree Node Level and Path Length

Introduction to Data Structure, Fall 2006 Slide- 6 California State University, Fresno Binary Tree Definition A binary tree T is a finite set of nodes with one of the following properties: –(a) T is a tree if the set of nodes is empty. (An empty tree is a tree.) –(b) The set consists of a root, R, and exactly two distinct binary trees, the left subtree T L and the right subtreeT R. The nodes in T consist of node R and all the nodes in T L and T R.

Introduction to Data Structure, Fall 2006 Slide- 7 California State University, Fresno Density of Binary Trees For a tree with depth d, the maximum number of nodes on each level n varies from 1 (if there is at most one node having one leaf node (or child) to 2 d (if all nodes have two leaf nodes (or children) Density is a measure of the size of a tree (number of nodes) relative to the depth of the tree. If density high, pack more nodes near the root. Search, insert, delete will be convenient. If density low, many nodes will be far from the root. Search, insert, delete will be difficult.

Introduction to Data Structure, Fall 2006 Slide- 8 California State University, Fresno Selected Samples of Binary Trees Density = 9/3 = 3 Density = 5/4 = 1.25

Introduction to Data Structure, Fall 2006 Slide- 9 California State University, Fresno Selected Samples of Binary Trees E D C B A Tree B Size 5 Depth 4 Degenerate tree: A tree in which there is a single leaf node and each interior (internal) node has only one child. Equivalent to linked list.

Introduction to Data Structure, Fall 2006 Slide- 10 California State University, Fresno Tree Node Level and Path Length – Complete Tree A tree in which all leaf nodes are at some depth n or n-1, and all leaves at depth n are toward the left.

Introduction to Data Structure, Fall 2006 Slide- 11 California State University, Fresno Tree Node Level and Path Length – Complete Tree

Introduction to Data Structure, Fall 2006 Slide- 12 California State University, Fresno Tree Node Level and Path Length – Complete Tree

Introduction to Data Structure, Fall 2006 Slide- 13 California State University, Fresno Tree Node Level and Path Length – Complete Tree

Introduction to Data Structure, Fall 2006 Slide- 14 California State University, Fresno Density of Complete Binary Trees For a complete binary tree with depth d At levels from 0 to d-1, since all nodes have two leaf nodes, the total number of nodes is …+ 2 d-1 = 2 d - 1 For level d, the number of nodes varies from 1 to 2 d. So, the total size of a tree varies from: 2 d -1+1 to 2 d -1+2 d, or 2 d to 2 d+1 -1

Introduction to Data Structure, Fall 2006 Slide- 15 California State University, Fresno Density of Complete Binary Trees Let the number of nodes of the complete tree is n, then 2 d <= n <= 2 d+1 -1 < 2 d+1 That is d <= logn < d+1 What does this mean? d = int(logn) Why is it true only for complete tree?

Introduction to Data Structure, Fall 2006 Slide- 16 California State University, Fresno Binary Tree Traversal – Preorder 1.Visit the node 2.Traverse the left subtree (“go left”). 3.Traverse the right subtree (“go right”). A E D C B G F JI H

Introduction to Data Structure, Fall 2006 Slide- 17 California State University, Fresno Binary Tree Traversal – Preorder void Preorder(struct tnode* root) { if(root==NULL) return; visit(root); Preorder(root->left); Preorder(root->right); } A E D C B G F JI H

Introduction to Data Structure, Fall 2006 Slide- 18 California State University, Fresno Binary Tree Traversal – Inorder 1.Traverse the left subtree (“go left”). 2.Visit the node 3.Traverse the right subtree (“go right”). A E D C B G F JI H

Introduction to Data Structure, Fall 2006 Slide- 19 California State University, Fresno Binary Tree Traversal – Postorder 1.Traverse the left subtree (“go left”). 2.Traverse the right subtree (“go right”). 3.Visit the node A E D C B G F JI H

Introduction to Data Structure, Fall 2006 Slide- 20 California State University, Fresno Binary Tree Nodes

Introduction to Data Structure, Fall 2006 Slide- 21 California State University, Fresno Binary Tree Nodes

Introduction to Data Structure, Fall 2006 Slide- 22 California State University, Fresno Binary Trees – Coding struct node { int data; struct node* left; struct node* right; }

Introduction to Data Structure, Fall 2006 Slide- 23 California State University, Fresno Binary Trees – Typical Problems Build (insert/delete) a tree. Count the number of nodes in a tree. Count the maximum depth in a tree. Print a tree in preorder. Print a tree in inorder. Print a tree in Postorder. Print all possible paths in a tree. Check if two binary trees are the same.