You are on page 1of 5

Moduri de parcurgere: -preordine: utilizare informatie radacina, parcurgere preordine subarbore stang, parcurgere preordine subarbore drept -inordine:

parcurgere inordine subarbore stang, utilizare informatie radacina, parcurgere inordine subarbore drept -postordine: parcurgere postordine subarbore stang, parcurgere postordine subarbore drept, utilizare informatie radacina

Preorder traversal sequence: F, B, A, D, C, E, G, I, H (root, left, right) Inorder traversal sequence: A, B, C, D, E, F, G, H, I (left, root, right); note how this produces a sorted sequence Postorder traversal sequence: A, C, E, D, B, H, I, G, F (left, right, root) Level-order traversal sequence: F, B, G, A, D, I, C, E, H

The number of nodes n in a perfect binary tree can be found using this formula: n = 2h + 1 1 where h is the height of the tree. The number of nodes n in a complete binary tree is minimum: n = 2h and maximum: n = 2h + 1 1 where h is the height of the tree. The number of leaf nodes L in a perfect binary tree can be found using this formula: L = 2h where h is the height of the tree. The number of nodes n in a perfect binary tree can also be found using this formula: n = 2L 1 where L is the number of leaf nodes in the tree. The number of NULL links in a Complete Binary Tree of n-node is (n+1). The number of leaf nodes in a Complete Binary Tree of n-node is . For any non-empty binary tree with n0 leaf nodes and n2 nodes of degree 2, n0 = n2 + 1.[5] n = n0 + n1 + n2 + n4 + n3 + n5 + .... + nB-1 + nB

B = n - 1, n = 1 + 1*n1 + 2*n2 + 3*n3 + 4*n4 + ... + B*nB, NOT include n0

#include <iostream> using namespace std; void sortare(int a[], int len) { int i, j, aux, x; for(i = 1; i<len; i++) { cout << "i=" << i << "\t"; for(x=0; x<len; x++) cout << a[x] << " "; if(x==len&&i==1) cout << " (nesortat)\n"; else cout << "\n"; j = i; while (j > 0 && a[j - 1] > a[j]) { aux = a[j]; a[j] = a[j - 1]; a[j - 1] = aux; j--; } } } int main() { int v[] = { 35,7,4,8,32,6 }; sortare(v, 6) ; cout << "i=6\t"; for(int i=0; i<6; i++) cout << v[i] << " "; cout << " (sortat)"; cin.get(); return 0; }

// Height of a tree using a marker. int height(node * tree) { node * tmp; queue *qptr; node * marker; marker = (node*)malloc(sizeof(node)); marker->left = NULL; marker->right = NULL;

marker->val = -111; qptr = (queue*)malloc(sizeof(queue)); initialize(qptr); int oneInsertion = -1; int height = 0; if(tree) { insert(tree,qptr); insert(marker,qptr); height++; while(!emptyQ(qptr)) { tmp = dele_te(qptr); oneInsertion = -1; while(tmp->val != marker->val) { // Make sure that atleast one insertion could be done. // If inserting leaf node only, then do not insert the arker. // This is the terminating condition for the marker insertion. if(tmp->left || tmp->right || (tmp->left && tmp->right)) oneInsertion++; if(tmp->left) insert(tmp->left,qptr); if(tmp->right) insert(tmp->right,qptr); tmp = dele_te(qptr); } if(oneInsertion > -1) { insert(marker,qptr); height++; } } return height;

#include <stdio.h> #include <stdlib.h> /* A binary tree node has data, pointer to left child and a pointer to right child */ struct node { int data; struct node* left; struct node* right; }; /* Function to get the count of leaf nodes in a binary tree*/ unsigned int getLeafCount(struct node* node) { if(node == NULL) return 0; if(node->left == NULL && node->right==NULL) return 1; else return getLeafCount(node->left)+ getLeafCount(node->right); } /* Helper function that allocates a new node with the given data and NULL left and right pointers. */ struct node* newNode(int data) { struct node* node = (struct node*) malloc(sizeof(struct node)); node->data = data; node->left = NULL; node->right = NULL; return(node); } /*Driver program to test above functions*/ int main() { /*create a tree*/ struct node *root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); /*get leaf count of the above created tree*/ printf("Leaf count of the tree is %d", getLeafCount(root));

getchar(); return 0; }

You might also like