Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Tree ADT: Properties

Similar presentations


Presentation on theme: "Binary Tree ADT: Properties"— Presentation transcript:

1 Binary Tree ADT: Properties
Full Binary Tree: All the internal nodes, non-leaf nodes, have two children. All the leaves are at the same depth k. The number of internal nodes in depth k tree is 2k - 1, where k >= 0 The number of leaves in a depth k tree is 2k The total number of nodes in a depth k tree is 2k+1 - 1 The height of the tree with n nodes is log2(n + 1)-1 Proper Binary Tree All the internal nodes have two children. The leaves can be located at different depths. The number of internal nodes in a depth k tree is at least k and at most 2k - 1, where k >= 0 The number of leaves in a depth k tree is at least k + 1 and most 2k The total number of nodes in a depth k tree is at least 2k + 1 and at most 2k+1 – 1 The height of the tree with n nodes is at least log2(n + 1)-1 and at most (n-1)/2

2 Binary Tree Operations
Initialize a binary tree Decide if the tree is empty Combine two binary trees by merging their roots to the root of a new tree traverse access left or right child of a node access parent of a node access a node's data value Some operations relate to nodes(e.g. reach parent or child); others relate to the tree (e.g. initialize)

3 Tree Traversal In a traversal, each and every node is visited once
A full traversal produces a linear order for the nodes Traversal Methods: Inorder (Left Child, Node, Right Child) Preorder (Node, Left Child, Right Child) Postorder (Left Child, Right Child, Node) Level Order Traversal

4 Inorder Traversal (LNR: left, node, right)
algorithm inorder(Treeptr T); (T is a pointer to a node in a binary tree. For full tree traversal, pass inorder the pointer to the top of the tree ) begin if T != NULL then inorder(T->Lchild); write(T->element); inorder(T->Rchild); endif Example : output: FDBGEACH

5 PreOrder Traversal (NLR: node, left, right)
algorithm preorder(Treeptr T); (T is a pointer to a node in a binary tree. For full tree traversal, pass preorder the pointer to the top of the tree) begin if T != NULL then write(T->data); preorder(T->Lchild); preorder(T->Rchild); Preorder endif Output: ABDFEGCH

6 PostOrder Traversal (LRN: left, right, node)
algorithm postorder(Treeptr T); (T is a pointer to a node in a binary tree. For full tree traversal, pass postorder the pointer to the top of the tree) begin if T != NULL then postorder(T->Lchild); postorder(T->Rchild); write(T->data); endif Output: FDGEBHCA

7 Level-Order Traversal
First visit the root, then the root's left child, followed by the root's right child, continue in this manner, visiting nodes at each new level from leftmost node to rightmost node May need a queue structure to realize this traversal algorithm LevelOrder(Treeptr T); begin front = 0; rear = 0 {initialize queue} enqueue(T); while (! queue_empty() ) do dequeue(T); write(T->data); if T->Lchild not null then enqueue(T->Lchild); if T->Rchild not null then enqueue(T->Rchild); endwhile end algorithm Output: ABCDEHFG

8 Application: Expression Trees
Algorithm to construct an expression tree given a postfix expression (e.g. a b + c d e + * *) Read one symbol at a time If symbol is an operand, create a one-node tree and push pointer to it onto stack If symbol is an operator, pop pointers to two trees, T1 and T2, from the stack and form a new tree whose root is the operator and whose left and right children point to T2 and T1, respectively (T2 oper T1) Push the pointer to the new tree onto the stack

9 Example Construct an expression tree given a postfix expression (e.g. a b + c d e + * *)

10 Implementation issues
Separate implementations for leaf and internal nodes data for leaves only? Data of one type for leaves and a different type for internal nodes? May save space to have separate node implementations An example that illustrates the use of separate node types is the expression tree Expression Trees: Leaves store operands (constants or var names) Internal nodes store operators evaluation The following tree represents the expression (a-b) + (d*e) using an inorder traversal

11 Linked List Representation of Binary Trees
In a link-based tree, every node has two child pointers, even if they are null number of empty subtrees (leaves) in a non-empty binary tree is one more than the number of nodes in the tree, thus about half the pointers are wasted null values Overhead = amount of space needed to maintain the data structure depends on which nodes store data values and whether a parent pointer is defined If data stored only in leaf nodes, fraction of space needed depends on whether the tree is full if skewed tree, only one leaf node of data in worst case if full tree, fewer nodes contribute to overhead If each node has two pointers and a data value, the total space for a tree of n nodes is given by n(2p + d) , where p=space for a pointer and d=space for data value total overhead = 2pn fraction of overhead = 2p/(2p+d) if p=d then about 2/3 of total space for a full tree is overhead

12 Space Requirements for Binary Trees
If pointers eliminated from leaf nodes the overhead fraction is significantly reduced [n/2 (2p) ] / [n/2(2p) + dn] = p / (p+d) if p=d overhead is reduced to about 1/2 the total space but if only leaves store useful data, half the data space is also unused, so overhead fraction is really 3/4 of the total space! Better scheme for full binary tree that stores data in the leaves: store two pointers and no data in internal nodes store only data in leaf nodes this requires pn + d(n/2) units of space (n/2 internal nodes) if p=d, overhead is approx. p / (2p+d) = 1/3

13 Array Representation for Binary Trees
An array of size 2h+1 –1, at most, is required to represent a binary tree with height h. For any node v, 1 <= v <= n, assign a number p(v) to the node as follows: If v is the root then p(v) = 1. If v is the left child of node u, then p(v) = 2p(u) If v is the right child of node u, then p(v) = 2p(u) + 1 Node v is stored at location p(v). No overhead if array is size n for a tree of n nodes The following is a proper binary tree

14 Linked List Tree Class /*1*/ class BinNode{ /*2*/ public:
/*3*/ ELEM element; //value stored in a node /*4*/ BinNode* left() const; //return pointer to left child /*5*/ BinNode* right() const; //return pointer to right child //constructors /*6*/ BinNode () /*7*/ { /*8*/ left = right = NULL; /*9*/ } /*10*/ BinNode (ELEM e, BinNode* lft = NULL, BinNode* rt = NULL){ /*11*/ element = e; left = lft; right = rt; /*12*/ } /*13*/ ~BinNode() { }; //destructor //Accessor functions /*14*/ BinNode* leftchild() const { return left; } /*15*/ BinNode* rightchild() const { return right; } /*16*/ ELEM value() const; //return value /*17*/ void setValue (ELEM val); /*18*/ bool isLeaf() const; /*19*/ };

15 Disadvantages of Array Representation
A lot of space is wasted if the tree is skewed in worst case, a skewed tree of depth k will require 2k- 1 spaces. Of these, only k will be used. Skewed tree: Computation time increases for traversals when a tree is skewed Deletion of nodes leaves gaps in storage Insertion of nodes from middle of the tree requires movement of potentially many nodes Arrays are suitable only if the tree is complete

16 Search Trees Search times depend on how data is stored
If a large amount of data is stored in a pointer-based list, then searching for an element will take O(n) time in the average case, whether the list is ordered or not If the list is ordered and stored in an array an element can be found in O(log n) time with a binary search, but inserting a new element will take O(n) time We would like to organize a collection of data so that both insertion and searching can be done quickly Binary search trees offer a solution

17


Download ppt "Binary Tree ADT: Properties"

Similar presentations


Ads by Google