Download presentation
Presentation is loading. Please wait.
Published byLauren Burns Modified over 11 years ago
1
COSC2007 Data Structures II Chapter 10 Trees I
2
2 Topics Terminology
3
3 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
4
4 Introduction & Terminology Hierarchical (Tree) structure: Parent-child relationship between elements of the structure Nonlinear One-to-many relationships among the elements Examples: Organization chart Library card-catalog Contents of Books More???
5
5 Introduction & Terminology Corporate structure
6
6 Introduction & Terminology Example: Library card catalog Card Catalog Subject Catalog Author CatalogTitle Catalog A - AbbexStafford, R - Stanford, RZon - Zz Stafford, R. H.Standish, T. A. Stanford Research Institute
7
7 Introduction & Terminology Table of contents of a book:
8
8 Trees and Tree Terminology A tree is a data structure that consists of a set of nodes and a set of edges (or branches) that connect nodes.
9
9 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
10
10 Introduction & Terminology Path from node n 1 to n k : A sequence of nodes n 1, n 2, …. n k such that there is an edge between each pair of nodes (n 1, n 2 ) (n 2, n 3 ),... (n k-1, n k ) Height h: The number of nodes on the longest path from the root to a leaf Ancestor-descendent relationship Generalization of parent-child 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
11
11 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) the lies directly below it Each node has at most one predecessor (parent) the lies directly above it
12
12 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
13
13 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
14
14 Number Of Nodes- height is h Minimum number of nodes in a binary tree: h At least one node at each of first h levels. Minimum number of nodes in a binary tree: 2 h - 1 All possible nodes at first h levels are present
15
15 Number Of Nodes & Height Let n be the number of nodes in a binary tree whose height is h. h <= n <= 2 h – 1 log 2 (n+1) <= h <= n
16
16 A B DE H IJK C FG L MNO 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 DE H IJK C FG L Not a full binary tree.A full binary tree.
17
17 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. A B DE H IJK C FG L Not a Complete Binary Tree A B DE H IJK C FG L A Complete Binary Tree
18
18 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 DE H IJK C FG L A B DE H IJK C FG Not a proper binary treeA proper binary tree
19
19 An Aside: A Representation for Complete Binary Trees Since tree is full, it maps nicely onto an array representation. A B DE H IJK C FG L 0 1 2 3 4 5 6 7 8 9 10 11 12 A B C D E F G H I J K L T: last
20
20 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 its 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.
21
21 Not Complete or Full or Proper A B D E H IJK C F L
22
22 Balanced vs. Unbalanced Trees 1 A B DE H IJK C FG L root A B D E H I JK C FG L start Sort of Balanced Mostly Unbalanced A binary tree in which the left and right subtrees of any node have heights that differ by at most 1
23
23 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 60 7020 1040 5030
24
24 Comparing Trees These two trees are not the same tree! A B A B Why?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.