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

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
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.
Chapter 11 A Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 A-2 Terminology A tree consists of vertices and edges, –An edge connects to.
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.
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 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.
Chapter 6 (cont’) Binary Tree. 6.4 Tree Traversal Tree traversal is the process of visiting each node in the tree exactly one time. This definition does.
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.
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.
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.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
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 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.
Traversal From CSCE 3110 Data Structures & Algorithm Analysis
Non Linear Data Structure
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Recursive Definition of Tree Structures
Week 6 - Wednesday CS221.
CS 302 Data Structures Trees.
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.
Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
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.
Data Structures & Algorithm Design
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Data Structures and Database Applications Binary Trees in C#
TREES General trees Binary trees Binary search trees AVL trees
Binary Tree Traversal Methods
Binary Tree Traversal Methods
2017, Fall Pusan National University Ki-Joune Li
Principles of Computing – UFCFA3-30-1
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Binary Tree Traversal Methods
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.
Binary Trees.
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

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

Family Tree Sub-trees root Banu Hashim

Node Types A Root (no parent) A F K B C H D I L N X P G M Q F K B C D Intermediate nodes (has a parent and at least one child) H I L N P G M Q Leaf nodes (0 children)

Degree of a Node Number of Children K B C H D I L N X P G M Q A C Degree 3 F X Degree 1 K B D Degree 2 H I L N P G M Q Degree 0

Level of a Node Distance from the root K B C H D I L N X P G M Q F K B Level 1 C H D L X Level 2 I N P G M Q Level 3 Height of the Tree = Maximum Level + 1

Binary Trees No node can have more than 2 children. That is, a node can have 0, 1, or 2 children

Examples of Binary Trees 1 2 3 4 A K B C H L X P M Q (a) (b)

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

Full Binary Tree A binary tree of height k having 2k – 1 nodes is called a full binary tree 1 3 2 5 4 7 6 8 9 10 11 12 13 14 15

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 1 3 2 5 4 7 6 8 9 10 11 12

Not a complete binary tree Missing left child 25 12 10 5 9 7 11 1 6 3 8 Not 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 << t->data; }

Binary Tree Traversal In Order Traversal (LNR) K B C H L X P M Q A A A B B B K K K C C C H H H L L L X X X if (t) { InOrder(t->left); visit(t); InOrder(t->right); } Q Q Q M M M P P P Q C M B H A L K X P

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 << t->data; }

Binary Tree Traversal Pre Order Traversal (NLR) K B C H L X P M Q A A A B B B K K K C C C H H H L L L X X X if (t) { visit(t); PreOrder(t->left); PreOrder(t->right); } Q Q Q M M M P P P A B C Q M H K L X P

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 << t->data; }

Binary Tree Traversal Post Order Traversal (LRN) K B C H L X P M Q A A A B B B K K K C C C H H H L L L X X X if (t) { PostOrder(t->left); PostOrder(t->right); visit(t); } Q Q Q M M M P P P Q M C H B L P X K A

Binary Tree Traversal A K B C H L X P M Q NLR – visit when at the left of the Node A B C Q M H K L X P LNR – visit when under the Node Q C M B H A L K X P LRN – visit when at the right of the Node Q M C H B L P X K A

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

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