Trees 2015, Fall Pusan National University Ki-Joune Li.

Slides:



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

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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.
Trees Chapter 8.
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
© 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.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Trees.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
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.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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 ),
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
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.
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.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
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.
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.
CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Trees. Binary Trees. Reading: Chap.4 ( ) Weiss.
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 Search Trees (BST)
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.
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.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
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.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
Chapter 05 Trees (Part II). Array Representation We can use an array to represent a complete binary tree. Lemma 5.4 ▫If a complete binary tree with.
CSCE 3110 Data Structures & Algorithm Analysis
Trees Chapter 15.
Trees.
Binary Trees, Binary Search Trees
Introduction to Trees IT12112 Lecture 05.
Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
2017, Fall Pusan National University Ki-Joune Li
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Binary Trees, Binary Search Trees
2018, Fall Pusan National University Ki-Joune Li
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Trees.
Binary Trees, Binary Search Trees
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 2015, Fall Pusan National University Ki-Joune Li

STEMPNU 2 Tree Definition  Tree : Finite set of one or more nodes One Root A set of n (  0) children, each of which is a tree (subtree) Hierarchical Structure Recursion  Recursive way of definition  Divide and Conquer a bc de f (a(b(d,e),c(f)))

STEMPNU 3 Some Terms Root  Leaf (or Terminal) Node  Internal nodes Child and Parent Sibling Degree of Tree  Maximum Branching Factor Level  Depth of Tree: Maximum Level a bc de f Root Leaf nodes Internal nodes Level 1 Level 2 Level 3

STEMPNU 4 Binary Tree  Tree whose maximum branch factor = 2  A finite set of nodes empty or Root, left subtree, and right subtree a bc de f

STEMPNU 5 Some Properties of Binary Tree A binary tree T of depth k  Maximum number of nodes on level i : 2 i-1  Maximum number of nodes in T : 2 k – 1  n 0 = n 2 + 1, where n 0 is the number of leaf nodes and n 2 is the number of nodes with two children Full Binary Tree  A Binary Tree of depth k with 2 k – 1 nodes  No empty subtrees except nodes on level k Complete Binary Tree  Nodes correspond to numberd from 1 to n a b de c fg

STEMPNU 6 Representation of Binary Tree : Array If a binary tree T with n nodes is complete and i is the node number of the i-th node,   LeftChild(i) = 2i if 2i  n, otherwise no left child  RightChild(i) = 2i + 1 if 2i +1  n, otherwise no left child Only valid for complete binary graph root Left child Right childParent

STEMPNU 7 Representation of Binary Tree : Linked Class BinaryTree { private: TreeNode*root; public: // Operations }; Class BinaryTree { private: TreeNode*root; public: // Operations }; Class TreeNode { friend class BinaryTree; private: DataTypedata; TreeNode *LeftChild; TreeNode*RightChild; }; Class TreeNode { friend class BinaryTree; private: DataTypedata; TreeNode *LeftChild; TreeNode*RightChild; }; Right Child Data Node Left Child

STEMPNU 8 Binary Tree Operations : Traversal Traversal  Prefix  Infix  Postfix  Level Order - + X* * XY X + Y * Z – X * Y - + X * Y Z * X Y X Y Z * + X Y * - YZ X + Y * Z – X * Y

STEMPNU 9 Preorder Traversal void BinaryTree::Preorder() { Preorder(root); } void BinaryTree::Preorder() { Preorder(root); } void BinaryTree::Preorder(TreeNode *node) { if(node!=NULL}{ cout data; preorder(node->leftChild); preorder(node->rightChild); } + A B +AB

STEMPNU 10 Level Order Traversal Not Recursive a b de c fg Queuing void BinaryTree::LevelOrder() { Queue *queue=new Queue; queue->add(root); TreeNode *p; while(queue->isEmpty()==NO) { p=queue->delete(); cout data; queue->add(node->leftChild); queue->add(node->rightChild); } a ab Output Queue c b de c fg

STEMPNU 11 TreeNode* BinaryTree::Copy(TreeNode *node) { TreeNode* p=NULL; if(node!=NULL}{ p=new TreeNode; p->data=node->data; p->leftVhild=copy(node->leftChild); p->rightChild=copy(node->rightChild); } return p; } Tree Copy TreeNode* BinaryTree::Copy() { return Copy(root); } TreeNode* BinaryTree::Copy() { return Copy(root); } C A B C A B Equal: Same WayEvaluation of propositional calculus

STEMPNU 12 Binary Search Tree  A kind of Binary Tree  For each node n in a binary search tree Max(n.leftChild) ≤ n.data < Min(n.rightChild)

STEMPNU 13 Binary Search Tree: Search Search by Recursion Search without Recursion TreeNode* BST::SearchBST(TreeNode *node,int key) { if(node==NULL) return NotFound; else if(key==node->data) return node; else if(key data) return SearchBST(node->leftChild); else return SearchBST(node->rightChild); } TreeNode* BST::SearchBST(int key) { TreeNode *t=root; while(t!=NULL) { if(key==t->data) return t; else if(key data) t=t->leftChild; else /* key > t->data) t=t->rightChild); } return NULL; }

STEMPNU 14 Binary Search Tree: Insertion Boolean BST::Insert(int key) { TreeNode *p=root; TreeNode *q=NULL; while(p!=NULL) { q=p; if(key==p->data) return FALSE; else if(key data) p=p->leftChild; else /*key > p->data*/ p=p->rightChild; } TreeNode *t=new TreeNode(key); if(root==NULL) root=t; else if(key data) q->leftChild=t; else q->rightChild = t; return TRUE; } p q p q p q 60

STEMPNU 15 Binary Search Tree: Deletion Three cases Case 1: Delete a Leaf Node Case 2: Delete a node with a child Case 3: Delete a node with two children 51 Largest node

STEMPNU 16 Heap Heap (Max Heap)  Priority Queue DeleteQueue: Delete Max Element from Queue  A Complete Binary Tree Data at a node is greater than the max of its two subtrees Represented by array (or pointers)

STEMPNU 17 Heap: Insertion Heap[++k]=60 Heap[  k/2  ]=55 Heap[k]=60 O(log 2 n)

STEMPNU 18 Heap: Deletion O(log 2 n)