Trees.

Slides:



Advertisements
Similar presentations
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Advertisements

Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
CHAPTER 71 TREE. Binary Tree A binary tree T is a finite set of one or more nodes such that: (a) T is empty or (b) There is a specially designated node.
COSC2007 Data Structures II
Traversing a Binary Tree 10/23/ Traversing Binary Tree There are 3 ways of traversing a binary tree T having root R. 1. Preorder Traversing Steps:
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.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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 ),
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.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
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.
NURUL HASLINDA NGAH BP-37 SEMESTER II 2013/2014.  At the end of this chapter you’ll be learnt about:  Binary tree  Binary search tree  Traversing.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
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 General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Trees Binary Trees Extended Binary Trees. Tree A Tree consist of Nodes connected by Edges. Node is represented by Circle Edges as Lines or Arrows A Tree.
CSCE 3110 Data Structures & Algorithm Analysis
Trees Saurav Karmakar
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Tree.
Trees ---- Soujanya.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Lecture Trees Chapter 9 of textbook 1. Concepts of trees
Lecture 22 Binary Search Trees Chapter 10 of textbook
TREE.
Data Structures & Algorithm Design
Lecture 18. Basics and types of Trees
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Tree data structure.
i206: Lecture 13: Recursion, continued Trees
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
Binary Trees, Binary Search Trees
Introduction to Trees IT12112 Lecture 05.
Trees, Binary Trees, and Binary Search Trees
Trees and Binary Trees.
CS212: Data Structures and Algorithms
Tree data structure.
Find in a linked list? first last 7  4  3  8 NULL
Trees.
Trees CMSC 202, Version 5/02.
Data Structures – Week #5
CMSC 202 Trees.
Data Structures and Algorithm Analysis Trees
Binary Trees, Binary Search 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.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Trees

Tree Collection of nodes or Finite set of nodes This collection can be empty Nodes and Edges Root Parent, Child, Siblings, Grand parent, Grand child, Ancestors, Decendents Every node except the root has one parent Subtree Degree of a node is # of its sub trees

Tree

Tree levels Two different approaches

Tree levels

Tree A path from node n1 to nk is defined as a sequence of nodes n1, n2, …….., nk The length of this path is the number of edges on the path There is a path of length zero from every node to itself There is exactly one path from the root to each node in a tree

Tree Height of a node is the length of a longest path from this node to a leaf All leaves are at height zero Height of a tree is the height of its root (maximum level)

Tree Depth of a node is the length of path from root to this node Root is at depth zero Depth of a tree is the depth of its deepest leaf that is equal to the height of this tree

Binary Trees There is no node with degree greater than two

Binary Tree Not a binary tree Binary tree

Binary Tree Complete binary tree Full binary tree

Full Binary Tree A full binary tree (sometimes proper binary tree or 2-tree or strictly binary tree or extended binary tree) is a tree in which every node other than the leaves has two children.

Complete Binary Tree A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.

Samples of Trees A A Complete Binary Tree A B B Skewed Binary Tree B C G E H I

Maximum Number of Nodes in BT The maximum number of nodes on level i of a binary tree is 2i, i>=0. The maximum number of nodes in a binary tree of depth k is 2k-1, k>=1.

Full binary tree of depth 4 Full BT vs. Complete BT A full binary tree of depth k is a binary tree of depth k having 2k-1 nodes, k>=0. A binary tree with n nodes and depth k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k. A A B C B C D E F G D E F G H I J K L M N O H I Complete binary tree Full binary tree of depth 4

Complete Binary Tree If a complete binary tree with n nodes (depth=└log n + 1┘) is represented sequentially, then for any node with index i, 1<=i<=n, we have: parent(i) is at └i/2┘ if i!=1. If i=1, i is at the root and has no parent. leftChild(i) is at 2i if 2i<=n. If 2i>n, then i has no left child. rightChild(i) is at 2i+1 if 2i+1<=n. If 2i+1>n, then i has no right child.

Sequential Representation [1] [2] [3] [4] [5] [6] [7] [8] [9] A B C D E F G H I Sequential Representation (1) waste space (2) insertion/deletion problem A [1] [2] [3] [4] [5] [6] [7] [8] [9] . [16] A B -- C D . E B A C B C D D E F G E H I

Linked Representation struct btnode { int data; btnode *left, *right; }; data left right data left right

Applications of Binary tree Implementing routing table in router Expression evaluation To solve database problems such as indexing. Data Compression code.

Tree Traversals Pre-Order N L R In-Order L N R Post-Order L R N

Tree Traversals Pre-Order(NLR) 1, 3, 5, 9, 6, 8

Tree Traversals In-Order(LNR) 5, 3, 9, 1, 8, 6

Tree Traversals Post-Order(LRN) 5, 9, 3, 8, 6, 1

Preorder traversal PREORDER(INFO,LEFT,RIGHT,ROOT) 1.[initially push NULL onto STACK , and initialize PTR] Set TOP:=1, STACK[1]:=NULL and PTR:=ROOT 2. Repeat steps 3 to 5 while PTR≠NULL 3. Apply PROCESS to INFO[PTR] 4. [right child?] If RIGHT[PTR] ≠ NULL , then [push on stack] Set TOP:=TOP+1, and STACK[TOP]:=RIGHT[PTR] [end of if structure] 5. [left child?] If LEFT[PTR] ≠ NULL , then Set PTR:=LEFT[PTR] Else:[pop from stack] Set PTR:=STACK[TOP] and TOP:=TOP-1 [end of step 2 loop] 6.exit

In-order traversal INORDER(INFO,LEFT,RIGHT,ROOT) 1. [initially push NULL onto STACK , and initialize PTR] Set TOP:=1, STACK[1]:=NULL and PTR:=ROOT 2. repeat while PTR ≠NULL [pushes left most path onto stack] a) set TOP:=TOP+1 and STACK[TOP]:=PTR b) set PTR:=LEFT[PTR] [end of loop] 3. Set PTR:=STACK[TOP] and TOP:=TOP-1 [pop from stack] 4. Repeat steps 5 to 7 while PTR ≠ NULL [backtracking] 5. Apply PROCESS to INFO[PTR] 6. [right child?] if RIGHT[PTR] ≠ NULL then: a)set PTR:=RIGHT[PTR] b) go to step 2 [end of if structure] 7. Set PTR:=STACK[TOP] and TOP:=TOP-1 [pop node] [end of step 4 loop] 8.exit

Post-order traversal POSTORDER (INFO, LEFT, RIGHT, ROOT) 1. [initially push NULL onto STACK , and initialize PTR] Set TOP:=1, STACK[1]:=NULL and PTR:=ROOT 2. Repeat 3 to 5 while PTR ≠NULL [pushes left most path onto stack] 3. Set TOP:=TOP+1 and STACK[TOP]:=PTR 4. If RIGHT[PTR] ≠ NULL then: Set TOP:=TOP+1 and STACK[TOP]:= -RIGHT[PTR] 5. Set PTR:=LEFT[PTR] [end of step 2] 6. PTR:=STACK[TOP] and TOP:=TOP-1 7. Repeat while PTR>0 a) Apply PROCESS to INFO[PTR] b) Set PTR:=STACK[TOP] and TOP:=TOP-1 8. If PTR<0 then: a) Set PTR:= -PTR b) Go to step 2 9.Exit

Tree traversal Applications Pre-order Tree copying Counting the number of nodes Counting the number of leaves Prefix notation from a expression tree Post-order Deleting a binary tree All stack oriented programming languages – mostly functional languages which fully works on nested functions. Calculator programs postfix notation in an expression tree – used in calculators In-order we can extract the sorted values in a BST

Binary Search Trees (BST) A data structure for efficient searching, insertion and deletion. Binary search tree property For every node X All the keys in its left subtree are smaller than the key value in X All the keys in its right subtree are larger than the key value in X

Binary Search Trees A binary search tree Not a binary search tree

Binary Search Trees Average depth of a tree is O(log N) Maximum depth of a tree is O(N) The same set of keys may have different BSTs

Searching BST If we are searching for 15, then we are done. If we are searching for a key < 15, then we should search in the left sub-tree. If we are searching for a key > 15, then we should search in the right sub-tree.

BST(searching) If ROOT=NULL then: FIND (INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR) If ROOT=NULL then: Set LOC:=NULL and PAR:=NULL and return 2) If ITEM=INFO[ROOT],then: Set LOC:=ROOT and PAR:=NULL and return If ITEM<INFO[ROOT],then: Set PTR:=LEFT[ROOT] and SAVE:=ROOT Else: Set PTR:=RIGHT[ROOT] and SAVE:=ROOT 4) Repeat step 5 and 6 while PTR≠NULL If ITEM=INFO[PTR] then: Set LOC:=PTR and PAR:=SAVE and return If ITEM<INFO[PTR],then: Set SAVE:=PTR and PTR:=LEFT[PTR] Set SAVE:=PTR and PTR:=RIGHT[PTR] 7) Set LOC:=NULL and PAR:=SAVE 8) Exit

BST(Insertion) INSBST (INFO, LEFT, RIGHT, ROOT, AVAIL, ITEM, LOC) Call FIND(INFO,LEFT,RIGHT,ROOT,ITEM,LOC,PAR) If LOC != NULL, then Exit 3) a) if AVAIL=NULL then : write OVERFLOW and exit b) Set NEW:=AVAIL, AVAIL:=LEFT[AVAIL] and INFO[NEW]:=ITEM c) Set LOC:=NEW , LEFT[NEW]:=NULL and RIGHT[NEW]:=NULL 4) If PAR=NULL then: Set ROOT:=NEW else if: ITEM<INFO[PAR] then: Set LEFT[PAR]:=NEW else : Set RIGHT[PAR]:=NEW 5) exit

BST (Deletion) CaseA (Info, Left, Right, Root, Loc, Par) This procedure deletes node N at location Loc, where N doesn’t have 2 children. Par=Null  N is root node; Child=Null  N has no child [Initilizes Child] If Left[Loc]= Null and Right[Loc] = Null, then Set Child = Null Else if Left[Loc] ≠ Null, then Set Child = Left[Loc] Else Set Child = Right[Loc] 2) If Par ≠ Null, then If Loc = Left[Par], then Set Left[Par] = Child Else Set Right[Par] = Child Else Set Root = Child 3) Return

BST (Deletion) CaseB(Info, Left, Right, Root, Loc, Par) This procedure deletes node N at location Loc, where N has 2 children. Par=Null  N is root node; Suc  inorder successor, ParSuc  Parent of inorder successor [Find Suc and ParSuc] Set Ptr = Right[Loc] and Save = Loc Repeat while Left[Ptr] ≠ Null Set Save = Ptr and Ptr = Left[Ptr] Set Suc = Ptr and ParSuc = Save [Delete inorder successor using Procedure of CaseA] Call CaseA (Info, Left, Right, Root, Suc, ParSuc) [Replace Node N by its inorder successor] a) If Par ≠ Null, then If Loc = Left[Par], then Set Left[Par] = Suc Else Set Right[Par] = Suc Else Set Root = Suc Set Left[Suc] = Left[Loc] and Right[Suc] = Right[Loc] 4) Return

Application of BST Storing a set of names, and being able to lookup based on a prefix of the name. (Used in internet routers.)