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.

Slides:



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

Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
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.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Trees, Binary Trees, and Binary Search Trees COMP171.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Binary Trees Chapter 6.
Tree.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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 ),
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.
Trees, Binary Trees, and Binary Search Trees COMP171.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
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.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
Binary Search Trees (BST)
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
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.
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.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
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.
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 Search Trees Chapter 7 Objectives
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Lecture 22 Binary Search Trees Chapter 10 of textbook
Data Structures & Algorithm Design
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Chapter 21: Binary Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
CMSC 202 Trees.
Binary Trees, Binary Search Trees
Tree.
Chapter 20: Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

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 typically drawn as the highest node in the tree Every node other than the root is connected to some other node called the parent. P C parent child edgenode root

3 Basic Terminology A node can have no more that one parent A node can have zero or more children Parents are typically drawn above their children A tree is connected in that if we start at any node other than the root and move to its parent and so on, we will eventually arrive at the root of the tree. P C2C1Cn...

4 Paths, Ancestors and Descendants A node by itself is considered a tree. A node and all of its descendants are considered a tree. An empty tree (root pointer == NULL) is considered a tree. n1 n3n2n4 n5n6n7 root

5 Paths, Ancestors, and Descendants Trees are built upon recursive definitions. Any node an all of its descendants is considered a subtree. n1 n3n2n4 n5n6n7 root Sub-tree

6 Paths, Ancestors and Descendants Nodes with no descendants are leaf nodes. Node n1 (the root) is an ancestor of all remaining nodes in the tree. n1 n3n2n4 n5n6n7 Leaf nodes

7 Paths, Ancestors and Descendants Nodes n2.. n7 are all descendants of n1. The path from n4 to n7 has a length of 1. –This is equal to the number of nodes on the path - 1. n1 n3n2n4 n5n6n7 Descendants of n1

8 Paths, Ancestors and Descendants The height of a node is the length of the longest path from the node to a leaf. –The height of the tree is 2, the height of the root. –The height of n1 is 2. –The height of n4 is 1 –The height on n6 is 0. n1 n3n2n4 n5n6n7 Height of tree is 2

9 Paths, Ancestors and Descendants The depth of a node is the length of the longest path from the root to the node. –The depth of n5 is 2. –The height of n5 is 0. –The depth of n3 is 1. –The height of n3 is 0. n1 n3n2n4 n5n6n7 The depth of n5 is 2

10 Binary Trees A binary tree node can have at most two children, a left child node and a right child node. Typical node definition for a binary tree: struct Node { int data; Node *leftChild, *rightChild; }; n1 n2n3 Left childRight child

11 Binary Trees struct Node { int data; Node *leftChild, *rightChild; }; The pointer fields are used to implement the edges of the tree. The data in the node may be simple or complex. –One of the data fields must serve as a key field.

12 Binary Trees A general recursive scheme for traversing a binary tree is: { action 1; recursive call on left subtree action 2; recursive call on right subtree action 3; } Not all three actions are required. The right subtree can be visited before the left subtree.

13 Binary Search Trees A binary search tree is a labeled binary tree in which the following properties hold at every node x in the tree: –All nodes in the left subtree of x have labels (values) less than the value of x. –All nodes in the right subtree of x have labels (values) greater than the value of x.

14 Binary Search Trees Order of insertion: 9, 3, 1, 4, 7, 6, 10 results in the above tree

15 Binary Search Trees Changing the order of insertion to: 6, 4, 1, 7, 3, 9, 10 results in the above tree

16 Binary Search Trees The following recursive algorithm displays the contents of a binary tree in ascending order. void ascending (Node *t) { if (t != NULL) { ascending(t->leftChild); (1) cout data; ascending(t->rightChild); (2)} }

17 Binary Search Trees A binary search tree must support the following operations: –Insertion of new nodes. –Deletion of existing nodes. –Verification that a node is/isn’t in the tree A data set upon which we can execute an insert, delete, and look-up is called a dictionary, no matter how the set is implemented or what it is used for.

18 Search Tree Verification //This function returns a 1 (true) if x is in the tree and 0 (false) //if x is not in the tree int lookUp (elementType x, Node *t) { if (t = = NULL) return 0; else if (x = = t->element) return 1; else if (x element) return lookUp(x, t->leftChild); else // x must be > t->element return lookUp(x, t->rightChild); }

19 Search Tree Insertion //This function inserts a new x into the tree Node* insert (elementType x, Node *t) { if (t = = NULL) { t = new Node; t->element = x; t->leftChild = NULL; t->rightChild = NULL; } else if (x element) (1) t->leftChild = insert(x, t->leftChild); else if (x > t->element) (2) t->rightChild = insert(x, t->rightChild); return t; }

20 Search Tree Deletion There are 3 cases that must be supported with a binary search tree deletion. –Case 1: Node to be deleted (b) is a leaf a b delete a->rightChild; a->rightChild = NULL; 10 a (before)(after)

21 Search Tree Deletion –Case 2: Node to be deleted (b) has only 1 subtree tempPtr = b->rightChild; delete a->rightChild; a->rightChild = tempPtr; a b a (before)(after) c d e c d e

22 Search Tree Deletion –Case 3: Node to be deleted (b) has 2 children Replace the node to be deleted (b) with the smallest node in the right subtree of b a b(before) cd efg h a f(after) cd ehg

23 Search Tree Deletion –Case 3: Node to be deleted (b) has 2 children a b(before) cd efg h a f(after) cd ehg Smallest node will never have a left child. Note how the right subtree of the smallest right subtree was reattached.

24 Deletion Algorithm

25 Height-Balanced Binary Trees Trees should be kept as short and bushy as possible. Order of insertion: 6, 4, 9, 10, 7, 5, 1 results in a height-balanced tree. This is a best case insertion scenario resulting in a search time of O(log n)

26 Skewed Binary Trees Order of insertion: 1, 3, 4, 6, 7, 9, 10 results in a skewed tree. This is a worst case insertion scenario resulting in a search time of O(n)