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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
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.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
Binary Trees Chapter 6.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
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.
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 Data Structures.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Compiled by: Dr. Mohammad Omar Alhawarat
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.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
COMP 103 Traversing Trees. 2 RECAP-TODAY RECAP  Building and Traversing Trees TODAY  Traversing Trees  Chapter 16, especially 16.2.
CSE 3358 NOTE SET 10 Data Structures and Algorithms.
Binary Search Trees Data Structures Ananda Gunawardena
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
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.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
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.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
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.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded 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.
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.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1 CMSC 341 Introduction to Trees Textbook sections:
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
Trees A non-linear implementation for collection classes.
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.
Trees. Trees: – A trunk from the roots – Divides into branches – Ends in leaves.
Trees Saurav Karmakar
Non Linear Data Structure
Data Structure and Algorithms
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Tree.
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Trees.
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Binary Tree Traversals
CMSC 202 Trees.
CS-240 Dick Steflik D.J. Foreman
Binary Trees, Binary Search Trees
Trees.
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: Binary Trees.
Binary Trees.
General Tree Concepts Binary Trees
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

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 bottom nodes are leaves  no outgoing edges  Every non-empty tree has one root one or more leaves

 Node A is the parent of node B  Node B is the child of node A  The root has no parent  All other nodes have exactly 1 parent A A B B

 Not a tree  D has 2 parents A A B B D D C C

 Path is a sequence of connected nodes  Length of a path is the number of edges in the path A to D is 2 C to F is 1 X is 0 A A B B D D C C F F X X Tree 1 Tree 2 Tree 3

 Height of a tree: length of its longest path from root to leaf  Ex: Height of 2 A A B B D D C C E E

 Depth of a node: length of path from root  Example A: 0 B: 1 E: 2 D: 2 C: 1 A A B B D D C C E E

 Subtrees of a node are the nodes rooted at a node’s children  A’s subtrees rooted at B rooted at C A A B B D D C C E E

 Special Tree  No node has more than 2 children can have 0,1, or 2  Each child is either “right” or “left” A A B B D D C C E E

A A B B D D C C E E X X Y Y Z Z S S T T

template class BinaryTreenode { private: D data_; BinaryTreenode * left_; BinaryTreenode * right_; public: BinaryTreenode(D d = D(), BinaryTreenode * left = NULL, BinaryTreenode * right = NULL); };

A A B B D D C C data: A right: left: data: C right: left: data: B right: left: data: D right: left:

 Example  Recursive definition of height for binary trees a tree with only a root (no children) has height 0 an non-empty tree has height 1 + max(height of left subtree, height of right subtree)

 No fixed number of children per node can’t have single member variable per child use a list of children items in list will be Treenode *s template class Treenode { private: D data; list *> kids;

A A B B C C D D data:A kids:items: count: 3 data: B kids: data: C kids: data: D kids:

 Alternative each node has a pointer to its first child each node has a pointer to its next sibling more basic linked list style template class Treenode { private: D data; Treenode * first_child_; Treenode * next_sibling_;

A A B B C C D D data:A first_: next_: data: B first_: next_: data: C first_: next_: data: D first_: next_:

19

 Iterate through all nodes each node visited once, to… print all values see if a node has some property modify the node, etc…  4 common orders for visiting nodes preorder postorder in order (binary trees only) level order

 Depth-first traversal  Visit the root first  Recursive definition visit the root do a preorder traversal of each subtree, left to right  Ex: A B E D C F A A B B D D C C E E F F

void preorder(TreeNode* node){ if( node != null ){ //--visit node— // preorder the children list ::iterator iter; for( iter = node->kids.begin(); iter != node->kids.end(); iter++){ preorder(*iter); }

 Depth-first traversal  Visit the root last  Recursive definition do a postorder traversal of each subtree, left to right visit the root  Ex: E D B F C A A A B B D D C C E E F F

 Depth-first traversal  For binary trees only  Visit root in between subtrees  Recursive definition in-order traversal of left subtree visit the root in-order traversal of right subtree  Ex: E B D A F C A A B B D D C C E E F F

 Difference is in when root is visited  Root first => preorder  Root last => postorder  Inbetween => in-order

 Breadth-first traversal  Visit all nodes at level 1 (depth 1) then level 2, level 3, etc… always left to right (or some order)  Ex: A B C E D F A A B B D D C C E E F F

 Use a queue instead of recursion (implicitly uses a stack) q.push(root); while( !q.empty() ){ //dequeue node n //visit n //enqueue all of n’s children(L to R) }

28

I laughed and he jumped she (ha!) ate allfive cakes  Do the pre, post, in, and level order traversals

I laughed and he jumped she (ha!) ate allfive cakes  Pre: I laughed and he jumped she (ha!) ate all 5 cakes  In: and he laughed she jumped I all ate 5 (ha!) cakes  Post: he and she jumped laughed all 5 ate cakes (ha!) I  Level: I laughed (ha!) and jumped ate cakes he she all 5