void inOrderTraverse(TreeNode* root){ if(root->left) inOrderTraverse(root->left); arr.push_back(root->val); if(root->right) inOrderTraverse(root->right); }
void inOrderTraverse(const Node& n, int *currentMode, int *modeCount, int* potentialMode, int *potentialCount) { if (n.left != nullptr) inOrderTraverse(*(n.left), currentMode, modeCount, potentialMode, potentialCount); if (*potentialCount > *modeCount) { *currentMode = *potentialMode; *modeCount = *potentialCount; } else if (*potentialMode != n.value) { *potentialMode = n.value; *potentialCount = 1; } else { (*potentialCount)++; } if (n.right != nullptr) inOrderTraverse(*(n.right), currentMode, modeCount, potentialMode, potentialCount); }
void inOrderTraverse(struct BSTNode* root){ if(root == NULL) return; inOrderTraverse(root->left); printf("Node value : %d\n",root->value); inOrderTraverse(root->right); return; }
/** * 前序遍历 */ void inOrderTraverse(BiTree T) { if(T==NULL) return; inOrderTraverse(T->lchild); printf("%c ", T->data); inOrderTraverse(T->rchild); }
//中序递归遍历T,对每个结点调用函数visit一次且仅一次 void inOrderTraverse(BiTreeLink T,int(*visit)(TElemType)){ //T不空 if(T){ inOrderTraverse(T->lchild,visit);//再中序遍历左子树 //再访问根结点 (*visit)(T->data);//visit指向函数的入口地址 inOrderTraverse(T->rchild,visit);//最后中序遍历右子树 } }
/* Left Subtree -> Root -> Right Sub-tree */ void inOrderTraverse(struct node *root) { if (root == NULL) return; inOrderTraverse(root->left); printf(" [%d] ", root->val); inOrderTraverse(root->right); }
void inOrderTraverse(Node* root) { if (root == NULL) return; inOrderTraverse(root->left); printf("%d\n", root->value); inOrderTraverse(root->right); }
void inOrderTraversalLCRSTree(Tree *tree, void *key, ProcessItem processItem, compareTo compare){ List *list = NULL; inOrderTraverse(tree->root, processItem, list, compare); }
int main() { BiTree T; //printf("input former sequence traversal, like:\nab#d##c##\n"); createBiTree(&T); inOrderTraverse(T); printf("\n"); return 0; }
bool isValidBST(TreeNode* root){ if (root == NULL) return true; arr.clear(); inOrderTraverse(root); if (arr.size() == 1) return true; for(int i = 0; i < arr.size() - 1; i++){ if(arr[i] >= arr[i+1]) return false; } return true; }
int findModeBST(const Node& root) { //assuming root is always a valid Node pointer int currentMode = root.value; int modeCount = 1; int potentialMode; int potentialCount; inOrderTraverse(root, ¤tMode, &modeCount, &potentialMode, &potentialCount); return currentMode; }
int main() { printf("Please input the values of nodes seperated by space with -1 denotes termination\n"); BTree root=(BTree)malloc(sizeof(BiTNode)); root->left=NULL; createSortedTree(&(root->left)); printf("create done! print\n"); printf("in order sequence:\n"); inOrderTraverse(root->left); printf("\npost order sequence(1):\n"); postOrderTraverse(root->left); printf("\npost order sequence(2):\n"); postOrderTraverse2(root->left); printf("\n The level sequenece is:\n"); levelTraverse(root->left); printf("\nlink view:\n"); linkTraverse(tree2link(root->left)); }
int main() { int nodes; printf("Your first BST...\n"); printf("Enter number of nodes.."); scanf("%d", &nodes); int num = 0; int val; /* Initialize tree root node */ struct node *root = NULL; /* TCreation of BST */ for (; num <nodes; num++) { int val; scanf("%d", &val); root = EnterNode(root, val); } /* Traversal */ printf("\nNow print PreOrder Traversal.."); preOrderTraverse(root); printf("\nNow print PostOrder Traversal.."); postOrderTraverse(root); printf("\nNow print InOrder Traversal.."); inOrderTraverse(root); /* Deletion of a node */ printf("\n Now we want to delete a node in BST..\n"); printf("Enter value of node to be deleted...\n"); scanf("%d", &val); // DeleteNodeInBST(root, val); /* Finding a node */ printf("\n Now we want to Find a node in BST..\n"); printf("Enter value of nodes to be found...\n"); scanf("%d", &val); FindNodeInBST(root, val); }
int main() { Node* root = NULL; constructBST(root, 3); constructBST(root, 2); constructBST(root, 1); constructBST(root, 4); constructBST(root, 0); printf("in order traverse the BST:\n"); inOrderTraverse(root); printf("------------------\n"); int maxDep = maxDepth(root); printf("maxDep : %d\n", maxDep); printf("------------------\n"); getLongest(root); printf("longest : %d\n", longest); return 0; }
void main(){ int i; BiTreeLink T,p,c; TElemType e1,e2; initBiTree(&T); //判断数是否为空,求树的深度 printf("\nInit a Binary Tree!\nThe tree is empty or not?%d(1:yes 0:no);The tree depth=%d\n",biTreeEmpty(T),biTreeDepth(T)); //寻找根结点 e1 = root(T); if(e1!=Nil) printf("\nThe root of the Binary Tree is:'%c'\n",e1); else printf("\nThe Binary Tres has no root\n"); //请先序输入二叉树(如:ab三个空格表示a为根结点,b为左子树的二叉树) printf("\nBuild a Binary Tree!like abc@@de@g@@f@@hi@j@@k@@,@stand for space\n"); createBiTree(&T); //判断数是否为空,求树的深度 printf("\nThe tree is empty or not?%d(1:yes 0:no);The tree depth=%d\n",biTreeEmpty(T),biTreeDepth(T)); //寻找根结点 e1 = root(T); if(e1!=Nil) printf("\nThe root of the Binary Tree is:'%c'\n",e1); else printf("\nThe Binary Tres has no root\n"); //层序递归遍历(Of sequence to traverse the binary tree) printf("\nlevel order Traverse the Binary Tree:\n"); levelOrderTraverse(T,visitT); //先序遍历 printf("\npreOrder Traverse the Binary Tree:\n"); preOrderTraverse(T,visitT); //中序递归遍历 printf("\n\ninOrder recursion Traverse the Binary Tree:\n"); inOrderTraverse(T,visitT); //中序非递归遍历1 printf("\ninOrder non-recursion Traverse the Binary Tree 1:\n"); inOrderTraverse1(T,visitT); //中序非递归遍历2 printf("inOrder non-recursion Traverse the Binary Tree 2:\n"); inOrderTraverse2(T,visitT); //后序递归遍历 printf("\npostOrder recursion Traverse the Binary Tree:\n"); postOrderTraverse(T,visitT); //修改结点 e1 = 'd';//原值 //scanf("%c",&e1); p = point(T,e1);//获得e1对应的指针 //获得对应结点的值 printf("\n\nKnow the previous vaule is:'%c'\nEnter the new vlaue:\n",value(p)); e2 = 'r';//新结点的值 //scanf("%c",&e2); assign(p,e2);//赋新值 //先序遍历 printf("preOrder Traverse the Binary Tree:\n"); preOrderTraverse(T,visitT); //寻找双亲 e1 = parent(T,e2); if(e1!=Nil) printf("\n\nthe parent of '%c' is : '%c'\n",e2,e1); else printf("'%c' has no parent\n",e2); //寻找左孩子 e1 = leftChild(T,e2); if(e1!=Nil) printf("\nthe left child of '%c' is : '%c'\n",e2,e1); else printf("'%c' has no left child\n",e2); //寻找右孩子 e1 = rightChild(T,e2); if(e1!=Nil) printf("\nthe right child of '%c' is : '%c'\n",e2,e1); else printf("'%c' has no right child\n",e2); //寻找左兄弟 e1 = leftSibling(T,e2); if(e1!=Nil) printf("\nthe left sibling of '%c' is : '%c'\n",e2,e1); else printf("'%c' has no left sibling\n",e2); //寻找右兄弟 e1 = rightSibling(T,e2); if(e1!=Nil) printf("\nthe right sibiling of '%c' is : '%c'\n",e2,e1); else printf("'%c' has no right sibiling\n",e2); //初始化需要插入的树 initBiTree(&c);//s=jk //这里有三个空格 printf("\nBuild the Binary Tree c which has no right child:\n"); c = (BiTreeLink)malloc(sizeof(BiTNode)); p = (BiTreeLink)malloc(sizeof(BiTNode)); c->lchild = p; c->rchild = NULL; c->data = 'm'; p->lchild = p->rchild = NULL; p->data = 'n'; //createBiTree(&c); //先序递归遍历 printf("\npreOrder Traverse the Binary Tree:\n"); preOrderTraverse(c,visitT); //树s插到树T中,请输入树T中树s的双亲结点 s为左(0)或右(1)子树: printf("\n\nInsert the Tree s to the Tree T,enter the parent of the Tree c in the Tree T,left Tree(0) and right Tree(1):\n"); e1= 'b';i = 0;//将子树c作为结点'b'的左子树 //scanf("%c%d",&e1,&i); p = point(T,e1);//p是T中树c的双亲结点指针 insertChild(p,i,c); //先序递归遍历 printf("\npreOrder Traverse the Binary Tree:\n"); preOrderTraverse(T,visitT); // 删除子树,请输入待删除子树根结点 左(0)或右(1)子树 printf("\n\nDelete the Tree s,enter the root of the deleting Child Tree, left Tree(0) and right Tree(1):\n"); e1= 'b';i = 1;//删除父结点为'b'的右子树 p = point(T,e1);//p是T中树c的双亲结点指针 deleteChild(p,i); //先序递归遍历 printf("\npreOrder Traverse the Binary Tree:\n"); preOrderTraverse(T,visitT); //清空子树 clearBiTree(&T); printf("\n\nEmpty the Binary Tree?%d(1:yes 0:no)\nThe tree depth=%d\n",biTreeEmpty(T),biTreeDepth(T)); //寻找根结点 e1 = root(T); if(e1!=Nil) printf("\nThe root of the Binary Tree is:'%c'\n",e1); else printf("\nThe Binary Tres has no root\n"); }