1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.

Slides:



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

Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
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.
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
Department of Computer Science University of Maryland, College Park
Trees, Binary Trees, and Binary Search Trees COMP171.
CS2420: Lecture 13 Vladimir Kulyukin Computer Science Department Utah State University.
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
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.
1 Trees What is a Tree? Tree terminology Why trees? General Trees and their implementation N-ary Trees N-ary Trees implementation Implementing trees Binary.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
Binary Trees Chapter 6.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
COSC2007 Data Structures II
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
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,
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Trees, Binary Trees, and Binary Search Trees COMP171.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lecture1 introductions and Tree Data Structures 11/12/20151.
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.
Disusun Oleh : Budi Arifitama Pertemuan ke-8. Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
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 Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
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.
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.
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.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Lecture 9 Binary Trees Trees General Definition Terminology
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
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
1 CMSC 341 Introduction to Trees Textbook sections:
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
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.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
UNIT III TREES.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Lecture 22 Binary Search Trees Chapter 10 of textbook
CMSC 341 Introduction to Trees.
Data Structures & Algorithm Design
Binary Trees, Binary Search Trees
Data Structures Binary Trees.
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Binary Trees, Binary Search Trees
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Binary Trees, Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

1 Trees

2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees

3 Tree Structures Arrays, vectors, lists are linear structures, one element follows another. Trees are hierarchical structure.

4 Tree Structures

5 Tree Terminology Tree is a set of nodes. The set may be empty. If not empty, then there is a distinguished node r, called root, all other nodes originating from it, and zero or more non-empty subtrees T 1,T 2, …,T k, each of whose roots are connected by a directed edge from r. (inductive definition) T1 T2 Tk … r

6 Tree Terminology  root  Nodes in subtrees are called successors or descendents of the root node  An immediate successor is called a child  A leaf node is a node without any children while an interior node has at least one child.  The link between node describes the parent-child relation.  The link between parent and child is called edge  A path between a parent P and any node N in its subtrees is a sequence of nodes P= X 0,X 1, …,X k =N, where k is the length of the path. Each node X i in the path is the parent of X i+1, (0≤i<k)  Size of tree is the number of nodes in the tree

7 Tree Node Level and Path Length  The level of a node N in a tree is the length of the path from root to N. Root is at level 0.  The depth of a tree is the length of the longest path from root to any node.

8 Binary Tree  In a binary tree, no node has more than two children, and the children are distinguished as left and right.  A binary tree T is a finite set of nodes with one the following properties: 1.T is a tree if the set of nodes is empty. 2.The set consists of a root R and exactly two distinct binary trees. The left subtree T L and the right subtree T R, either or both subtree may be empty.

9 Examples of Binary Trees Size? depth?

10 Full Binary Tree full binary tree: a binary tree is which each node was exactly 2 or 0 children

11 Density of a Binary Tree  At any level n, a binary tree may contain from 1 to 2 n nodes. The number of nodes per level contributes to the density of the tree.  Degenerate tree: there is a single leaf node and each interior node has only one child.  An n-node degenerate tree has depth n-1  Equivalent to a linked list  A complete binary tree of depth n is a tree in which each level from 0 to n-1 has all possible nodes and all leaf nodes at level n occupy the leftmost positions in the tree.

12 Complete Binary Tree complete binary tree: a binary tree in which every level, except possibly the deepest is completely filled. At depth n, the height of the tree, all nodes are as far left as possible

13 Complete or noncomplete?

14 Complete or noncomplete?

15 Complete or noncomplete? occup y

16 Complete or noncomplete?

17 Perfect Binary Tree perfect binary tree: a binary tree with all leaf nodes at the same depth. All internal nodes have exactly two children. a perfect binary tree has the maximum number of nodes for a given height a perfect binary tree has 2 (n+1) - 1 nodes where n is the height of a tree –height = 0 -> 1 node –height = 1 -> 3 nodes –height = 2 -> 7 nodes –height = 3 -> 15 nodes

Binary Search Trees Key property –Value at node Smaller values in left subtree Larger values in right subtree –Example X > Y X < Z Y X Z

Binary Search Trees Examples Binary search trees Not a binary search tree

Iterative Search of Binary Tree Node *Find( Node *n, int key) { while (n != NULL) { if (n->data == key) // Found it return n; if (n->data > key) // In left subtree n = n->left; else // In right subtree n = n->right; } return null; } Node * n = Find( root, 5);

Recursive Search of Binary Tree Node *Find( Node *n, int key) { if (n == NULL) // Not found return( n ); else if (n->data == key) // Found it return( n ); else if (n->data > key) // In left subtree return Find( n->left, key ); else // In right subtree return Find( n->right, key ); } Node * n = Find( root, 5);

Example Binary Searches Find ( root, 2 ) > 2, left 5 > 2, left 2 = 2, found 5 > 2, left 2 = 2, found root

Example Binary Searches Find (root, 25 ) < 25, right 30 > 25, left 25 = 25, found 5 < 25, right 45 > 25, left 30 > 25, left 10 < 25, right 25 = 25, found

Types of Binary Trees Degenerate – only one child Complete – always two children Balanced – “mostly” two children –more formal definitions exist, above are intuitive ideas Degenerate binary tree Balanced binary tree Complete binary tree

Binary Search Tree Construction How to build & maintain binary trees? –Insertion –Deletion Maintain key property (invariant) –Smaller values in left subtree –Larger values in right subtree

Binary Search Tree – Insertion Algorithm 1.Perform search for value X 2.Search will end at node Y (if X not in tree) 3.If X < Y, insert new leaf X as new left subtree for Y 4.If X > Y, insert new leaf X as new right subtree for Y Observations –O( log(n) ) operation for balanced tree –Insertions may unbalance tree

Example Insertion Insert ( 20 ) < 20, right 30 > 20, left 25 > 20, left Insert 20 on left 20

Binary Search Tree – Deletion Algorithm 1.Perform search for value X 2.If X is a leaf, delete X 3.Else // must delete internal node a) Replace with largest value Y on left subtree OR smallest value Z on right subtree b) Delete replacement value (Y or Z) from subtree Observation –O( log(n) ) operation for balanced tree –Deletions may unbalance tree

Example Deletion (Leaf) Delete ( 25 ) < 25, right 30 > 25, left 25 = 25, delete

Example Deletion (Internal Node) Delete ( 10 ) Replacing 10 with largest value in left subtree Replacing 5 with largest value in left subtree Deleting leaf

Example Deletion (Internal Node) Delete ( 10 ) Replacing 10 with smallest value in right subtree Deleting leafResulting tree