Trees.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II Chapter 10 Trees I. 2 Topics Terminology.
Advertisements

Author: Takdir, S.ST. © Sekolah Tinggi Ilmu Statistik.
Introduction to Trees Chapter 6 Objectives
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
EC-211 DATA STRUCTURES LECTURE Tree Data Structure Introduction –The Data Organizations Presented Earlier are Linear in That Items are One After.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS2420: Lecture 13 Vladimir Kulyukin Computer Science Department Utah State University.
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
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.
Binary Trees Chapter 6.
COSC2007 Data Structures II
Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical.
Tree A connected graph that contains no simple circuits is called a tree. Because a tree cannot have a simple circuit, a tree cannot contain multiple.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
1 Searching Searching in a sorted linked list takes linear time in the worst and average case. Searching in a sorted array takes logarithmic time in the.
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.
Data Structures TREES.
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.
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.
Discrete Mathematics Chapter 5 Trees.
Binary Tree 10/22/081. Tree A nonlinear data structure Contain a distinguished node R, called the root of tree and a set of subtrees. Two nodes n1 and.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems.
BINARY TREES A BINARY TREE t IS EITHER EMPTY OR CONSISTS OF AN ITEM, CALLED THE ROOT ITEM, AND TWO DISTINCT BINARY TREES, CALLED THE LEFT SUBTREE AND.
CHAPTER 11 TREES INTRODUCTION TO TREES ► A tree is a connected undirected graph with no simple circuit. ► An undirected graph is a tree if and only.
1Computer Sciences. 2 HEAP SORT TUTORIAL 4 Objective O(n lg n) worst case like merge sort. Sorts in place like insertion sort. A heap can be stored as.
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
David Kauchak cs062 Spring 2010
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
Heaps, Heap Sort and Priority Queues
Red Black Trees
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Tree.
Section 8.1 Trees.
Lecture 18. Basics and types of Trees
CHAPTER 4 Trees.
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
(2,4) Trees 11/15/2018 9:25 AM Sorting Lower Bound Sorting Lower Bound.
Trees and Binary Trees.
Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103 Chapter 10 Trees Slides are adopted from “Discrete.
Trees.
CS200: Algorithm Analysis
Trees (Part 1, Theoretical)
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Binary Trees, Binary Search Trees
(edited by Nadia Al-Ghreimil)
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Topic 5: Heap data structure heap sort Priority queue
David Kauchak cs302 Spring 2012
Binary Trees.
7.1 Trees.
Binary Trees, Binary Search Trees
8.2 Tree Traversals Chapter 8 - Trees.
Presentation transcript:

Trees

Trees Set of elements or nodes one node as root every other node has a single predecessor (parent) and zero or more successors (children) nodes without successors are called leaves Nodes are connected by edges The depth of a node is the number of edges on the path to the root The height of the tree is the number of edges on the longest path from root to any leaf A complete tree has all leaves at same depth, all other nodes have the same number of children

Example leaf leaf leaf leaf leaf leaf leaf leaf root height of tree = 3  edge leaf leaf leaf leaf leaf leaf leaf leaf

Questions What is the height of the tree? What is the depth of node n? What is the depth of node p? Is the tree complete? n p

Questions What is the height of the tree? What is the depth of node n? What is the depth of node p? What is the depth of node q? p q n

Subtrees The subtree rooted at x is the tree of all descendants of x that has node x as root q p subtree rooted at q subtree rooted at p

Binary trees A binary tree is a structure defined on a finite set of nodes that either contains no nodes is composed of three disjoint sets of nodes a root node a binary tree called its left subtree a binary tree called its right subtree

Left and right subtrees In a binary tree the left subtree of x is the tree rooted at the left child of x the right subtree of x is the tree rooted at the right child of x x left subtree right subtree

Binary trees p q w x y z left subtree of root right subtree of root

Binary trees p q w x y z subtree rooted at p subtree rooted at q left subtree of p right subtree of p left subtree of q right subtree of q subtree rooted at p subtree rooted at q

Binary trees p q w x y z

Questions In a complete binary tree of height 3, how many nodes are in the tree? how many leaves are in the tree? In a complete binary tree of height 4, How many nodes in a complete binary tree of height h? How many leaves in a complete binary tree of height h? How many nodes in a binary tree of height h? How many leaves in a binary tree of height h?

Complete binary tree of height 3 15 nodes 8 leaves

Complete binary tree of height 4 31 nodes 16 leaves

Binary trees In a binary tree of height h the total number of nodes is at most 20 +21 +…+2h = 2h+1 –1 the number of leaf nodes is at most 2h If the tree is complete there are exactly 2h+1 – 1 nodes there are exactly 2h leaves binary tree complete binary tree

Decision trees A decision tree begins with a decision you need to make start with an initial decision node ask a question Structure for investigating options and the possible outcomes of choosing those options result of a decision can be another decision outcome is a terminal node Tree should have unique paths from the decision node to each of the terminal nodes. help you to choose between several courses of action

A decision tree yes yes no yes no no He received the Physics Prize in 1921. yes Would you like to read about Einstein? yes Try the Medicine Prize in 1962. no Would you like to read about a scientist? Look up the Peace Prize in 1991. yes no Would you prefer a humanitarian? Try the Prize in Literature, 1970. no

A decision tree for insertion sort a  b? sort three elements a,b,c yes no b  c? a  c? yes no yes no a  c? b  c? a  b  c b  a  c yes no yes no a  c  b c  a  b b  c  a c  b  a

Insertion sort of 4 elements a  b? sort four elements a,b,c,d best case path yes no b  c? a  c? yes no c  d? … b  c? worst case path no a  d? a  b  c  d … … no b  d? no … c  d? d  c  b  a

Decision-tree model Decision trees are used to model comparison sorts insertion sort, mergesort, quicksort Each possible permutation of n elements must appear as a leaf in the tree at least n! leaves Worst-case number of comparisons = length of longest path from root to leaf Insertion sort worst case: number of comparisons: 0+1+ … + n-1 = ½ (n2-n) Length of longest path in decision tree for 4 elements = ½ (42-4) = 6