Trees Definitions Implementation Traversals K-ary Trees

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Binary Tree Terminology Linear versus hierarchical data Tree – connected graph with no cycles Child Parent Descendant Sibling Ancestor Leaf vs. internal.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
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.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Lecture 17 Non-Linear data structures Richard Gesick.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; } 
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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.
Tree Data Structures.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
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.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems.
Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Trees A non-linear implementation for collection classes.
Trees. Trees: – A trunk from the roots – Divides into branches – Ends in leaves.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Recursive Definition of Tree Structures
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
Tree.
Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Data Structures & Algorithm Design
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Binary Trees.
Abstract Data Structures
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
Trees.
Binary Trees.
Data Structures: Trees and Binary Trees
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Chapter 9 Binary Trees.
Design and Analysis of Algorithms
Binary Trees, Binary Search Trees
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Trees.
Chapter 20: Binary Trees.
Binary Trees.
Trees.
Tree and its terminologies
Non-Linear data structures
General Tree Concepts Binary Trees
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Trees.
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Trees Definitions Implementation Traversals K-ary Trees Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting trees: k-ary ↔ binary

Definitions Nodes Edges Root node Interior node Leaf Parent Children Ancestor / Proper ancestor Descendant / Proper descendant Level of a node Height of a node / tree Degree of a node / tree Depth of node Path Acyclic graph

a j b k c g m d l i h f e descendants of “a” root, height=4, depth=level=0 degree=1 proper descendants of “a” j b k c interior node, height=2, depth=level=2 degree=2 g m d l i h f e degree=0 leaf, height=0, depth=level=4 degree of tree = 2 height of tree = 4

Implementing a Tree Nodes and Links A B C D Node { Object value; Node lchild; Node rchild; } // Node A B C ▲ ▲ ▲ D ▲=null link ▲ ▲

Implementing a Tree One array A B C D A: 0 1 2 3 4 5 6 7 8 9 A[1] is root lchild of A[i] is A[2i] rchild of A[i] is A[2i+1] “-” means array element is null / not used A[0] not used as a node A[0] may be used to hold general info (e.g., number of nodes in tree) A B C ▲ ▲ ▲ D ▲=null link ▲ ▲

Implementing a Tree Three array A B C D ▲ ▲=null link root = 4 freelist = 7 null = “/” = -1 0 1 2 3 4 5 6 7 8 9 Object value: D - - - A - B - C - int lchild: / / 3 5 6 1 / 9 0 2 int rchild: / / / / 8 / / / / /

Traversals Preorder N L R preorder (Node t) if (t == null) return; visit (t.value()); preorder (t.lchild()); preorder (t.rchild()); } // preorder

Traversals Inorder L N R inorder (Node t) if (t == null) return; inorder (t.lchild()); visit (t.value()); inorder (t.rchild()); } // inorder

Traversals Postorder L R N postorder (Node t) if (t == null) return; postorder (t.lchild()); postorder (t.rchild()); visit (t.value()); } // postorder

a j b k c g m d l i h f e preorder: a j k m l b c g i h d f e inorder: m k l j a b i g h c f d e postorder: m l k j i h g f e d c b a

K-ary Trees a q c e f b d n m i p g k j degree of tree = 4 degree of nodes f and n = 3 height of tree = 3 depth=level of m = 2

K-ary Tree => Binary Tree q c e f b d n m i p K-ary Binary root root leftmost child left child right sibling right child g k j

Traversals K-ary Tree Binary Tree n g e j k m d c b f i p q a n g e j Preorder: Inorder: Postorder: Preorder: Inorder: Postorder: