Data Structure - Binary Tree 1 -

Similar documents
Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

CHAPTER 5. Trees CHAPTER 5 1/70

Tree Data Structures CSC 221

Binary Trees, Binary Search Trees

DATA STRUCTURES USING C

IX. Binary Trees (Chapter 10)

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

TREES. Trees - Introduction

Trees. (Trees) Data Structures and Programming Spring / 28

Chapter 20: Binary Trees

CSI33 Data Structures

Data Structure. Chapter 5 Trees (Part II) Angela Chih-Wei Tang. National Central University Jhongli, Taiwan

Binary Search Trees. What is a Binary Search Tree?

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Introduction to Computers and Programming. Concept Question

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Binary Trees. Height 1

Data and File Structures Laboratory

Binary Trees. Examples:

CSCI2100B Data Structures Trees

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

Binary Trees

CSI33 Data Structures

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Tree Structures. Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest

Module 04 Trees Contents

March 20/2003 Jayakanth Srinivasan,

TREES Lecture 10 CS2110 Spring2014

Programming II (CS300)

CE 221 Data Structures and Algorithms

Tree Structures. A hierarchical data structure whose point of entry is the root node

Trees and Tree Traversals. Binary Trees. COMP 210: Object-Oriented Programming Lecture Notes 8. Based on notes by Logan Mayfield

Associate Professor Dr. Raed Ibraheem Hamed

We have the pointers reference the next node in an inorder traversal; called threads

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

CSC148 Week 6. Larry Zhang

Graphs V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

Trees. CSE 373 Data Structures

Data Structures and Algorithms

Trees, Binary Trees, and Binary Search Trees

BBM 201 Data structures

Trees. Truong Tuan Anh CSE-HCMUT

Unit 1 (9Hrs) Trees. Mahesh Sanghavi & Deepali Pawar Department of Computer Engineering, SNJB s KBJ College of Engineering, Chandwad, India

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Data Structures And Algorithms

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Trees. A tree is a directed graph with the property

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

[ DATA STRUCTURES ] Fig. (1) : A Tree

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

CSE 230 Intermediate Programming in C and C++ Binary Tree

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Why Do We Need Trees?

Principles of Computer Science

Binary Trees and Binary Search Trees

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Trees. Trees. CSE 2011 Winter 2007

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

CHAPTER 5 TREES PART I

國立清華大學電機工程學系. Outline

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

CS301 - Data Structures Glossary By

COMP : Trees. COMP20012 Trees 219

TREES Lecture 12 CS2110 Fall 2016

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

A set of nodes (or vertices) with a single starting point

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Stacks, Queues and Hierarchical Collections

Garbage Collection: recycling unused memory

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR


Trees. Tree Structure Binary Tree Tree Traversals

Trees and Binary Trees

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Trees: examples (Family trees)

Topic 14. The BinaryTree ADT

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Algorithms and Data Structures

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Successor/Predecessor Rules in Binary Trees

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

Binary Trees. Reading: Lewis & Chase 12.1, 12.3 Eck Programming Course CL I

Introduction to Binary Trees

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Transcription:

Data Structure - Binary Tree 1 - Hanyang University Jong-Il Park

Basic Tree Concepts Logical structures Chap. 2~4 Chap. 5 Chap. 6 Linear list Tree Graph Linear structures Non-linear structures

Linear Lists and Trees Linear lists are useful for serially ordered data. (e 0, e 1, e 2,, e n-1 ) Days of week Months in a year Students in this class Trees are useful for hierarchically ordered data. Employees of a corporation. President, vice presidents, managers, and so on.

Hierarchical Data and Trees The element at the top of the hierarchy is the root. Elements next in the hierarchy are the children of the root. Elements next in the hierarchy are the grandchildren of the root, and so on. Elements that have no children are leaves.

Example Tree President root children of root VP1 VP2 VP3 Manager1 Manager2 Manager Manager grand children of root Worker Bee great grand child of root

Example Tree two types of genealogical charts

Terminology(Cont.) Def. Tree : A finite set of one or more nodes s.t. (1) There is a specially designated node called the root (2) The remaining nodes are partitioned into n disjoint sets T 1,.., T n, where each of these sets is a tree. T 1,.., T n are called the subtrees of the root. node, branch degree # of subtrees of a node

Terminology (Cont.) leaf or terminal node nodes that have degree zero nonterminal nodes siblings children of the same parent degree of a tree the maximum of the degree of the nodes in the tree ancestors of a node all the nodes along the path from the root to that node

Terminology (Cont.) level of a node defined by letting the root be at level one height or depth of a tree the maximum level of any node in the tree

Representation of trees: 1) Left child-right sibling representation every node has at most one leftmost child and at most one closest right sibling

Representation of trees: 2) Representation as a degree-two tree have the two children of a node binary trees left child-right child trees

Representation of trees: 2) Representation as a degree-two tree (Cont.)

BINARY TREES

Binary trees Definition: A Tree in which each node has no more than 2 children (left subtree and right subtree) Typical Representation in C typedef struct Tree_Node *PtrToNode; typedef struct PtrToNode Tree; r struct Tree_Node { ElementType Element; Tree Left; Tree Right; }; T L T R Division of 16Computer Science and Engineering, Hanyang University

The abstract data type a binary tree empty tree having zero nodes consist of a root and two disjoint binary trees (left subtree and right subtree) Binary tree Binary search tree

The abstract data type(cont.) distinctions between a binary tree and a tree a binary tree is a set of nodes either empty or there is no tree having zero nodes but there is an empty binary tree in a binary tree, distinguish between the order of the children; in a tree we do not the same as a tree

The abstract data type (Cont.)

The abstract data type (Cont.) a skewed binary tree: Figure 5.10(a) a complete binary tree: Figure 5.10(b)

Properties of binary trees Lemma 5.2 [Maximum number of nodes] 1) maximum number of nodes on level i of a binary tree: 2 i-1 2) maximum number of nodes in a binary tree of depth k : 2 k 1 Sum of max. no. of nodes on level i Lemma 5.3 [Relation between # of leaf nodes and degree-2 nodes] n 0 : # of leaf nodes n 2 : # of nodes of degree 2 n 0 = n 2 + 1

Properties of binary trees (Cont.) Lemma 5.3 [Relation between # of leaf nodes and degree-2 nodes] (Cont.) Proof: n 1 : # of nodes of degree one n : the total number of nodes n = n 0 + n 1 + n 2 ----- (5.1) B : the number of branches n = B + 1 every node except the root has a branch leading into it B = n 1 + 2n 2 n = B + 1 = n 1 + 2n 2 + 1 ----- (5.2) n 0 = n 2 + 1 from Eq. 5.1 and 5.2

Properties of binary trees (Cont.) Definition: a full binary tree of depth k is a binary tree of depth k having 2 k - 1 nodes Definition: 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

BINARY TREE REPRESENTATION

Array representation can easily determine the locations of the parent, left child, and right child of any node Lemma 5.4: in a complete binary tree, for any node i 1) parent(i) = i /2 2) leftchild(i) = 2i 3) rightchild(i) = 2i + 1

Array representation (Cont.) for the skewed tree, space is wasted

Linked representation general inadequacies of sequential representation wasteful for a skewed tree insertion and deletion of nodes from the middle of a tree require memory movement linked representation typedef struct node *treepointer; typedef struct node { int data; treepointer leftchild, rightchild; };

Linked representation (Cont.) /* global variable */ treepointer root;

Application of binary trees Binary Search Tree: representing ordered sets of elements Expression Tree: intermediate representation for expressions used by the compiler Expression Tree: An Example (a+b*c)+((d*e+f)*g) + + * a * + g b c * f d e Division 29 of Computer Science and Engineering, Hanyang University

BINARY TREE TRAVERSAL

Introduction traverse a tree or visit each node in the tree exactly once the order in which the nodes are visited let L, V, and R stand for moving left, visiting the node, and moving right six traversals: LVR, LRV, VLR, VRL, RVL, RLV consider only that we traverse left before right LVR: inorder -> infix LRV: postorder -> postfix VLR: preorder -> prefix

Traversal of binary trees Three ways to visit or traverse every node of a tree T, whose root is r and whose subtrees are T 1, T 2,..., T k. Preorder: Visit r, then recursively do a pre-order traversal of T 1, T 2,..., T k. [+,+,a,*,b,c,*,+,*,d,e,f,g] Example: Depth-First Search Postorder: Recursively do post-order traversal of T 1, T 2,..., T k, and then visit r. [a,b,c,*,+,d,e,*,f,+,g,*,+] Example: Code generation for Compilers Inorder: (for binary tree) Do in-order traversal of T L, visit r, then do inorder traversal of T R. [a,+,b,*,c,+,d,*,e,+,f,*,g] Example: Binary Search Tree Division 32 of Computer Science and Engineering, Hanyang University

Inorder traversal move left until we cannot go farther, and then visit the node, move one node to the right and continue

Inorder traversal (Cont.) - Left subtree root right subtree

Preorder traversal visit a node, traverse left, and continue if we cannot continue, move right and begin again or move back

Postorder traversal

Iterative inorder traversal to implement the tree traversal method without using recursion by using iterators

Level-Order Traversal a traversal that requires a queue visit the root first, then the root's left child, followed by the root's right child

Traversal without a stack 1. add a parent field to each node can trace way back up to any root and down again or 2. represent binary trees as threaded binary trees require two bits per node use the leftchild and rightchild fields to maintain the paths back to the root

ADDITIONAL BINARY TREE OPERATIONS

Copying binary trees Modification of the postorder traversal algorithm

Testing equality determine the equality of two binary trees equivalent if two binary trees have the same topology and the identical data topology: every branch in one tree corresponds to a branch in the second tree in the same order

The satisfiability problem the formulas of the propositional calculus a variable is an expression if x and y are expressions, x y, x y, x are expressions can use parentheses to alter the normal order of evaluation x 1 (x 2 x 3 ) the satisfiability problem for formulas of propositional calculus ask whether or not there is an assignment of values to the variables that causes the value of the expression to be true

The satisfiability problem (Cont.) (x 1 x 2 ) ( x 1 x 3 ) x 3 let (x 1, x 2, x 3 ) take on all possible combinations of true and false check the formula for each combination to evaluate an expression, need to traverse its tree in postorder

The satisfiability problem (Cont.) define this node structure in C True and False : constants typedef enum {not,and,or,true,false} logical; typedef struct node *treepointer; typedef struct node { treepointer leftchild; logical data; short int value; treepointer rightchild; };

The satisfiability problem (Cont.) See Program 5.9.

THREADED BINARY TREES

Threads there are more 0-links than actual pointers n + 1 0-links and 2n total links where # of nodes is n replace the 0-links by pointers, called threads 1) replace the rightchild field(value = 0) in node p by the inorder successor of p 2) replace the leftchild(value = 0) at node p by the inorder predecessor of p

Threads (Cont.) distinguish between threads and normal pointers add two boolean fields, leftthread and rightthread if t leftthread == TRUE, then t leftchild is a thread

Threads (Cont.) assume a head node for all threaded binary trees the original tree is the left subtree of the head node

Threads (Cont.)

Inorder traversal of a threaded binary tree if x rightthread == true, then the inorder successor of x is x rightchild if x rightthread == false, then the inorder successor of x is obtained by following a path of left-child links from the right child of x until a node with leftthread == true

Inorder traversal of a threaded binary tree (Cont.) Program 5.10

Inserting a node into a threaded binary tree how to make insertions into a threaded tree the case of inserting r as the right child of a node s if s has an empty right subtree, then a simple insertion if s has non-empty right subtree, then make this right subtree as the right subtree of r

Inserting a node into a threaded binary tree (Cont.)

Inserting a node into a threaded binary tree (Cont.)