CSE 214 Computer Science II Introduction to Tree Fall 2017 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu http://www3.cs.stonybrook.edu/~cse214/sec02/
Tree Tree is a non-linear data structure which is a collection of data (Node) organized in hierarchical structure. In tree data structure, every individual element is called as Node. Node stores the actual data of that particular element and link to next element in hierarchical structure. Tree with 11 nodes and 10 edges Shebuti Rayana (CS, Stony Brook University) 2
Root In a tree data structure, the first node is called as Root Node. Every tree must have root node. In any tree, there must be only one root node. Root node does not have any parent. (same as head in a LinkedList). Here, A is the Root node Shebuti Rayana (CS, Stony Brook University) 3
Edge The connecting link between any two nodes is called an Edge. In a tree with 'N' number of nodes there will be a maximum of 'N-1' number of edges. Edge is the connecting link between the two nodes Shebuti Rayana (CS, Stony Brook University) 4
Parent The node which is predecessor of any node is called as Parent Node. The node which has branch from it to any other node is called as parent node. Parent node can also be defined as "The node which has child / children". Here, A is Parent of B and C B is Parent of D, E and F C is the Parent of G and H Shebuti Rayana (CS, Stony Brook University) 5
Child The node which is descendant of any node is called as CHILD Node. In a tree, any parent node can have any number of child nodes. In a tree, all the nodes except root are child nodes. Here, B and C are Children of A G and H are Children of C K is a Child of G Shebuti Rayana (CS, Stony Brook University) 6
Siblings In a tree data structure, nodes which belong to same Parent are called as Siblings. In simple words, the nodes with same parent are called as Sibling nodes. Here, B and C are siblings D, E and F are siblings G and H are siblings Shebuti Rayana (CS, Stony Brook University) 7
Leaf The node which does not have a child is called as Leaf Node. leaf node is also called as 'Terminal' node. Here, D, I, J, F. K and H are leaf nodes Shebuti Rayana (CS, Stony Brook University) 8
Internal Nodes In a tree data structure, the node which has at least one child is called as Internal Node. Here, A, B, E, C, G are Internal Nodes Shebuti Rayana (CS, Stony Brook University) 9
Degree the total number of children of a node is called as Degree of that Node. The highest degree of a node among all the nodes in a tree is called as 'Degree of Tree' Here, Degree of A is 2 Degree of B is 3 Degree of F is 0 Shebuti Rayana (CS, Stony Brook University) 10
Level In a tree data structure, the root node is said to be at Level 0 and the children of root node are at Level 1 and the children of the nodes which are at Level 1 will be at Level 2 and so on. In simple words, in a tree each step from top to bottom is called as a Level and the Level count starts with '0' and incremented by one at each level (Step). Shebuti Rayana (CS, Stony Brook University) 11
Height the total number of edges from leaf node to a particular node in the longest path is called the Height of that Node. In a tree, height of the root node is said to be height of the tree. In a tree, height of all leaf nodes is '0'. Here, Height of the tree is 3 Shebuti Rayana (CS, Stony Brook University) 12
Depth The total number of edges from root node to a particular node is called as Depth of that Node. In a tree, the total number of edges from root node to a leaf node in the longest path is said to be Depth of the tree. Here, Depth of the tree is 3 Shebuti Rayana (CS, Stony Brook University) 13
Path The sequence of Nodes and Edges from one node to another node is called a Path between that two Nodes. Length of a Path is total number of nodes in that path. In below example the path A - B - E - J has length 4. Here, Path between A and J: A-B-E-J Path between C and K: C-G-K Shebuti Rayana (CS, Stony Brook University) 14
Sub-tree Each child from a node forms a subtree recursively. Every child node will form a subtree on its parent node. Shebuti Rayana (CS, Stony Brook University) 15
Tree Definition With the basic vocabulary now defined, we can move on to a formal definition of a tree. Two definitions of a tree. One definition involves nodes and edges. The second definition, which will prove to be very useful, is a recursive definition. Shebuti Rayana (CS, Stony Brook University) 16
Tree Definition Definition One: A tree consists of a set of nodes and a set of edges that connect pairs of nodes. A tree has the following properties: One node of the tree is designated as the root node. Every node n, except the root node, is connected by an edge from exactly one other node p, where p is the parent of n. A unique path traverses from the root to each node. If each node in the tree has a maximum of two children, we say that the tree is a binary tree. Shebuti Rayana (CS, Stony Brook University) 17
Tree Definition Definition Two: A tree is either empty or consists of a root and zero or more subtrees, each of which is also a tree. The root of each subtree is connected to the root of the parent tree by an edge. Shebuti Rayana (CS, Stony Brook University) 18
Advantages of Trees Trees are so useful and frequently used, because they have some advantages: Trees reflect structural relationships in the data Trees are used to represent hierarchies Trees provide an efficient insertion and searching Trees are very flexible data structure, allowing to move subtrees around with minimum effort Shebuti Rayana (CS, Stony Brook University) 19
Examples of Tree An example of the biological classification of some animals. From this simple example, we can learn about several properties of trees. The first property this example demonstrates is that trees are hierarchical. By hierarchical, we mean that trees are structured in layers with the more general things near the top and the more specific things near the bottom. The top of the hierarchy is the Kingdom, the next layer of the tree (the children of the layer above) is the Phylum, then the Class, and so on. However, no matter how deep we go in the classification tree, all the organisms are still animals. Shebuti Rayana (CS, Stony Brook University) 20
Examples of Tree Another example of a tree structure that you probably use every day is a file system. In a file system, directories, or folders, are structured as a tree. Following figure illustrates a small part of a Unix file system hierarchy. Shebuti Rayana (CS, Stony Brook University) 21
Examples of Tree The HTML source code and the tree accompanying the source illustrate another hierarchy. Notice that each level of the tree corresponds to a level of nesting inside the HTML tags. The first tag in the source is<html> and the last is </html> All the rest of the tags in the page are inside the pair. If you check, you will see that this nesting property is true at all levels of the tree. Shebuti Rayana (CS, Stony Brook University) 22
Some More Examples genealogical trees organizational trees biological hierarchy trees evolutionary trees population trees book classification trees decision trees graph spanning trees search trees compression trees program dependency trees expression/syntax trees Shebuti Rayana (CS, Stony Brook University) 23
Binary Tree Binary tree is a special type of tree data structure in which every node can have a maximum of 2 children. One is known as left child and the other is known as right child. In a binary tree, every node can have either 0 children or 1 child or 2 children but not more than 2 children. Shebuti Rayana (CS, Stony Brook University) 24
Types of Binary Tree A full binary tree is a binary tree in which each node has exactly zero or two children. A complete binary tree is a binary tree, which is completely filled, with the possible exception of the bottom level, which is filled from left to right. Full Tree Complete Tree Shebuti Rayana (CS, Stony Brook University) 25