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.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Advertisements

Trees Types and Operations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
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.
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.
Department of Computer Science University of Maryland, College Park
© 2004 Goodrich, Tamassia Binary Search Trees   
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Binary Search Trees1 Part-F1 Binary Search Trees   
Binary Search Trees1 ADT for Map: Map stores elements (entries) so that they can be located quickly using keys. Each element (entry) is a key-value pair.
© 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.
Marc Smith and Jim Ten Eyck
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Trees.
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
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.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
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.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
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 ADTs Tree concepts. Applications of Trees. A Tree ADT – requirements, contract. Linked implementation of Trees. Binary Tree ADTs. Binary Search.
Tree Data Structures. Introductory Examples Willliam Willliam BillMary Curt Marjorie Richard Anne Data organization such that items of information are.
Tree Data Structures.
1 CSC 211 Data Structures Lecture 24 Dr. Iftikhar Azim Niaz 1.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Binary Search Trees   . 2 Binary Search Tree Properties A binary search tree is a binary tree storing keys (or key-element pairs) at its.
Lecture1 introductions and Tree Data Structures 11/12/20151.
Data Structures & Algorithm Analysis Muhammad Hussain Mughal Trees. Binary Trees. Reading: Chap.4 ( ) Weiss.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
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.
CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Trees. Binary Trees. Reading: Chap.4 ( ) Weiss.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
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.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
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.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Trees (Unit 7).
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
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.
CSCE 3110 Data Structures & Algorithm Analysis
Search Trees.
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)
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Lecture 22 Binary Search Trees Chapter 10 of textbook
Binary Trees, Binary Search Trees
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Binary Trees, Binary Search Trees
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Tree.
Tree.
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:

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 the root. n The remaining nodes are partitioned into n>=0 disjoint sets T 1,..., T n, where each of these sets is a tree. n We call T 1,..., T n the subtrees of the root.

4 Level and Depth Level node (13) degree of a node leaf (terminal) nonterminal parent children sibling degree of a tree (3) ancestor level of a node height of a tree (4)

CHAPTER 5 5 Terminology n The degree of a node is the number of subtrees of the node –The degree of A is 3; the degree of C is 1. n The node with degree 0 is a leaf or terminal node. n A node that has subtrees is the parent of the roots of the subtrees. n The roots of these subtrees are the children of the node. n Children of the same parent are siblings. n The ancestors of a node are all the nodes along the path from the root to the node.

6 Representation of Trees n List Representation –( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) ) –The root comes first, followed by a list of sub-trees datalink 1link 2...link n How many link fields are needed in such a representation?

CHAPTER 5 7 Left Child - Right Sibling ABCD E F G H I J K LM data left child right sibling

CHAPTER 5 8 Binary Trees n A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left subtree and the right subtree. n Any tree can be transformed into binary tree. –by left child-right sibling representation n The left subtree and the right subtree are distinguished.

J IM H L A B C D E F G K *Figure 5.6: Left child-right child tree representation of a tree

CHAPTER 5 10 Abstract Data Type Binary_Tree structure Binary_Tree(abbreviated BinTree) is objects: a finite set of nodes either empty or consisting of a root node, left Binary_Tree, and right Binary_Tree. functions: for all bt, bt1, bt2  BinTree, item  element Bintree Create()::= creates an empty binary tree Boolean IsEmpty(bt)::= if (bt==empty binary tree) return TRUE else return FALSE

Class NodeBT{ NodeBT bTKi, bTKa; Object item; public NodeBT(NodeBt btki, Object it, NodeBt btka){ bTKi = btki; item = it; bTKa = btka; } Class BinaryTree{ NodeBt node; public create(){node = null;} // public BinaryTree(){node = null;} public isEmpty(BinaryTree bt){ return bt== null;} public Object inOrder(BinaryTree bt){} public Object preOrder(BinaryTree bt){} public Object posOrder(BinaryTree bt){} } 11

B public Object inOrder(BinaryTree bt){ inOrder(btKi); printData() intOrder(btKa); } public Object preOrder(BinaryTree bt){ printData() preOrder(btKi) preOrder(btKa) } public Object posOrder(BinaryTree bt){ posOrder(btKi) posOrder(btKa) printData() } preOrder(bt) print- A preOrder(btKi) print-B preOder(btKi) print-D preOder(btKa) print-E preOrder(btKa) print-C 12

CHAPTER 5 13 BinTree MakeBT(bt1, item, bt2)::= return a binary tree whose left subtree is bt1, whose right subtree is bt2, and whose root node contains the data item Bintree Lchild(bt)::= if (IsEmpty(bt)) return error else return the left subtree of bt element Data(bt)::= if (IsEmpty(bt)) return error else return the data in the root node of bt Bintree Rchild(bt)::= if (IsEmpty(bt)) return error else return the right subtree of bt

CHAPTER 5 14 Samples of Trees ABABABCGEIDHF Complete Binary Tree Skewed Binary Tree ECD

CHAPTER 5 15 Maximum Number of Nodes in BT n The maximum number of nodes on level i of a binary tree is 2 i-1, i>=1. n The maximum nubmer of nodes in a binary tree of depth k is 2 k -1, k>=1. i-1 Prove by induction.

CHAPTER 5 16 Relations between Number of Leaf Nodes and Nodes of Degree 2 For any nonempty binary tree, T, if n 0 is the number of leaf nodes and n 2 the number of nodes of degree 2, then n 0 =n 2 +1 proof: Let n and B denote the total number of nodes & branches in T. Let n 0, n 1, n 2 represent the nodes with no children, single child, and two children respectively. n= n 0 +n 1 +n 2, B+1=n, B=n 1 +2n 2 ==> n 1 +2n 2 +1= n, n 1 +2n 2 +1= n 0 +n 1 +n 2 ==> n 0 =n 2 +1

CHAPTER 5 17 Full BT VS Complete BT n A full binary tree of depth k is a binary tree of depth k having 2 -1 nodes, k>=0. n A binary tree with n nodes and depth k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k. k A B C G E I D H F A B C G E K D J F I H O N M L Full binary tree of depth 4 Complete binary tree

CHAPTER 5 18 Binary Tree Sequential Representations n If a complete binary tree with n nodes (depth = log n + 1) is represented sequentially, then for any node with index i, 1<=i<=n, we have: –parent(i) is at i/2 if i!=1. If i=1, i is at the root and has no parent. –left_child(i) ia at 2i if 2i n, then i has no left child. –right_child(i) ia at 2i+1 if 2i +1 n, then i has no right child.

CHAPTER 5 19 Sequential Representation A B -- C -- D --. E [1] [2] [3] [4] [5] [6] [7] [8] [9]. [16] [1] [2] [3] [4] [5] [6] [7] [8] [9] ABCDEFGHIABCDEFGHI ABECDABCGEIDHF (1) waste space (2) insertion/deletion problem

20 List Representation

BST 21

Binary Search Trees22 Binary Search Trees n A binary search tree is a binary tree storing keys (or key-value entries) at its internal nodes and satisfying the following property: –Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u)  key(v)  key(w) n External nodes do not store items n An inorder traversal of a binary search trees visits the keys in increasing order

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

Example Binary Searches n 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

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

Binary Search Tree – Insertion n 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 n Observations –O( log(n) ) operation for balanced tree –Insertions may unbalance tree

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

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.eft, key ); else // In right subtree return Find( n.right, key ); } Node n = Find( root, 5);

Binary Search Tree – Deletion n 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) n Delete ( 25 ) < 25, right 30 > 25, left 25 = 25, delete

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

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

QUESTION?? 34