5.3.5—二叉查找树—Convert Sorted List to Binary Sear Tree

来源:互联网 发布:mmd双人动作数据 编辑:程序博客网 时间:2024/06/08 02:24
描述
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced
BST.

#pragma once#include<iostream>struct BinaryTreeNode{int                    m_nValue;BinaryTreeNode*        m_pLeft;BinaryTreeNode*        m_pRight;};BinaryTreeNode* CreateBinaryTreeNode(int value);void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight);void PrintTreeNode(BinaryTreeNode* pNode);void PrintTree(BinaryTreeNode* pRoot);void DestroyTree(BinaryTreeNode* pRoot);


#include "BinaryTree.h"BinaryTreeNode* CreateBinaryTreeNode(int value){BinaryTreeNode* pNode = new BinaryTreeNode();pNode->m_nValue = value;pNode->m_pLeft = NULL;pNode->m_pRight = NULL;return pNode;}void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight){if (pParent != NULL){pParent->m_pLeft = pLeft;pParent->m_pRight = pRight;}}void PrintTreeNode(BinaryTreeNode* pNode){if (pNode != NULL){printf("value of this node is: %d\n", pNode->m_nValue);if (pNode->m_pLeft != NULL)printf("value of its left child is: %d.\n", pNode->m_pLeft->m_nValue);elseprintf("left child is null.\n");if (pNode->m_pRight != NULL)printf("value of its right child is: %d.\n", pNode->m_pRight->m_nValue);elseprintf("right child is null.\n");}else{printf("this node is null.\n");}printf("\n");}void PrintTree(BinaryTreeNode* pRoot){PrintTreeNode(pRoot); if (pRoot != NULL){if (pRoot->m_pLeft != NULL)PrintTree(pRoot->m_pLeft);if (pRoot->m_pRight != NULL)PrintTree(pRoot->m_pRight);}}void DestroyTree(BinaryTreeNode* pRoot){if (pRoot != NULL){BinaryTreeNode* pLeft = pRoot->m_pLeft;BinaryTreeNode* pRight = pRoot->m_pRight;delete pRoot;pRoot = NULL;DestroyTree(pLeft);DestroyTree(pRight);}}
#ifndef LISTNODE_H_#define LISTNODE_H_#include<iostream>#include<vector>;using namespace std;struct node{int data;node *next;};node *CreateNode(int a[], int n);void DisplayNode(node *head);int ListLength(node*head);void DeleteNode(node *head);node *nth_node(node *head,int len);#endif#include"ListNode.h"node *CreateNode(int a[], int n){if (n <= 0) return NULL;node *head = new node();head->data = a[0];node *p = head;for (int i = 1; i < n; i++){node *q = new node();q->data = a[i];p->next = q;p = q;}p->next = NULL;return  head;}void DisplayNode(node *head){node *p = head;while (p){cout << p->data << " ";p = p->next;}cout << endl;}int ListLength(node *head){node *p = head;int length = 0;while (p){length++;p = p->next;}return  length;}void DeleteNode(node *head){node *p = head;while (head){head = p->next;delete p;p = head;}}node *nth_node(node *head, int len){if (len <= 0 || len > ListLength(head))return NULL;node *p = head;for (int i = 1; i < len; i++){p = p->next;}return p;}
#include "BinaryTree.h"#include"ListNode.h"#include<algorithm>#include<vector>#include<stack>using namespace std;//===由有序链表构建BSTBinaryTreeNode *ConSortListToBST(node *head, int len){if (len == 0)return NULL;if (len==1)return CreateBinaryTreeNode(head->data);node *temp = nth_node(head,len / 2 + 1);BinaryTreeNode *proot = CreateBinaryTreeNode(temp->data);proot->m_pLeft = ConSortListToBST(head, len / 2);proot->m_pRight = ConSortListToBST(nth_node(head, len / 2 + 2), (len-1)/2);return proot;}BinaryTreeNode *ConSortListToBST(node *head){int length = ListLength(head);return ConSortListToBST(head, length);}int main(){const int n = 6;int a[n] = { 1, 3, 5, 7, 9, 11};node *head = CreateNode(a, n);BinaryTreeNode *proot = ConSortListToBST(head);//===PrintTree(proot);//===DestroyTree(proot);DeleteNode(head);}




阅读全文
0 0
原创粉丝点击