NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.

Slides:



Advertisements
Similar presentations
Trees and Binary Trees Become Rich Force Others to be Poor Rob Banks
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.
Computer Science C++ High School Level By Guillermo Moreno.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
© 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.
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.
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
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.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
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.
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. 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 & Algorithm Analysis Muhammad Hussain Mughal Trees. Binary Trees. Reading: Chap.4 ( ) Weiss.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
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.
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.
CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Trees. Binary Trees. Reading: Chap.4 ( ) Weiss.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
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 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.
CH 7 : TREE ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.
Foundation of Computing Systems Lecture 4 Trees: Part I.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 CMSC 341 Introduction to Trees Textbook sections:
TREE Ahsan Ahmed College of Computer and Information Science Majma’ah University 1.
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.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Binary Trees.
CSCE 3110 Data Structures & Algorithm Analysis
Trees 5/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Trees 5/10/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Binary Trees.
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Binary Search Tree (BST)
Trees 二○一八年八月二十六日 Part-C Trees Trees.
Trees Another Abstract Data Type (ADT)
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.
Binary Trees, Binary Search Trees
Trees 9/21/2018 9:58 PM Trees this is a tree Trees.
Introduction to Trees IT12112 Lecture 05.
DATA STRUCTURE SUBMUTTED BY:- MADHU MADHAN Lecturer in computer engg. G.P. MEHAM (ROHTAK)
Trees and Binary Trees.
Find in a linked list? first last 7  4  3  8 NULL
Trees Another Abstract Data Type (ADT)
Week nine-ten: Trees Trees.
Trees Another Abstract Data Type (ADT)
Trees Palestine Gaza West Bank 48 Stolen Land Trees Trees
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 Tree Properties & Representation
Binary Trees.
Binary Trees, Binary Search Trees
Trees.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

NATURE VIEW OF A TREE leaves branches root

COMPUTER SCIENTIST’S VIEW root leaves nodes branches

WHAT IS A TREE A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. consists of nodes with a parent-child relation. Computers”R”Us Sales R&D Manufacturing Laptops Desktops US International Europe Asia Canada

TERMINOLOGY Subtree: tree consisting of a node and its descendants Root: node without parent (A) Siblings: nodes share the same parent Internal node: node with at least one child (A, B, C, F) External node (leaf ): node without children (E, I, J, K, G, H, D) Ancestors of a node: parent, grandparent, grand-grandparent, etc. Descendant of a node: child, grandchild, grand-grandchild, etc. Depth of a node: number of ancestors Height of a tree: maximum depth of any node (3) Degree of a node: the number of its children Degree of a tree: the maximum number of its node. A B D C G H E F I J K subtree

STRUCTURE OF TREE Every tree node: object – useful information children – pointers to its children Data Data Φ Data Φ Data Φ Data Φ Data Φ Data Φ

LEFT CHILD,RIGHT SIBLING REPRESENTATION Data Left Child Right Sibling A B C D E F G H I J K L

BINARY TREE Applications: A binary tree is a tree with the following properties: Each internal node has at most two children (degree of two) The children of a node are an ordered pair We call the children of an internal node left child and right child Alternative recursive definition: a binary tree is either a tree consisting of a single node, OR a tree whose root has an ordered pair of children, each of which is a binary tree Applications: arithmetic expressions decision processes searching A B C D E F G H I

A B Skewed Binary Tree E C D 5 A B C G E I D H F Complete Binary Tree 1 2 3 4

DATA STRUCTURE FOR BINARY TREE A node is represented by an object storing Element Parent node Left child node Right child node ∅ B A D C E B D A C E

LINKED REPRESENTATION typedef struct node *tree_pointer; typedef struct node { int data; tree_pointer left_child, right_child; }; data left_child data right_child left_child right_child

MAXIMUM NUMBER OF NODES IN A BINARY TREE The maximum number of nodes on depth i of a binary tree is 2i, i>=0. The maximum nubmer of nodes in a binary tree of height k is 2k+1-1, k>=0. Prove by induction.

NODE NUMBER PROPERTIES 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Left child of node i is node 2i, unless 2i > n, where n is the number of nodes. If 2i > n, node i has no left child.

If 2i+1 > n, node i has no right child. 3 4 5 6 7 8 9 10 11 12 13 14 15 Right child of node i is node 2i+1, unless 2i+1 > n, where n is the number of nodes. If 2i+1 > n, node i has no right child.

Full binary tree of depth 3 COMPLETE BINARY TREE A labeled binary tree containing the labels 1 to n with root 1, branches leading to nodes labeled 2 and 3, branches from these leading to 4, 5 and 6, 7, respectively, and so on. A binary tree with n nodes and level k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of level k. 1 2 3 7 5 9 4 8 6 Complete binary tree 1 2 3 7 5 11 4 10 6 9 8 15 14 13 12 Full binary tree of depth 3

BINARY TREE TRAVERSALS Let l, R, and r stand for moving left, visiting the node, and moving right. There are six possible combinations of traversal lRr, lrR, Rlr, Rrl, rRl, rlR Adopt convention that we traverse left before right, only 3 traversals remain lRr, lrR, Rlr inorder, postorder, preorder

PRE-ORDER TRAVERSAL In preorder, the root is visited first Here’s a preorder traversal to print out all the elements in the binary tree: void printPreorder(struct node* node) { if (node == NULL) return; /* first print data of node */ printf("%d ", node->data); /* then recur on left sutree */ printPreorder(node->left); /* now recur on right subtree */ printPreorder(node->right); }

IN-ORDER TRAVERSAL In in-order, the root is visited in the middle Here’s an inorder traversal to print out all the elements in the binary tree: void printInorder(struct node* node) { if (node == NULL) return; /* first recur on left child */ printInorder(node->left); /* then print the data of node */ printf("%d ", node->data); /* now recur on right child */ printInorder(node->right); }

POST-ORDER TRAVERSAL In postorder, the root is visited last Here’s a postorder traversal to print out all the elements in the binary tree: void printpostorder(struct node* node) { if (node == NULL) return; /* first recur on left child */ printpostorder(node->left); /* now recur on right child */ printpostorder(node->right); /* then print the data of node */ printf("%d ", node->data); }

INTRODUCTION TO BST An important special kind of binary tree is binary search tree(BST).In a BST, each node stores some information including a unique key value, and perhaps some associated data. A binary tree is a BST iff, for every node in the tree: All keys in n’s left subtree are less than the key in n, and All keys in n’s right subtree are greater than the key in n. NOTE: If duplicate keys are allowed, then nodes with values that are equal to the key in node n can be either in n’s left subtree or in its right subtree(but not both).In these notes,we will assume that duplicates are not allowed.

IMPORTANCE OF BST The reason binary-search trees are important is that the following operations can be implemented efficiently using a BST: Insert a key value. Determine whether a key value is in the tree. Remove a key value from the tree. Print all the key values in sorted order.

INSERTING A NODE

DELETING A NODE