240 likes | 254 Views
Binary Trees. Computer Science and Engineering. Introduction. We studied linked list is a dynamic linear data structure. It is dynamic since it supports efficient addition and deletion of items. It is linear since it is sequential and each element in it has exactly one successor.
E N D
Binary Trees Computer Science and Engineering B.Ramamurthy
Introduction • We studied linked list is a dynamic linear data structure. • It is dynamic since it supports efficient addition and deletion of items. • It is linear since it is sequential and each element in it has exactly one successor. • A tree is nonlinear data structure. • Each element may have more than one successor. • Can be static or dynamic. B.Ramamurthy
Topics for Discussion • Elements of a tree • Examples of trees • Binary Tree Definition • Types of binary trees • Contiguous (static) representation • Dynamic representation B.Ramamurthy
Terminology • Trees are used to represent relationships: items in a tree are referred to as nodes and the lines connecting the nodes that express the hierarchical relationship are referred to as edges. • The edges in a tree are directed. • Trees are hierarchical which means that a parent-child relationship exist between the nodes in the tree. • Each node has at most one parent. The node with no parents is the root node. • The nodes that have no successors (no children nodes) are known as leaf nodes. • Lets look at some examples and identify the various elements. B. Ramamurthy
Examples • Family ancestor tree • Directory of files organization in your computer system • Parse tree • Languages are defined using grammar • Grammars are specified using rules or syntax • Syntax is expressed using a notation called Backaus-Naur Form (BNF) (John Backus and Peter Naur) • Expression trees • Game trees B.Ramamurthy
An Ancester Tree (From Greek mythology) Gaea Cronus Phoebe Ocean Zeus Poseidon Demeter Pluto Leto ……… Apollo B.Ramamurthy
BNF for a Language • BNF notation includes nonterminals and terminals. • Terminals are literals or particular symbols. • Nonterminals are general expressions that can be substituted with terminals and nonterminals. Grammar rules specify the definition of a nonterminal. Nonterminals are enclosed with angle brackets <nonterminal> • Symbols used in construction include ::= (defines), | (or) and other common operators. B.Ramamurthy
BNF for a Java Statement <statement> ::= <selection-stmt> | <other-stmt> <selection-stmt> ::= if (<expr>) <statement> else <statement> <expr>::= <relational-expr>|<assign-expr>|<identifier> <relational-expr> ::= <expr> <rel-op> <expr> <assign-expr> ::= <expr> = <expr> B.Ramamurthy
Parse tree <statement> <selection-stmt> if ( <expr> ) <statement> else <statement> … <expr> <relational-expr> <expr> <expr> <rel-op> …. A major task of the compiler is to construct a parse tree from the input program and verify it is correct. <identifier> <identifier> < b a B.Ramamurthy
Expression tree + A + B + C * D + <left><root><right> (in-order expression) <root><left><right> (pre-order expression> <left><right><root> (post-order expression) * A B C D Single representation; Multiple views B.Ramamurthy
X X X X X X X X X X X X Game Tree …… X X X …. … X X X X X X B.Ramamurthy
Binary Tree • A binary tree can be defined recursively as follows. It is either • empty, or • consists of a root node together with left and right trees, both of which are binary trees. B.Ramamurthy
Binary Tree NonEmpty Empty NullObject (pattern) Singleton (pattern) B.Ramamurthy
Binary Tree (contd.) B.Ramamurthy
Binary Tree (contd.) B.Ramamurthy
Characteristics of trees • A path is a sequence of nodes n1, n2, ..., nk such that node ni is the parent of node ni+1 for all 1 <= i <= k. • The length of a path is the number of edges on the path. • The height of a node is the length of the longest path from the node to a leaf. • The height of tree is the height of its root. • The level of a node is the length of the path from the root to the node. B.Ramamurthy
Full Binary Tree • A full binary tree is a tree in which each node has exactly zero or two non-empty children. All leaves are at the same level. • A complete binary tree in which all the leaf nodes are in level n or n-1 and all leaves on the level n are filled from left to right. • There are some interesting properties that arise out of this definition. • Lets look at some examples to illustrate the various definitions. B.Ramamurthy
Example root Level 0 Level 1 internal node Height of the tree:3 leaf B.Ramamurthy
Contiguous Representation for complete binary tree 1 2 3 5 4 6 7 8 B.Ramamurthy
Complete binary tree (contd.) • Number the N nodes sequentially, from 1.. N, starting with the root , level by level from left to right. • Parent(n) = floor(n/2) for n>1 • Left child(n) = 2n (if 2n <= N, else no left child) • Right child(n) = 2n+1 (if 2n+1 <= N, else no right child) • The node number of children and parent can be calculated for a given node n. B.Ramamurthy
Contiguous representation • By placing the N nodes in a contiguous sequence we can use simple arithmetic relationships to process the nodes. This will eliminate storage for the pointers to sub trees. Root object 0 1 2 3 4 5 6 8 7 B.Ramamurthy
Array Representation • Refer to the array in slide 21 which represents the complete binary tree in slide 19; • Array index 1 has the root object, 2 and 3 the left and right sub tree of root object respectively and so on. • The storage needed for the pointers to the left and right sub tree for each node is eliminated; • But the location need to be calculated every time a node is accessed. Trade off is between the storage need for the pointers and extra execution time incurred for computing the location. B.Ramamurthy
Linked Representation • In its simplest form class BTree{ Object obj; BTree left; BTree right; //constructor get, set methods //public Object visitor(…) //for all other //operations } B.Ramamurthy
BTreeInterface Object getData( ) Object getLeft( ) Object getRight( ) void setData( ) void setLeft( ) void setRight( ) BTree Linked Representation • Simple interface/implementation /application • Addition of a visitor (Visitor Pattern) • State-based implementation B.Ramamurthy