380 likes | 391 Views
COSC2007 Data Structures II. Chapter 11 Trees I. Topics. Terminology. Introduction & Terminology. Review Position-oriented ADT: Insert, delete, or ask questions about data items at specific position Examples: Stacks, Queues Value-oriented ADT:
E N D
COSC2007 Data Structures II Chapter 11 Trees I
Topics • Terminology
Introduction & Terminology • Review • Position-oriented ADT: • Insert, delete, or ask questions about data items at specific position • Examples: Stacks, Queues • Value-oriented ADT: • Insert, delete, or ask questions about data items containing a specific value • Example?
Introduction & Terminology • Hierarchical (Tree) structure: • Parent-child relationship between elements of the structure • More? • Examples: • Organization chart • Library card-catalog • More???
Card Catalog Author Catalog Title Catalog Subject Catalog A - Abbex Stafford , R - Stanford, R Zon - Zz Stafford, R. H. Standish, T. A. Stanford Research Institute Introduction & Terminology • Example: Library card catalog
Introduction & Terminology • Table of contents of a book:
Trees and Tree Terminology • A tree is a data structure that consists of a set of nodes (vertex) and a set of edges (or branches) that connect nodes.
Introduction & Terminology • A is the root node. • B is the parent of D and E. • C is the sibling of B • D and E are the children of B • D, E, F, G, I are external nodes, or leaves • A, B, C, H are internal nodes • The depth (level) of E is 2 • The height of the tree is (3)/4 • The degree of node B is 2 • Property?
Introduction & Terminology • Path from node n1 to nk: • A sequence of nodes n1, n2, … . nksuch that there is an edge between each pair of nodes (n1, n2) (n2, n3), . . . (nk-1, nk) • Height h: • The number of nodes on the longest path from the root to a leaf • Ancestor-descendent relationship • Ancestor of node n: • A node on the path from the root to n • Descendent of node n: • A node on the path from n to a leaf
Introduction & Terminology • Nodes: • Contains information of an element • Each node may contain one or more pointers to other nodes • A node can have more than one immediate successor (child) that lies directly below it • Each node has at most one predecessor (parent) that lies directly above it
Binary Trees • A binary tree is an ordered tree in which every node has at most two children. • A binary tree is: • either empty • or consists of a • root node (internal node) • a left binary tree and • a right binary tree • Each node has at most two children • Left child • Right child
Binary Trees • Special trees • Left (right) subtree of node n: • In a binary tree, the left (right) child (if any) of node n plus its descendants • Empty BT: • A binary tree with no nodes • Binary tree is position-oriented or value-oriented? But?
Number Of Nodes-heightis h • Suppose the height is h • Minimum number of nodes in a binary tree: • At least one node at each of first h levels. • Maximum number of nodes in a binary tree: • All possible nodes at first h levels are present
Number Of Nodes & Height • Let n be the number of nodes in a binary tree whose height is h. h <= n <= 2h – 1 • If we know n, what will be the value for h??
A B C D E F G H L I J K Full Binary Trees • In a full binary tree, • every leaf node has the same depth • every non-leaf node (internal node) has two children. A B C D E F G H L I J K M N O Not a full binary tree. A full binary tree.
A B C D E F G A H L I J K C B F G D E H I J K L Complete Binary Tree • In a complete binary tree • every level except the deepest must contain as many nodes as possible ( that is, the tree that remains after the deepest level is removed must be full). • at the deepest level, all nodes are as far to the left as possible. Not a Complete Binary Tree A Complete Binary Tree
A B C D E F G H L I J K Proper Binary Tree • In a proper binary tree, • each node has exactly zero or two children • each internal node has exactly two children. A B C D E F G H I J K Not a proper binary tree A proper binary tree
A C B F G D E H I J K L An Aside: A Representation for Complete Binary Trees • Since tree is complete, it maps nicely onto an array representation. last A B C D E F G H I J K L T: 0 1 2 3 4 5 6 7 8 9 10 11 12
Properties of the Array Representation • Data from the root node is always in T[0]. • Suppose some node appears in T[i] • data for its parent is always at location T[(i-1)/2] (using integer division) • data for it’s children nodes appears in locations T[2*i+1] for the left child T[2*i+2] for the right child • formulas provide an implicit representation of the edges • can use these formulas to implement efficient algorithms for traversing the tree and moving around in various ways.
A C B F D E H I J K L What Tree?
A B C D E F G root H L I J K Balanced vs. Unbalanced Binary Trees A binary tree in which the left and right subtrees of any node have heights that differ by at most 1 start A B C F G L D H I E Mostly Unbalanced Sort of Balanced J K
60 20 70 10 40 30 50 Binary search tree (BST) • A binary tree where • The value in any node n is greater than the value in every node in n’s left subtree • The value in any node n is less than the value of every node in n's right subtree • The subtrees are binary search trees too
A A B B Comparing Trees These two trees are not the same tree! Why?
Review • Which of the following ADT is value-oriented? • list • sorted list • stack • queue • binary tree
Review • The ______ is a position-oriented ADT that is not linear. • sorted list • queue • binary tree • list
Review • A node of a tree is called a(n) ______. • edge • root • branch • vertex
Review • The lines between the nodes of a tree are called ______. • branches • edges • Arches • subtrees
Review • The node that is directly above node n in a tree is called the ______ of node n. • root • leaf • parent • child
Review • In a tree, the children of the same parent are called ______. • leafs • siblings • roots • contemporaries
Review • Each node in a tree has ______. • exactly one parent • at most one parent • exactly two parents • at most two parents
Review • A descendant of node n is a node on a path from node n to ______. • the root • a leaf • a sibling of node n • a child of node n
Review • A subtree of node n is a subtree rooted at ______. • node n • the parent of node n • a child of node n • a sibling of node n
Review • The ______ of a tree is the number of nodes on the longest path from the root to a leaf. • height • length • width • age
Review • In a ______ of height h, all nodes that are at a level less than h have two children each. • general tree • binary tree • full binary tree • complete binary tree
Review • A ______ of height h is full down to level h – 1, with level h filled in from left to right. • full binary tree • complete binary tree • balanced binary tree • general tree
Review • In ______, the left and right subtrees of any node have heights that differ by at most 1. • all trees • all binary tress • n-ary trees • balanced binary trees
Review • Which of the following is NOT a property of a complete binary tree of height h? • all nodes at level h – 2 and above have two children each • when a node at level h – 1 has children, all nodes to its left at the same level have two children each • when a node at level h – 1 has one child, it is a left child • all leaves are at level h
Review • In an array based representation of a complete binary tree, which of the following represents the left child of node tree[i]? • tree[i+2] • tree[i–2] • tree[2*i+1] • tree[2*i+2]