Trees. Family Tree Recursive Definition Tree - Definition A tree is a finite set of one or more nodes such that: 1.There is a specially designated node.

Slides:



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

Computer Science C++ High School Level By Guillermo Moreno.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.
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.
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.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
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.
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.
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 ),
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R2. Binary Search Trees.
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 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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Trees 2015, Fall Pusan National University Ki-Joune Li.
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.
Discrete Mathematics Chapter 5 Trees.
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 Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
2/11/ IT 179 Recursive Definition of Tree Structures 1.Empty is a tree; the root is null 2.A node points to a finite number of the roots of some.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
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:
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Non Linear Data Structure
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Recursive Definition of Tree Structures
Week 6 - Wednesday CS221.
Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree.
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Data Structures & Algorithm Design
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Depict the Tree Structure in a picture
TREES General trees Binary trees Binary search trees AVL trees
Trees.
2017, Fall Pusan National University Ki-Joune Li
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
CS-240 Dick Steflik D.J. Foreman
Trees Chapter 10.
2018, Fall Pusan National University Ki-Joune Li
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: Binary Trees.
Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
2019, Fall Pusan National University Ki-Joune Li
Presentation transcript:

Trees

Family Tree

Recursive Definition Tree - Definition A tree is a finite set of one or more nodes such that: 1.There is a specially designated node called the root. 2.The remaining nodes are partitioned in n  0 disjoint sets T 1, T 2, …, T n, where each of these sets is a tree. 3. T 1, T 2, …, T n are called the sub-trees of the root.

Family Tree root Sub-trees Banu Hashim

A FKB CHD I L N X PGMQ Tree

A FKB CHD I L N X PGMQ A Root (no parent) FKB CDX Intermediate nodes (has a parent and at least one child) H I L NPGMQ Leaf nodes (0 children) Node Types

A FKB CHD I L N X PGMQ A C Degree 3 F X Degree 1 H I L NPGMQ Degree 0 KB D Degree 2 Degree of a Node Number of Children

A FKB CHD I L N X PGMQ A Level 0 FKB Level 1 CHDLX Level 2 INPGMQ Level 3 Level of a Node Distance from the root Height of the Tree = Maximum Level + 1

Examples of Binary Trees A KB CHLX PMQ A B C Q M

Properties of Binary Trees The maximum number of nodes on level i of a binary tree is 2 i The maximum number of nodes in a binary tree of height k is 2 k – 1

Full Binary Tree A binary tree of height k having 2 k – 1 nodes is called a full binary tree

Complete Binary Tree A binary tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right, is called a complete binary tree

Binary Tree No node has a degree > 2 struct TreeNode { int data; TreeNode*left, *right;// left subtree and right subtree }; Class BinaryTree { private: TreeNode * root; public: BinaryTree() { root = NULL; } void add (int data); void remove (int data); void InOrder();// In order traversal ~ BinaryTree(); };

Binary Tree Traversal In order Traversal (LNR) void BinaryTree::InOrder()// work horse function { InOrder(root); } void BinaryTree::InOrder(TreeNode *t) { if (t) { InOrder(t->left); visit(t); InOrder(t->right); } void BinaryTree::visit (TreeNode *t) { cout data; }

A KB CHLX PMQ A B C QQ Q Q C C MM M M C B B HH H H B A A K LL L L K K XX X PP P P X K A if (t) { InOrder(t->left); visit(t); InOrder(t->right); } Binary Tree Traversal In Order Traversal (LNR)

Binary Tree Traversal Pre Order Traversal (NLR) void BinaryTree::PreOrder() { PreOrder(root); } void BinaryTree::PreOrder(TreeNode *t) // work horse function { if (t) { visit(t); PreOrder(t->left); PreOrder(t->right); } void BinaryTree::visit (TreeNode *t) { cout data; }

A KB CHLX PMQ A B C QQQ C MMM C B HHH B A K LLL K XX PPP X K A if (t) { visit(t); PreOrder(t->left); PreOrder(t->right); } Binary Tree Traversal Pre Order Traversal (NLR) ABCQMHKLXP

Binary Tree Traversal Post Order Traversal (LRN) void BinaryTree::PostOrder()// work horse function { PostOrder(root); } void BinaryTree::PostOrder(TreeNode *t) { if (t) { PostOrder(t->left); PostOrder(t->right); visit(t); } void BinaryTree::visit (TreeNode *t) { cout data; }

A KB CHLX PMQ A B C QQ Q Q C C MM M M C B B HH H H B A A K LL L L K K XX X PP P P X K A if (t) { PostOrder(t->left); PostOrder(t->right); visit(t); } Binary Tree Traversal Post Order Traversal (LRN)

Binary Tree Traversal A KB CHLX PMQ NLR – visit when at the left of the Node ABCQMHKLXP LNR – visit when under the Node QCMBHALKXP LRN – visit when at the right of the Node QMCHBLPXKA

- /+ A*D* FCBE LNR: A+B*C-D/E*F NLR: -+A*BC/D*EF LRN: ABC*+DEF*/- Expression Tree

BinaryTree ::~ BinaryTree(); Delete both the left child and right child before deleting itself LRN Which Algorithm?