Download presentation
Presentation is loading. Please wait.
1
CHAPTER 12 Trees
2
2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs. A tree with N nodes has N -1 links Parents and children: The first node in the ordered pair is called a parent of the second node, called a child Each child may have only one parent – this is the defining characteristics of a tree. Root: One node is designated to be the root of the tree – it does not have a parent Leaf: Nodes that don’t have children are called leaves. Internal node: A node that is not the root and has at least one child is called an internal node
3
3 Tree terminology
4
4 A root Leaves or terminal nodes Child (of root) Parent (of P, U, T, E) M C O L N D R P UTE
5
5 More Definitions Path: A sequence of nodes such that each next node is a child of the previous, Any node below another node and on a path from that node is called a descendant of that node Any node above another node on a connecting path from the root to that node is called an ancestor of that node All children of the same node are called siblings A tree that limits each node to no more than n children is called an n-ary tree
6
6 More Definitions Path length: the number of the links in the path Level (depth) of a node : The length of the path from the root to that node The root is considered to be level 0, the children of the root are at level 1, the grandchildren of the root are at level 2, and so on Height of a node: The length of the longest path from that node to a leaf Height or order of a tree is the length of the longest path from the root to a leaf
7
7 Path length and level
8
8 Balanced trees Subtree: a tree rooted at some internal node Balanced tree: For each node the height of its subtrees differs by not more than 1 link A tree is considered to be balanced if all of the leaves of the tree are at roughly the same depth
9
9 Balanced and unbalanced trees
10
10 Complete and perfect trees Complete tree: The heights of all leaves differ by at most 1 link. All nodes have same number of children except the leaves (0 children) and possibly the rightmost internal node with maximum depth. Perfect tree: All nodes have same number of children All leaves have same height
11
11 Some complete trees
12
12 Tree Implementations Tree nodes with references to: The object stored in the node Each child The parent (optional) Arrays Two dimensional, N x N (N – number of nodes) Two dimensional, M x 2 (M – number of links) One dimensional (complete trees), N One dimensional – any tree, N
13
13 Binary Tree nodes public class BinaryTreeNode { protected T element; protected BinaryTreeNode left, right; BinaryTreeNode (T object) // constructor { element = object; left = null; right = null; } …… }
14
14 Binary Tree: Linked Implementation public class LinkedBinaryTree { protected int count; protected BinaryTreeNode root; public LinkedBinaryTree() // empty tree { count = 0; root = null; } public LinkedBinaryTree( T element) // root specified { count = 1; root = new BinaryTreeNode (element); }
15
15 Tree implementations with arrays The nodes of the tree have to be numbered A.Two dimensional N x N Array[i][j] = 1 if j is a child of i = 0 otherwise B.Two dimensional M x 2, M – number of links Each row contains a pair representing a link
16
16 Example – two dimensional arrays Let the tree be given by the following pairs ( 6 nodes, 5 links): (1,2),(1,3),(1,4),(2,5),(2,6) A:B: 0 1 1 1 0 01 2 0 0 0 0 1 11 3 0 0 0 0 0 01 4 0 0 0 0 0 02 5 0 0 0 0 0 02 6 0 0 0 0 0 0 more efficient in terms of space very inefficient implementation In terms of space
17
17 Tree implementations with arrays One dimensional arrays: A.Complete binary trees Array[0] contains the root Array[2*n + 1] contains the left child of node Array[n] Array[2*n+2] contains the right child of node Array[n] B.Any tree: the indexes in the array correspond to the nodes Array[n] contains the parent of node n Array[0] contains -1, i.e. no parent
18
18 Example – one dimensional arrays A: Complete binary tree Let the tree be given by the following pairs ( 7 nodes): (a,b),(a,c),(b,e),(b,f),(c,d), (c,m), (e,k) Array:a b c e f d m the left child of c: array[2] is in Indexes 0 1 2 3 4 5 6 array[2*2 + 1], this is d Quick access to children and parents, but the tree has to be complete. What can be done if it is not complete? B: Any tree. Nodes are numbers from 0 Let the tree be: (0,1),(0,2),(0,3),(2,4),(2,5),(3,6) Array: -1 0 0 0 2 2 3 indexes 0 1 2 3 4 5 6 Quick access from any node to the root. Used in game programming
19
19 Traversal of Binary Trees Preorder Inorder Postorder Levelorder – nodes are visited from left to right starting with level 0
20
20 Preorder traversal Visit the root Visit the left subtree Visit the right subtree preorder(tree) { if (tree == null) return else visit (tree) preorder (tree.left) preorder (tree.right) }
21
21 Inorder traversal Visit the left subtree Visit the root Visit the right subtree inorder(tree) { if (tree == null) return else inorder (tree.left) visit (tree) inorder (tree.right) }
22
22 Postorder traversal Visit the left subtree Visit the right subtree Visit the root postorder(tree) { if (tree == null) return else postorder (tree.left) postorder (tree.right) visit (tree) }
23
23 C R L E T N O U M P A D C O M P U T E R L A N D P M U O C T E L R N A D P U M O L N D A R E T C
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.