You are on page 1of 3

Trees consist of a finite set of elements called nodes, or vertices, and a finite set of directed arcs called branches

that connect pairs of nodes. a finite set of one or more nodes such that a) there is a specially designated node called the root b) the remaining nodes are partitioned into n >= 0 disjoint , Tn where each of sets T1, these sets is a tree. T1, , Tn are called the subtrees of the root.

parent - immediate root of a node. - root of a subtree or tree ex. Nodes 60, 30, 20, 90, 80, 110 are considered as parent nodes. child - immediate descendant of a node. - a root of the subtree of a node ex. The nodes 30 and 90 are children of node 60. sibling nodes - children of the same parent. ex. nodes 20 and 40 are siblings, as well as node 100 and 130. leaf/terminal node - a node having no children. - nodes that have a degree zero ex. Nodes 10, 70, 100, and 130 are terminal nodes.

Fig. 1

60

30

90

20

40

80

110

ancestor - a node is an ancestor of another node if it is the parent of the node, or the parent of some other ancestor of that node. - all the nodes along the path from the root to that node.
130

10

70

100

ex. node 60 is an ancestor of nodes 10, 70, 100, 130. descendant - a node is a descendant of another node if it is a child of the node or the child of some other descendant of that node. - all nodes of the subtree of a node. ex. The descendants of node 90 are nodes 80, 110, 70, 100, and 130. degree of a node - the number of subtrees pointed to by the node. degree of a tree - the degree of a tree is the maximum degree of the nodes in the tree.

Tree Vocabulary node - an item of information plus the branches to other items. ex. The lists 10, 20, 30, 40, 60, 70, 80, 90, 100, 110, and 130 are called nodes of a tree. root - the top node that is pointed to by no other node except an external pointer. ex. Node 60 is the root of the tree.

Page 1 of 3

Binary Tree - a finite set of nodes which is either empty or consists of a root node and maximum of two disjoint binary tree called left subtree and right subtree. left subtree - the descendants to the left of a node. right subtree - the descendant to the right of a node. degree of a node The degree of a nonterminal node of a binary tree may be a maximum of 2 since the node may contain pointers to at most two other nodes. The degree of a terminal node of a binary tree is 0 since the node contains null pointers. degree of a tree The degree of a binary tree is 2, which is the maximum number of pointers a node may contain. level of a node - refers to the distance of a node from the root. The root of the tree is on level 1. The children of the root of a tree are on level 2 of the tree. The maximum number of nodes for a tree of level h is 2h 1 (binary tree). height/depth - the height (h) of a tree is the number of levels of nodes contained in the tree. ex. The height of the tree in Fig. 1 is 4. The height of the tree is the most important characteristic since the height indicates the maximum number of nodes that must be visited (compared) to locate a value in a tree. skewed tree - a skewed tree is one with more levels of nodes in one subtree of a node than in the other subtree.

balanced tree - a tree in which the height is a minimum for the number of nodes. Binary Search Tree (BST) - a special kind of binary tree where in the value of each node is greater than the value in its left child and less than the value in its right child. immediate predecessor - is the largest value in the left subtree. The immediate predecessor is located by choosing the left pointer and chasing right pointers until a node is reached with a nil right pointer. ex. the immediate predecessor of 60 is 40. the immediate predecessor of 90 is 80. immediate successor - is the smallest value in the right subtree. Located by choosing the right pointer of a node, then chase left pointers until a node is reached with a nil left pointer. ex. the immediate successor of 60 is 70. the immediate successor of 90 is 100.

Tree Traversals Traversing a tree means to visit all of its nodes example of which is by printing all of the values in the tree. Fig. 2
M

L Page 2 of 3

INORDER To print out the values in the tree in order. In traversing a tree, we first need to print the roots left subtree the values in the tree that are smaller than the value in the root node. Then we print the value of the root node. Finally, we print the values in the roots right subtree those values that are larger than the value in the root node. Given the tree in Fig. 2, an inorder traversal would print A D J L M Q R T PRE-ORDER Traversing a tree in preorder visits each node before its left and right subtrees or work at a node is performed before (pre) its children are processed. A preorder traversal of a binary tree. 1. visits the root. 2. traverse the left subtree preorder. 3. traverse the right subtree preorder. Given the tree in Fig. 2, a preorder traversal would print M D A J L R Q T

Sample Problem. 1. Convert the expression 2 + 3 * 5 ((6 + 8)/ 2) in to a binary tree. Where each operand is represented as a child and each operator as the parent. Answer
+ 2 *

2. Using the answer in number 1, traverse the tree and print the values using the following. a) INORDER (LNR) b) PREORDER (NLR) c) POSTORDER (LRN) Answer a) INORDER 2+3*56+8/2 b) PREORDER -+2*35/+682 c) POSTORDER 235*+68+2/3. Traverse the BST below
S

POST-ORDER In a postorder, traversing a tree would start by traversing the left subtree, followed by the right subtree, and then the root. The work at a node is performed after (post) its children are evaluated. A postorder traversal of a binary tree. 1. traverse the left subtree post order. 2. traverse the right subtree post order. 3. visits the root. In a postorder traversal, the tree in Fig. 2 would print A L J D Q T R M
Page 3 of 3 A

You might also like