Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees. Family Tree Recursive Definition Tree - Definition A tree is a finite set of one or more nodes such that: 1.There is a specially designated node.

Similar presentations


Presentation on theme: "Trees. Family Tree Recursive Definition Tree - Definition A tree is a finite set of one or more nodes such that: 1.There is a specially designated node."— Presentation transcript:

1 Trees

2 Family Tree

3 Recursive Definition Tree - Definition A tree is a finite set of one or more nodes such that: 1.There is a specially designated node called the root. 2.The remaining nodes are partitioned in n  0 disjoint sets T 1, T 2, …, T n, where each of these sets is a tree. 3. T 1, T 2, …, T n are called the sub-trees of the root.

4 Family Tree root Sub-trees Banu Hashim

5 A FKB CHD I L N X PGMQ Tree

6 A FKB CHD I L N X PGMQ A Root (no parent) FKB CDX Intermediate nodes (has a parent and at least one child) H I L NPGMQ Leaf nodes (0 children) Node Types

7 A FKB CHD I L N X PGMQ A C Degree 3 F X Degree 1 H I L NPGMQ Degree 0 KB D Degree 2 Degree of a Node Number of Children

8 A FKB CHD I L N X PGMQ A Level 0 FKB Level 1 CHDLX Level 2 INPGMQ Level 3 Level of a Node Distance from the root Height of the Tree = Maximum Level + 1

9 Examples of Binary Trees A KB CHLX PMQ A B C Q M

10 Properties of Binary Trees The maximum number of nodes on level i of a binary tree is 2 i The maximum number of nodes in a binary tree of height k is 2 k – 1

11 Full Binary Tree A binary tree of height k having 2 k – 1 nodes is called a full binary tree 1 32 5476 89101112131415

12 Complete Binary Tree A binary tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right, is called a complete binary tree 1 32 5476 89101112

13 Binary Tree No node has a degree > 2 struct TreeNode { int data; TreeNode*left, *right;// left subtree and right subtree }; Class BinaryTree { private: TreeNode * root; public: BinaryTree() { root = NULL; } void add (int data); void remove (int data); void InOrder();// In order traversal ~ BinaryTree(); };

14 Binary Tree Traversal In order Traversal (LNR) void BinaryTree::InOrder()// work horse function { InOrder(root); } void BinaryTree::InOrder(TreeNode *t) { if (t) { InOrder(t->left); visit(t); InOrder(t->right); } void BinaryTree::visit (TreeNode *t) { cout data; }

15 A KB CHLX PMQ A B C QQ Q Q C C MM M M C B B HH H H B A A K LL L L K K XX X PP P P X K A if (t) { InOrder(t->left); visit(t); InOrder(t->right); } Binary Tree Traversal In Order Traversal (LNR)

16 Binary Tree Traversal Pre Order Traversal (NLR) void BinaryTree::PreOrder() { PreOrder(root); } void BinaryTree::PreOrder(TreeNode *t) // work horse function { if (t) { visit(t); PreOrder(t->left); PreOrder(t->right); } void BinaryTree::visit (TreeNode *t) { cout data; }

17 A KB CHLX PMQ A B C QQQ C MMM C B HHH B A K LLL K XX PPP X K A if (t) { visit(t); PreOrder(t->left); PreOrder(t->right); } Binary Tree Traversal Pre Order Traversal (NLR) ABCQMHKLXP

18 Binary Tree Traversal Post Order Traversal (LRN) void BinaryTree::PostOrder()// work horse function { PostOrder(root); } void BinaryTree::PostOrder(TreeNode *t) { if (t) { PostOrder(t->left); PostOrder(t->right); visit(t); } void BinaryTree::visit (TreeNode *t) { cout data; }

19 A KB CHLX PMQ A B C QQ Q Q C C MM M M C B B HH H H B A A K LL L L K K XX X PP P P X K A if (t) { PostOrder(t->left); PostOrder(t->right); visit(t); } Binary Tree Traversal Post Order Traversal (LRN)

20 Binary Tree Traversal A KB CHLX PMQ NLR – visit when at the left of the Node ABCQMHKLXP LNR – visit when under the Node QCMBHALKXP LRN – visit when at the right of the Node QMCHBLPXKA

21 - /+ A*D* FCBE LNR: A+B*C-D/E*F NLR: -+A*BC/D*EF LRN: ABC*+DEF*/- Expression Tree

22 BinaryTree ::~ BinaryTree(); Delete both the left child and right child before deleting itself LRN Which Algorithm?


Download ppt "Trees. Family Tree Recursive Definition Tree - Definition A tree is a finite set of one or more nodes such that: 1.There is a specially designated node."

Similar presentations


Ads by Google