2019, Fall Pusan National University Ki-Joune Li

Slides:



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

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.
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.
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.
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Trees.
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.
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.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
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.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
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 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.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Trees 2015, Fall Pusan National University Ki-Joune Li.
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.
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.
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.
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.
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)
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
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
Traversal From CSCE 3110 Data Structures & Algorithm Analysis
Trees Chapter 15.
Binary Tree.
Trees.
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
Binary Trees, Binary Search Trees
Depict the Tree Structure in a picture
Introduction to Trees IT12112 Lecture 05.
Chapter 8 – Binary Search Tree
Trees.
General Trees & Binary 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 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) A heap.
General Trees & Binary Trees
2018, Fall Pusan National University Ki-Joune Li
Binary Trees, Binary Search Trees
Trees Chapter 10.
2018, Fall Pusan National University Ki-Joune Li
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Tree.
Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
C++ Plus Data Structures
Binary Trees, Binary Search Trees
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

2019, Fall Pusan National University Ki-Joune Li Trees 2019, Fall Pusan National University Ki-Joune Li

Tree Definition Hierarchical Structure Recursion 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 b c d e f (a(b(d,e),c(f)))

Some Terms Root Child and Parent Sibling Degree of Tree Level Leaf (or Terminal) Node Internal nodes Child and Parent Sibling Degree of Tree Maximum Branching Factor Level Depth of Tree: Maximum Level Internal nodes Root Level 1 Level 2 Level 3 a b c d e f Leaf nodes

Binary Tree Binary Tree Tree whose maximum branch factor = 2 A finite set of nodes empty or Root, left subtree, and right subtree a b c d e f

Some Properties of Binary Tree A binary tree T of depth k Maximum number of nodes on level i : 2i-1 Maximum number of nodes in T : 2k – 1 n0 = n2 + 1, where n0 is the number of leaf nodes and n2 is the number of nodes with two children Full Binary Tree A Binary Tree of depth k with 2k – 1 nodes No empty subtrees except nodes on level k Complete Binary Tree Nodes correspond to numberd from 1 to n a b d e c f g 1 2 4 5 3

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 1 2 3 4 5 1 2 4 5 3 root Left child Right child Parent

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

Binary Tree Operations : Traversal Prefix Infix Postfix Level Order X + Y * Z – X * Y - - + X * Y Z * X Y X + Y * Z – X * Y + * X Y Z * + X Y * - X * X Y Y Z

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

Level Order Traversal Not Recursive Queuing a b c d e f g Output a b c void BinaryTree::LevelOrder() { Queue *queue=new Queue; queue->add(root); TreeNode *p; while(queue->isEmpty()==NO) { p=queue->delete(); cout << p->data; queue->add(node->leftChild); queue->add(node->rightChild); } d e f g Output a b c Queue a b c d e f g

Tree Copy C C A B A B Equal: Same Way TreeNode* BinaryTree::Copy() { return Copy(root); } 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; C A B C A B Equal: Same Way Evaluation of propositional calculus

Binary Search Tree 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) 55 51 62 38 74 20 45 62 80 40 51

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<node->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 < t->data) t=t->leftChild; else /* key > t->data) t=t->rightChild); } return NULL;

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 < p->data) p=p->leftChild; else /*key > p->data*/ p=p->rightChild; } TreeNode *t=new TreeNode(key); if(root==NULL) root=t; else if(key < q->data) q->leftChild=t; else q->rightChild = t; return TRUE; + 60 q 55 q p 38 74 p q 20 45 62 80 p 40 51 60

Binary Search Tree: Deletion Three cases Largest node 55 55 51 38 38 74 38 74 20 45 45 62 45 62 40 51 40 51 55 40 51 55 Case 1: Delete a Leaf Node Case 2: Delete a node with a child Case 3: Delete a node with two children

Heap Heap (Max Heap) Priority Queue A Complete Binary Tree 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) 74 45 55 23 34 12

Heap: Insertion O(log2 n) + 60 74 74 45 55 45 55 23 34 12 23 34 12 60 Heap[++k]=60 74 Heap[ k/2 ]=55 45 55 O(log2 n) Heap[k]=60 23 34 12 60

Heap: Deletion O(log2 n) 74 12 45 55 45 55 45 55 34 23 12 34 23 12 34