Tree.

Slides:



Advertisements
Similar presentations
Introduction to Trees Chapter 6 Objectives
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
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.
Binary Trees Chapter 6.
Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
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. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Data Structures TREES.
Data Structures & Algorithm Analysis Muhammad Hussain Mughal Trees. Binary Trees. Reading: Chap.4 ( ) Weiss.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
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.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 16: Trees Announcements 1.Programming project.
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.
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.
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 General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
Trees A non-linear implementation for collection classes.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
CSE 373 Data Structures Lecture 7
CSCE 3110 Data Structures & Algorithm Analysis
Data Structures and Design in Java © Rick Mercer
CC 215 Data Structures Trees
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Binary Trees.
MCS680: Foundations Of Computer Science
Trees Another Abstract Data Type (ADT)
Csc 2720 Instructor: Zhuojun Duan
CMSC 341 Introduction to Trees.
Trees What is a Tree? Tree terminology Why trees?
Lecture 18. Basics and types of Trees
CHAPTER 4 Trees.
CSE 373 Data Structures Lecture 7
ITEC 2620M Introduction to Data Structures
Tree data structure.
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
Introduction to Trees IT12112 Lecture 05.
Trees and Binary Trees.
Tree data structure.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Trees CSE 373 Data Structures.
Trees Another Abstract Data Type (ADT)
Trees.
Data Structures: Trees and Binary Trees
Trees Definitions Implementation Traversals K-ary Trees
Trees Another Abstract Data Type (ADT)
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Binary Trees, Binary Search Trees
Trees.
Binary Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Binary Trees.
Tree and its terminologies
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Introduction to Trees Chapter 6 Objectives
Trees.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Tree

What is Tree? Definition A collection of nodes to represent a hierarchical relationship Each node has a value with a list of references to nodes. Each node is composed with the parent-child relationship. Acyclic graph: contain no cycle. T1 T2 1 T3 ……. root

Tree Example File directory structure

Basic Notations Terminology Node: a basic component in a tree Edge: The connection between one node and another Root: the top node in a tree A Internal node (non-terminal node): a node with one or more degrees A, B, C, D, E, H Leaf node (terminal node): a node with degree zero F, G, I, J, K, L A B C D E F J K G H I L

Basic Notations Terminology Level: The level of a node is its distance from the root. The level of the root node is 0. Height (depth): The longest path (number of nodes) from a root to the farthest leaf The height of a tree: The maximum level of nodes plus 1 Degree: The number of subtrees of a node Degree of a tree: The maximum degree of nodes Level 0 A B C D E F G H I Level 1 Level 2

Basic Notations Terminology Parent: a node that has one or more subtrees. Child: a node that is connected from a parent node. Sibling: a set of nodes with the same parent Ancestor: all nodes along the path from the root to the node Descendant: all nodes that are in a subtree A B C D E F G H I

Basic Notations Example A is the root node. B is the parent of nodes E and F. C and D are children of node A. B and C are sibling nodes. A and D are ancestors of node I. H and I are descents of node A. A B C D E F G H I

Representation of Tree Array representation for child nodes Store the children with an array pointer. n is the degree of the tree. item link_1 link_2 … link_n A B C D E F G H I The degree of the tree is 3.

Representation of Tree Array representation for child nodes Each node consist of a value and a list of node pointers. A B C D E F G H I E F G H I B C D A

Representation of Trees Left-child right-sibling representation Nodes of a fixed size: Two link fields per node Easy to manage the tree item left_child right_sibling A B C D E F G H I A B C D E F G H I

Representation of Trees LCRS representation A B C D E F G H I A B C D E F G H I

What is Binary Tree? Definition A binary tree is a finite set of nodes such that 1) empty or 2) consists of root node and at most two disjoint binary trees, called left subtree and right subtree Q: Is it a binary tree? Left subtree A Right subtree B B Left subtree D

What is Binary Tree? Q: Are they binary trees? Q: Are they same? A B C D E F G H I J A B C D E F G H I A A B B

Properties of Binary Tree If a binary tree has 𝒏 nodes, it has 𝒏 −𝟏 edges. (Prove it !!) A B C D E F G H I J A B C D E F # of nodes: 10, # of edges: 9 # of nodes: 6, # of edges: 5

Properties of Binary Tree Suppose that the height of a binary tree is 𝒌. Minimum number of nodes: 𝒌 Maximum number of nodes: 𝟐 𝒌 −𝟏 Level 0 A A B D E C F G Level 1 B Level 2 C # number of nodes: 1 + 1+ 1 = 3 # number of nodes: 1 + 2 + 4 = 7

Properties of Binary Tree Suppose that the number of nodes in a tree is 𝒏. Minimum height of the tree: 𝒍𝒐 𝒈 𝟐 (𝒏+𝟏) Maximum height of the tree: 𝒏 A B A B D E C F G C D E F G Height of the tree: 3 Height of the tree: 7

Perfect? Full Binary Tree The full binary tree of height 𝒌 has 𝟐 𝒌 −𝟏 nodes. A B D E C F G H I J K L M N O A B D E C F G The full binary tree of height 3 has 7 nodes. The full binary tree of height 4 has 15 nodes.

Complete Binary Tree Complete binary tree A binary tree with 𝑛 nodes that correspond to the nodes numbered from 1 to 𝑛 in the full binary tree of height 𝑘 A A B C B C D E F G D H I J K

Full vs. Complete Binary Tree Q: Is it a full binary tree, a complete binary tree, or none? Q: If a binary tree is the full binary tree, then it is also a complete binary tree. True of False. A B D E C A B D C E F

Array Representation How to represent a binary tree by an array The mapping for a binary tree corresponds to the nodes numbered from 1 to 𝑛 in the full binary tree of height 𝑘 [0] Index Data [0] A [1] B [2] C [3] D [4] E [5] F [6] G [7] H [8] I A [1] [2] B C [3] [4] [5] [6] D E F G [7] [8] H I

Array Representation Rule for array representation The parent node 𝑖 is at (𝑖−1)/2 . The left child of node 𝑖 is at 2 𝑖+1 −1. The right child of node 𝑖 is at 2 𝑖+1 . Index Data [0] A [1] B [2] C [3] D [4] E [5] F [6] G [7] H [8] I [0] A [1] [2] B C [3] [4] [5] [6] D E F G [7] [8] H I

Array Representation Example of array representation [0] Index Data [1] B [2] C [3] D [4] [5] [6] E [7] [8] F A [1] [2] B C [3] [4] [5] [6] D E [7] [8] F

Array Representation Example of array representation [0] Index Data [1] B [2] [3] C [4] [5] [6] [7] D [8] A [1] [2] B [3] [4] [5] [6] C [7] [8] D

Cons/Pros of Array Representation Easy to implement a binary tree Ideal for representing complete binary tree Cons Inefficient for arbitrary binary tree E.g., skewed binary tree Difficult to update insertions/deletions A B C D

Linked List Representation How to represent a binary tree by linked list A B D E C F G H I D E B A F G C H I

Linked List Representation How to represent a binary tree by linked list The skewed binary tree does not incur unnecessary space overhead. C B A D A B C D

Linked List Representation Node representation in a binary tree left_child item right_child typedef int BData; typedef struct _bTreeNode { BData item; struct _bTreeNode * left_child; struct _bTreeNode * right_child; } BTreeNode; item left_child right_child

How to Build a Binary Tree int main() { BTreeNode * n1 = (BTreeNode *)malloc(sizeof(BTreeNode)); BTreeNode * n2 = (BTreeNode *)malloc(sizeof(BTreeNode)); BTreeNode * n3 = (BTreeNode *)malloc(sizeof(BTreeNode)); n1->item = 10; // Setting the first node n1->left_child = n2; n1->right_child = NULL; n2->item = 20; // Setting the second node n2->left_child = n3; n2->right_child = NULL; n3->item = 30; // Setting the third node n3->left_child = NULL; n3->right_child = NULL; free(n1), free(n2), free(n3); return 0; } 10 20 30

Binary Tree Implementation Operations // Create a new node. BTreeNode * CreateNode(BData item); // Destroy a node. void DestroyNode(BTreeNode * node); // Conect the root to a left-side node. void CreateLeftSubtree(BTreeNode* root, BTreeNode * left); // Conect the root to a right-side node. void CreateRightSubtree(BTreeNode* root, BTreeNode * right); // Traverse a tree. void Inorder(BTreeNode* root); void Preorder(BTreeNode* root); void Postorder(BTreeNode* root); void Levelorder(BTreeNode* root);

Binary Tree Implementation CreateNode and DestroyNode operations // Create a new node. BTreeNode * CreateNode(BData item) { BTreeNode * node = (BTreeNode*)malloc(sizeof(BTreeNode)); node->item = item; node->left_child = NULL; node->right_child = NULL; return node; } // Destroy a node. void DestroyNode(BTreeNode * node) free(node);

Binary Tree Implementation CreateLeftSubtree and CreaterightSubtree operations // Conect the root to a left-side node. void CreateLeftSubtree(BTreeNode* root, BTreeNode * left) { if (root->left_child != NULL) exit(1); root->left_child = left; } // Conect the root to a right-side node. void CreateRightSubtree(BTreeNode* root, BTreeNode * right) if (root->right_child != NULL) root->right_child = right;

Binary Tree Implementation int main() { BTreeNode * node1 = CreateNode(1); BTreeNode * node2 = CreateNode(2); BTreeNode * node3 = CreateNode(3); BTreeNode * node4 = CreateNode(4); BTreeNode * node5 = CreateNode(5); BTreeNode * node6 = CreateNode(6); CreateLeftSubtree(node1, node2); CreateRightSubtree(node1, node3); CreateLeftSubtree(node2, node4); CreateRightSubtree(node2, node5); CreateLeftSubtree(node3, node6); DestroyNode(node1); DestroyNode(node2); DestroyNode(node3); DestroyNode(node4); DestroyNode(node5); DestroyNode(node6); return 0; } 1 2 4 5 3 6

Total Number of Nodes How to count the number of nodes of a binary tree Recursive definition 𝑁 𝑇 = 1 𝑖𝑓 𝑇 𝑖𝑠 𝑎 𝑙𝑒𝑎𝑓 𝑛𝑜𝑑𝑒 𝑁 𝑇.𝑟𝑖𝑔ℎ𝑡 ,+𝑁 𝑇.𝑙𝑒𝑓𝑡) +1 𝑂𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 int Nodes(BTreeNode *node) { int r = 0, l = 0; if (node->right_child != NULL) r = Nodes(node->right_child); if (node->left_child != NULL) l = Nodes(node->left_child); return 1 + r + l; } A B D E C F G H I 9 5 3 3 1 1 1 1 1

The Height of Binary Tree How to calculate the height of a binary tree Recursive definition 𝐻 𝑇 = 1 𝑖𝑓 𝑇 𝑖𝑠 𝑎 𝑙𝑒𝑎𝑓 𝑛𝑜𝑑𝑒 max⁡(𝐻 𝑇.𝑟𝑖𝑔ℎ𝑡 , 𝐻 𝑇.𝑙𝑒𝑓𝑡) +1 𝑂𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 int Height(BTreeNode *node) { int r = 0, l = 0; if (node->right_child != NULL) r = Height(node->right_child); if (node->left_child != NULL) l = Height(node->left_child); return 1 + max(r, l); } A B D E C F G H I Right subtree Left subtree