Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC2007 Data Structures II

Similar presentations


Presentation on theme: "COSC2007 Data Structures II"— Presentation transcript:

1 COSC2007 Data Structures II
April 21, 2017 COSC2007 Data Structures II Chapter 11 Trees I Trees

2 Topics Terminology

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 Example?

4 Introduction & Terminology
Hierarchical (Tree) structure: Parent-child relationship between elements of the structure More? Examples: Organization chart Library card-catalog More???

5 Introduction & Terminology
Example: Library card catalog Card Catalog Subject Catalog Author Catalog Title Catalog A - Abbex Stafford , R - Stanford, R Zon - Zz Stafford, R. H. Standish, T. A. Stanford Research Institute

6 Introduction & Terminology
Table of contents of a book:

7 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.

8 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?

9 Introduction & Terminology
Path from node n1 to nk: A sequence of nodes n1, n2, … . nk such 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

10 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

11 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

12 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?

13 Number Of Nodes-height is 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

14 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??

15 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 D E H I J K C F G L A B C D E F G H I J K M N O L Not a full binary tree. A full binary tree.

16 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 D E H I J K C F G L A B D E H I J K C F G L Not a Complete Binary Tree A Complete Binary Tree

17 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 D E H I J K C F G L A B C D E F G H I J K Not a proper binary tree A proper binary tree

18 An Aside: A Representation for Complete Binary Trees
Since tree is complete, it maps nicely onto an array representation. A B D E H I J K C F G L last T: A B C D E F G H I J K L

19 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.

20 What Tree? A B D E H I J K C F L

21 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 A B D E H I J K C F G L root L D H I E Mostly Unbalanced Sort of Balanced J K

22 Binary search tree (BST)
60 70 20 10 40 50 30 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

23 Comparing Trees These two trees are not the same tree! A B A B Why?

24 Review Which of the following ADT is value-oriented? list sorted list
stack queue binary tree

25 Review The ______ is a position-oriented ADT that is not linear.
sorted list queue binary tree list

26 Review A node of a tree is called a(n) ______. edge root branch vertex

27 Review The lines between the nodes of a tree are called ______.
branches edges Arches subtrees

28 Review The node that is directly above node n in a tree is called the ______ of node n. root leaf parent child

29 Review In a tree, the children of the same parent are called ______.
leafs siblings roots contemporaries

30 Review Each node in a tree has ______. exactly one parent
at most one parent exactly two parents at most two parents

31 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

32 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

33 Review The ______ of a tree is the number of nodes on the longest path from the root to a leaf. height length width age

34 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

35 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

36 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

37 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

38 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]


Download ppt "COSC2007 Data Structures II"

Similar presentations


Ads by Google